@graphql-tools/batch-delegate 9.0.26 → 9.0.27-alpha-265e02fbe59e09be954c62a869987ea85383c0eb
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +66 -0
- package/dist/index.cjs +7 -3
- package/dist/index.js +7 -3
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,71 @@
|
|
|
1
1
|
# @graphql-tools/batch-delegate
|
|
2
2
|
|
|
3
|
+
## 9.0.27-alpha-265e02fbe59e09be954c62a869987ea85383c0eb
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#396](https://github.com/graphql-hive/gateway/pull/396) [`265e02f`](https://github.com/graphql-hive/gateway/commit/265e02fbe59e09be954c62a869987ea85383c0eb) Thanks [@ardatan](https://github.com/ardatan)! - Memoize the key arguments correctly;
|
|
8
|
+
|
|
9
|
+
With the following schema and resolvers, `userById` should batch all the requests to `usersByIds`;
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
{
|
|
13
|
+
typeDefs: /* GraphQL */ `
|
|
14
|
+
type User {
|
|
15
|
+
id: ID!
|
|
16
|
+
email: String!
|
|
17
|
+
}
|
|
18
|
+
type Query {
|
|
19
|
+
userById(id: ID!): User
|
|
20
|
+
usersByIds(ids: [ID!]): [User]
|
|
21
|
+
}
|
|
22
|
+
`,
|
|
23
|
+
resolvers: {
|
|
24
|
+
Query: {
|
|
25
|
+
usersByIds: (_root, args) => {
|
|
26
|
+
return args.ids.map((id: string) => users.find((user) => user.id === id));
|
|
27
|
+
},
|
|
28
|
+
userById: (root, args, context, info) => {
|
|
29
|
+
return batchDelegateToSchema({
|
|
30
|
+
schema: userSubschema,
|
|
31
|
+
fieldName: 'usersByIds',
|
|
32
|
+
key: args.id,
|
|
33
|
+
rootValue: root,
|
|
34
|
+
context,
|
|
35
|
+
info,
|
|
36
|
+
})
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
}
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
This query should batch all the requests to `usersByIds`:
|
|
44
|
+
|
|
45
|
+
```graphql
|
|
46
|
+
{
|
|
47
|
+
userById(id: "1") {
|
|
48
|
+
id
|
|
49
|
+
email
|
|
50
|
+
}
|
|
51
|
+
userById(id: "2") {
|
|
52
|
+
id
|
|
53
|
+
email
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
The delegation request should be;
|
|
59
|
+
|
|
60
|
+
```graphql
|
|
61
|
+
{
|
|
62
|
+
usersByIds(ids: ["1", "2"]) {
|
|
63
|
+
id
|
|
64
|
+
email
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
3
69
|
## 9.0.26
|
|
4
70
|
|
|
5
71
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
|
@@ -9,8 +9,9 @@ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
|
9
9
|
|
|
10
10
|
var DataLoader__default = /*#__PURE__*/_interopDefault(DataLoader);
|
|
11
11
|
|
|
12
|
+
const DEFAULT_ARGS_FROM_KEYS = (keys) => ({ ids: keys });
|
|
12
13
|
function createBatchFn(options) {
|
|
13
|
-
const argsFromKeys = options.argsFromKeys ??
|
|
14
|
+
const argsFromKeys = options.argsFromKeys ?? DEFAULT_ARGS_FROM_KEYS;
|
|
14
15
|
const fieldName = options.fieldName ?? options.info.fieldName;
|
|
15
16
|
const { valuesFromResults, lazyOptionsFn } = options;
|
|
16
17
|
return function batchFn(keys) {
|
|
@@ -74,7 +75,9 @@ function getLoader(options) {
|
|
|
74
75
|
dataLoaderOptions,
|
|
75
76
|
fieldNodes = info.fieldNodes[0] && delegate.getActualFieldNodes(info.fieldNodes[0]),
|
|
76
77
|
selectionSet = fieldNodes?.[0]?.selectionSet,
|
|
77
|
-
returnType = info.returnType
|
|
78
|
+
returnType = info.returnType,
|
|
79
|
+
argsFromKeys = DEFAULT_ARGS_FROM_KEYS,
|
|
80
|
+
key
|
|
78
81
|
} = options;
|
|
79
82
|
const loaders = getLoadersMap(context ?? GLOBAL_CONTEXT, schema);
|
|
80
83
|
let cacheKey = fieldName;
|
|
@@ -87,7 +90,8 @@ function getLoader(options) {
|
|
|
87
90
|
}
|
|
88
91
|
const fieldNode = fieldNodes?.[0];
|
|
89
92
|
if (fieldNode?.arguments) {
|
|
90
|
-
|
|
93
|
+
const args = argsFromKeys([key]);
|
|
94
|
+
cacheKey += fieldNode.arguments.filter((arg) => arg.name.value in args).map((arg) => memoizedPrint(arg)).join(",");
|
|
91
95
|
}
|
|
92
96
|
let loader = loaders.get(cacheKey);
|
|
93
97
|
if (loader === void 0) {
|
package/dist/index.js
CHANGED
|
@@ -3,8 +3,9 @@ import { memoize2, memoize1, fakePromise, mapMaybePromise, relocatedError } from
|
|
|
3
3
|
import DataLoader from 'dataloader';
|
|
4
4
|
import { print, getNamedType, GraphQLList } from 'graphql';
|
|
5
5
|
|
|
6
|
+
const DEFAULT_ARGS_FROM_KEYS = (keys) => ({ ids: keys });
|
|
6
7
|
function createBatchFn(options) {
|
|
7
|
-
const argsFromKeys = options.argsFromKeys ??
|
|
8
|
+
const argsFromKeys = options.argsFromKeys ?? DEFAULT_ARGS_FROM_KEYS;
|
|
8
9
|
const fieldName = options.fieldName ?? options.info.fieldName;
|
|
9
10
|
const { valuesFromResults, lazyOptionsFn } = options;
|
|
10
11
|
return function batchFn(keys) {
|
|
@@ -68,7 +69,9 @@ function getLoader(options) {
|
|
|
68
69
|
dataLoaderOptions,
|
|
69
70
|
fieldNodes = info.fieldNodes[0] && getActualFieldNodes(info.fieldNodes[0]),
|
|
70
71
|
selectionSet = fieldNodes?.[0]?.selectionSet,
|
|
71
|
-
returnType = info.returnType
|
|
72
|
+
returnType = info.returnType,
|
|
73
|
+
argsFromKeys = DEFAULT_ARGS_FROM_KEYS,
|
|
74
|
+
key
|
|
72
75
|
} = options;
|
|
73
76
|
const loaders = getLoadersMap(context ?? GLOBAL_CONTEXT, schema);
|
|
74
77
|
let cacheKey = fieldName;
|
|
@@ -81,7 +84,8 @@ function getLoader(options) {
|
|
|
81
84
|
}
|
|
82
85
|
const fieldNode = fieldNodes?.[0];
|
|
83
86
|
if (fieldNode?.arguments) {
|
|
84
|
-
|
|
87
|
+
const args = argsFromKeys([key]);
|
|
88
|
+
cacheKey += fieldNode.arguments.filter((arg) => arg.name.value in args).map((arg) => memoizedPrint(arg)).join(",");
|
|
85
89
|
}
|
|
86
90
|
let loader = loaders.get(cacheKey);
|
|
87
91
|
if (loader === void 0) {
|
package/package.json
CHANGED