@graphql-tools/batch-execute 10.0.7 → 10.0.8-alpha-30e125d6da4fc2fb20c6a00f6979646b1bea19f6

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 CHANGED
@@ -1,5 +1,13 @@
1
1
  # @graphql-tools/batch-execute
2
2
 
3
+ ## 10.0.8-alpha-30e125d6da4fc2fb20c6a00f6979646b1bea19f6
4
+ ### Patch Changes
5
+
6
+
7
+
8
+ - [#2210](https://github.com/graphql-hive/gateway/pull/2210) [`31b3f24`](https://github.com/graphql-hive/gateway/commit/31b3f247a46d3e942077dda55fb23b4d2d3132a7) Thanks [@ardatan](https://github.com/ardatan)! - Inherit `operationName`, `operationType`, `context`, `info`, and `subgraphName` from any available request in the batch.
9
+ If `operationName` is not defined, it will be inherited from the operation itself, if it has a name.
10
+
3
11
  ## 10.0.7
4
12
  ### Patch Changes
5
13
 
package/dist/index.cjs CHANGED
@@ -47,22 +47,40 @@ function parseKeyFromPath(path) {
47
47
  }
48
48
 
49
49
  function mergeRequests(requests, extensionsReducer) {
50
- if (requests.length === 1) {
51
- return requests[0];
50
+ const firstRequest = requests[0];
51
+ if (!firstRequest) {
52
+ throw new Error("At least one request is required");
53
+ }
54
+ const requestCount = requests.length;
55
+ if (requestCount === 1) {
56
+ return firstRequest;
52
57
  }
53
- const subgraphName = requests[0].subgraphName;
54
58
  const mergedVariables = /* @__PURE__ */ Object.create(null);
55
59
  const mergedVariableDefinitions = [];
56
60
  const mergedSelections = [];
57
61
  const mergedFragmentDefinitions = [];
58
62
  let mergedExtensions = /* @__PURE__ */ Object.create(null);
59
- for (let index = 0; index < requests.length; index++) {
63
+ let subgraphName;
64
+ let operationName;
65
+ let operationType;
66
+ let context;
67
+ let info;
68
+ let rootValue;
69
+ for (let index = 0; index < requestCount; index++) {
60
70
  const request = requests[index];
61
71
  if (request) {
72
+ subgraphName ??= request.subgraphName;
73
+ operationName ??= request.operationName;
74
+ operationType ??= request.operationType;
75
+ context ??= request.context;
76
+ info ??= request.info;
77
+ rootValue ??= request.rootValue;
62
78
  const prefixedRequests = prefixRequest(createPrefix(index), request);
63
79
  for (const def of prefixedRequests.document.definitions) {
64
80
  if (isOperationDefinition(def)) {
65
81
  mergedSelections.push(...def.selectionSet.selections);
82
+ operationType ||= def.operation;
83
+ operationName ||= def.name?.value;
66
84
  if (def.variableDefinitions) {
67
85
  mergedVariableDefinitions.push(...def.variableDefinitions);
68
86
  }
@@ -75,11 +93,10 @@ function mergeRequests(requests, extensionsReducer) {
75
93
  mergedExtensions = extensionsReducer(mergedExtensions, request);
76
94
  }
77
95
  }
78
- const firstRequest = requests[0];
79
- if (!firstRequest) {
80
- throw new Error("At least one request is required");
96
+ if (operationName == null) {
97
+ operationName = info?.operation?.name?.value;
81
98
  }
82
- const operationType = firstRequest.operationType ?? utils.getOperationASTFromRequest(firstRequest).operation;
99
+ operationType ||= "query";
83
100
  const mergedOperationDefinition = {
84
101
  kind: graphql.Kind.OPERATION_DEFINITION,
85
102
  operation: operationType,
@@ -87,15 +104,12 @@ function mergeRequests(requests, extensionsReducer) {
87
104
  selectionSet: {
88
105
  kind: graphql.Kind.SELECTION_SET,
89
106
  selections: mergedSelections
90
- }
91
- };
92
- const operationName = firstRequest.operationName ?? firstRequest.info?.operation?.name?.value;
93
- if (operationName) {
94
- mergedOperationDefinition.name = {
107
+ },
108
+ name: operationName ? {
95
109
  kind: graphql.Kind.NAME,
96
110
  value: operationName
97
- };
98
- }
111
+ } : void 0
112
+ };
99
113
  return {
100
114
  subgraphName,
101
115
  document: {
@@ -104,10 +118,11 @@ function mergeRequests(requests, extensionsReducer) {
104
118
  },
105
119
  variables: mergedVariables,
106
120
  extensions: mergedExtensions,
107
- context: firstRequest.context,
108
- info: firstRequest.info,
121
+ context,
122
+ info,
109
123
  operationType,
110
- rootValue: firstRequest.rootValue
124
+ rootValue,
125
+ operationName
111
126
  };
112
127
  }
113
128
  function prefixRequest(prefix, request) {
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { getOperationASTFromRequest, relocatedError, memoize2of4 } from '@graphql-tools/utils';
1
+ import { relocatedError, getOperationASTFromRequest, memoize2of4 } from '@graphql-tools/utils';
2
2
  import { fakePromise } from '@whatwg-node/promise-helpers';
3
3
  import DataLoader from 'dataloader';
4
4
  import { Kind, visit } from 'graphql';
@@ -41,22 +41,40 @@ function parseKeyFromPath(path) {
41
41
  }
42
42
 
43
43
  function mergeRequests(requests, extensionsReducer) {
44
- if (requests.length === 1) {
45
- return requests[0];
44
+ const firstRequest = requests[0];
45
+ if (!firstRequest) {
46
+ throw new Error("At least one request is required");
47
+ }
48
+ const requestCount = requests.length;
49
+ if (requestCount === 1) {
50
+ return firstRequest;
46
51
  }
47
- const subgraphName = requests[0].subgraphName;
48
52
  const mergedVariables = /* @__PURE__ */ Object.create(null);
49
53
  const mergedVariableDefinitions = [];
50
54
  const mergedSelections = [];
51
55
  const mergedFragmentDefinitions = [];
52
56
  let mergedExtensions = /* @__PURE__ */ Object.create(null);
53
- for (let index = 0; index < requests.length; index++) {
57
+ let subgraphName;
58
+ let operationName;
59
+ let operationType;
60
+ let context;
61
+ let info;
62
+ let rootValue;
63
+ for (let index = 0; index < requestCount; index++) {
54
64
  const request = requests[index];
55
65
  if (request) {
66
+ subgraphName ??= request.subgraphName;
67
+ operationName ??= request.operationName;
68
+ operationType ??= request.operationType;
69
+ context ??= request.context;
70
+ info ??= request.info;
71
+ rootValue ??= request.rootValue;
56
72
  const prefixedRequests = prefixRequest(createPrefix(index), request);
57
73
  for (const def of prefixedRequests.document.definitions) {
58
74
  if (isOperationDefinition(def)) {
59
75
  mergedSelections.push(...def.selectionSet.selections);
76
+ operationType ||= def.operation;
77
+ operationName ||= def.name?.value;
60
78
  if (def.variableDefinitions) {
61
79
  mergedVariableDefinitions.push(...def.variableDefinitions);
62
80
  }
@@ -69,11 +87,10 @@ function mergeRequests(requests, extensionsReducer) {
69
87
  mergedExtensions = extensionsReducer(mergedExtensions, request);
70
88
  }
71
89
  }
72
- const firstRequest = requests[0];
73
- if (!firstRequest) {
74
- throw new Error("At least one request is required");
90
+ if (operationName == null) {
91
+ operationName = info?.operation?.name?.value;
75
92
  }
76
- const operationType = firstRequest.operationType ?? getOperationASTFromRequest(firstRequest).operation;
93
+ operationType ||= "query";
77
94
  const mergedOperationDefinition = {
78
95
  kind: Kind.OPERATION_DEFINITION,
79
96
  operation: operationType,
@@ -81,15 +98,12 @@ function mergeRequests(requests, extensionsReducer) {
81
98
  selectionSet: {
82
99
  kind: Kind.SELECTION_SET,
83
100
  selections: mergedSelections
84
- }
85
- };
86
- const operationName = firstRequest.operationName ?? firstRequest.info?.operation?.name?.value;
87
- if (operationName) {
88
- mergedOperationDefinition.name = {
101
+ },
102
+ name: operationName ? {
89
103
  kind: Kind.NAME,
90
104
  value: operationName
91
- };
92
- }
105
+ } : void 0
106
+ };
93
107
  return {
94
108
  subgraphName,
95
109
  document: {
@@ -98,10 +112,11 @@ function mergeRequests(requests, extensionsReducer) {
98
112
  },
99
113
  variables: mergedVariables,
100
114
  extensions: mergedExtensions,
101
- context: firstRequest.context,
102
- info: firstRequest.info,
115
+ context,
116
+ info,
103
117
  operationType,
104
- rootValue: firstRequest.rootValue
118
+ rootValue,
119
+ operationName
105
120
  };
106
121
  }
107
122
  function prefixRequest(prefix, request) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphql-tools/batch-execute",
3
- "version": "10.0.7",
3
+ "version": "10.0.8-alpha-30e125d6da4fc2fb20c6a00f6979646b1bea19f6",
4
4
  "type": "module",
5
5
  "description": "A set of utils for faster development of GraphQL tools",
6
6
  "repository": {