-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(batch-delegate): memoize the key arguments correctly (#396)
- Loading branch information
Showing
5 changed files
with
422 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
'@graphql-tools/batch-delegate': patch | ||
--- | ||
|
||
Memoize the key arguments correctly; | ||
|
||
With the following schema and resolvers, `userById` should batch all the requests to `usersByIds`; | ||
|
||
```ts | ||
{ | ||
typeDefs: /* GraphQL */ ` | ||
type User { | ||
id: ID! | ||
email: String! | ||
} | ||
type Query { | ||
userById(id: ID!): User | ||
usersByIds(ids: [ID!]): [User] | ||
} | ||
`, | ||
resolvers: { | ||
Query: { | ||
usersByIds: (_root, args) => { | ||
return args.ids.map((id: string) => users.find((user) => user.id === id)); | ||
}, | ||
userById: (root, args, context, info) => { | ||
return batchDelegateToSchema({ | ||
schema: userSubschema, | ||
fieldName: 'usersByIds', | ||
key: args.id, | ||
rootValue: root, | ||
context, | ||
info, | ||
}) | ||
}, | ||
}, | ||
}, | ||
} | ||
``` | ||
|
||
This query should batch all the requests to `usersByIds`: | ||
|
||
```graphql | ||
{ | ||
userById(id: "1") { | ||
id | ||
} | ||
userById(id: "2") { | ||
id | ||
} | ||
} | ||
``` | ||
|
||
The delegation request should be; | ||
|
||
```graphql | ||
{ | ||
usersByIds(ids: ["1", "2"]) { | ||
id | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.