relay-compiler 9.0.0 → 10.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. package/bin/RelayCompilerBin.js.flow +169 -0
  2. package/bin/RelayCompilerMain.js.flow +515 -0
  3. package/bin/__fixtures__/plugin-module.js.flow +17 -0
  4. package/bin/relay-compiler +3862 -2505
  5. package/codegen/CodegenDirectory.js.flow +375 -0
  6. package/codegen/CodegenRunner.js.flow +432 -0
  7. package/codegen/CodegenTypes.js.flow +28 -0
  8. package/codegen/CodegenWatcher.js.flow +254 -0
  9. package/codegen/NormalizationCodeGenerator.js.flow +571 -0
  10. package/codegen/ReaderCodeGenerator.js.flow +512 -0
  11. package/codegen/RelayCodeGenerator.js.flow +85 -0
  12. package/codegen/RelayFileWriter.js.flow +367 -0
  13. package/codegen/SourceControl.js.flow +58 -0
  14. package/codegen/compileRelayArtifacts.js.flow +182 -0
  15. package/codegen/createPrintRequireModuleDependency.js.flow +21 -0
  16. package/codegen/sortObjectByKey.js.flow +25 -0
  17. package/codegen/writeRelayGeneratedFile.js.flow +223 -0
  18. package/core/ASTCache.js.flow +74 -0
  19. package/core/ASTConvert.js.flow +233 -0
  20. package/core/CompilerContext.js.flow +191 -0
  21. package/core/CompilerError.js.flow +250 -0
  22. package/core/DotGraphQLParser.js.flow +39 -0
  23. package/core/GraphQLCompilerProfiler.js.flow +341 -0
  24. package/core/GraphQLDerivedFromMetadata.js.flow +36 -0
  25. package/core/GraphQLWatchmanClient.js.flow +111 -0
  26. package/core/IR.js.flow +326 -0
  27. package/core/IRPrinter.js.flow +477 -0
  28. package/core/IRTransformer.js.flow +377 -0
  29. package/core/IRValidator.js.flow +260 -0
  30. package/core/IRVisitor.js.flow +150 -0
  31. package/core/JSModuleParser.js.flow +24 -0
  32. package/core/RelayCompilerScope.js.flow +199 -0
  33. package/core/RelayFindGraphQLTags.js.flow +119 -0
  34. package/core/RelayGraphQLEnumsGenerator.js.flow +55 -0
  35. package/core/RelayIRTransforms.js.flow +138 -0
  36. package/core/RelayParser.js.flow +1731 -0
  37. package/core/RelaySourceModuleParser.js.flow +135 -0
  38. package/core/Schema.js.flow +2037 -0
  39. package/core/SchemaUtils.js.flow +120 -0
  40. package/core/filterContextForNode.js.flow +50 -0
  41. package/core/getFieldDefinition.js.flow +156 -0
  42. package/core/getIdentifierForArgumentValue.js.flow +49 -0
  43. package/core/getIdentifierForSelection.js.flow +69 -0
  44. package/core/getLiteralArgumentValues.js.flow +32 -0
  45. package/core/getNormalizationOperationName.js.flow +19 -0
  46. package/core/inferRootArgumentDefinitions.js.flow +323 -0
  47. package/index.js +1 -1
  48. package/index.js.flow +200 -0
  49. package/language/RelayLanguagePluginInterface.js.flow +283 -0
  50. package/language/javascript/FindGraphQLTags.js.flow +137 -0
  51. package/language/javascript/RelayFlowBabelFactories.js.flow +176 -0
  52. package/language/javascript/RelayFlowGenerator.js.flow +1099 -0
  53. package/language/javascript/RelayFlowTypeTransformers.js.flow +184 -0
  54. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +34 -0
  55. package/language/javascript/formatGeneratedModule.js.flow +65 -0
  56. package/lib/bin/RelayCompilerBin.js +10 -0
  57. package/lib/bin/RelayCompilerMain.js +127 -130
  58. package/lib/codegen/CodegenDirectory.js +2 -6
  59. package/lib/codegen/CodegenRunner.js +37 -76
  60. package/lib/codegen/CodegenWatcher.js +13 -21
  61. package/lib/codegen/NormalizationCodeGenerator.js +131 -50
  62. package/lib/codegen/ReaderCodeGenerator.js +116 -49
  63. package/lib/codegen/RelayCodeGenerator.js +17 -6
  64. package/lib/codegen/RelayFileWriter.js +15 -37
  65. package/lib/codegen/compileRelayArtifacts.js +16 -30
  66. package/lib/codegen/sortObjectByKey.js +43 -0
  67. package/lib/codegen/writeRelayGeneratedFile.js +86 -96
  68. package/lib/core/ASTCache.js +3 -4
  69. package/lib/core/CompilerContext.js +3 -4
  70. package/lib/core/CompilerError.js +27 -54
  71. package/lib/core/GraphQLCompilerProfiler.js +8 -12
  72. package/lib/core/GraphQLDerivedFromMetadata.js +1 -10
  73. package/lib/core/GraphQLWatchmanClient.js +4 -12
  74. package/lib/core/IRPrinter.js +16 -21
  75. package/lib/core/IRTransformer.js +8 -6
  76. package/lib/core/IRValidator.js +1 -3
  77. package/lib/core/RelayCompilerScope.js +4 -4
  78. package/lib/core/RelayGraphQLEnumsGenerator.js +12 -15
  79. package/lib/core/RelayIRTransforms.js +23 -13
  80. package/lib/core/RelayParser.js +53 -89
  81. package/lib/core/RelaySourceModuleParser.js +1 -3
  82. package/lib/core/Schema.js +106 -77
  83. package/lib/core/SchemaUtils.js +15 -1
  84. package/lib/core/getFieldDefinition.js +12 -15
  85. package/lib/core/getIdentifierForSelection.js +1 -1
  86. package/lib/core/inferRootArgumentDefinitions.js +27 -36
  87. package/lib/index.js +1 -3
  88. package/lib/language/javascript/FindGraphQLTags.js +7 -72
  89. package/lib/language/javascript/RelayFlowBabelFactories.js +5 -5
  90. package/lib/language/javascript/RelayFlowGenerator.js +131 -108
  91. package/lib/language/javascript/RelayFlowTypeTransformers.js +1 -3
  92. package/lib/reporters/ConsoleReporter.js +1 -3
  93. package/lib/reporters/MultiReporter.js +1 -3
  94. package/lib/runner/Artifacts.js +69 -170
  95. package/lib/runner/BufferedFilesystem.js +32 -66
  96. package/lib/runner/GraphQLASTNodeGroup.js +54 -120
  97. package/lib/runner/GraphQLNodeMap.js +14 -19
  98. package/lib/runner/Sources.js +70 -85
  99. package/lib/runner/StrictMap.js +21 -37
  100. package/lib/runner/getChangedNodeNames.js +30 -62
  101. package/lib/transforms/ApplyFragmentArgumentTransform.js +71 -31
  102. package/lib/transforms/ClientExtensionsTransform.js +15 -15
  103. package/lib/transforms/ConnectionTransform.js +26 -38
  104. package/lib/transforms/DeclarativeConnectionMutationTransform.js +225 -0
  105. package/lib/transforms/DeferStreamTransform.js +27 -17
  106. package/lib/transforms/DisallowTypenameOnRoot.js +55 -0
  107. package/lib/transforms/FieldHandleTransform.js +7 -3
  108. package/lib/transforms/FilterCompilerDirectivesTransform.js +29 -0
  109. package/lib/transforms/FlattenTransform.js +23 -19
  110. package/lib/transforms/GenerateIDFieldTransform.js +56 -35
  111. package/lib/transforms/GenerateTypeNameTransform.js +84 -10
  112. package/lib/transforms/InlineDataFragmentTransform.js +9 -4
  113. package/lib/transforms/MaskTransform.js +17 -17
  114. package/lib/transforms/MatchTransform.js +114 -32
  115. package/lib/transforms/ReactFlightComponentTransform.js +162 -0
  116. package/lib/transforms/RefetchableFragmentTransform.js +21 -17
  117. package/lib/transforms/RelayDirectiveTransform.js +8 -3
  118. package/lib/transforms/RequiredFieldTransform.js +380 -0
  119. package/lib/transforms/SkipClientExtensionsTransform.js +8 -0
  120. package/lib/transforms/SkipHandleFieldTransform.js +6 -2
  121. package/lib/transforms/SkipRedundantNodesTransform.js +9 -2
  122. package/lib/transforms/SkipSplitOperationTransform.js +32 -0
  123. package/lib/transforms/SkipUnreachableNodeTransform.js +9 -2
  124. package/lib/transforms/SkipUnusedVariablesTransform.js +18 -17
  125. package/lib/transforms/SplitModuleImportTransform.js +2 -2
  126. package/lib/transforms/TestOperationTransform.js +26 -20
  127. package/lib/transforms/ValidateGlobalVariablesTransform.js +18 -30
  128. package/lib/transforms/ValidateRequiredArgumentsTransform.js +12 -15
  129. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +16 -30
  130. package/lib/transforms/ValidateUnusedVariablesTransform.js +18 -30
  131. package/lib/transforms/query-generators/FetchableQueryGenerator.js +161 -0
  132. package/lib/transforms/query-generators/NodeQueryGenerator.js +22 -3
  133. package/lib/transforms/query-generators/QueryQueryGenerator.js +2 -1
  134. package/lib/transforms/query-generators/ViewerQueryGenerator.js +1 -0
  135. package/lib/transforms/query-generators/index.js +23 -6
  136. package/lib/transforms/query-generators/utils.js +17 -16
  137. package/lib/util/RelayCompilerCache.js +2 -4
  138. package/lib/util/argumentContainsVariables.js +37 -0
  139. package/lib/util/dedupeJSONStringify.js +15 -12
  140. package/lib/util/generateAbstractTypeRefinementKey.js +24 -0
  141. package/lib/util/getModuleName.js +1 -1
  142. package/lib/util/joinArgumentDefinitions.js +3 -1
  143. package/package.json +7 -7
  144. package/relay-compiler.js +4 -4
  145. package/relay-compiler.min.js +4 -4
  146. package/reporters/ConsoleReporter.js.flow +81 -0
  147. package/reporters/MultiReporter.js.flow +43 -0
  148. package/reporters/Reporter.js.flow +19 -0
  149. package/runner/Artifacts.js.flow +219 -0
  150. package/runner/BufferedFilesystem.js.flow +194 -0
  151. package/runner/GraphQLASTNodeGroup.js.flow +176 -0
  152. package/runner/GraphQLASTUtils.js.flow +26 -0
  153. package/runner/GraphQLNodeMap.js.flow +55 -0
  154. package/runner/Sources.js.flow +228 -0
  155. package/runner/StrictMap.js.flow +96 -0
  156. package/runner/compileArtifacts.js.flow +76 -0
  157. package/runner/extractAST.js.flow +100 -0
  158. package/runner/getChangedNodeNames.js.flow +48 -0
  159. package/runner/getSchemaInstance.js.flow +36 -0
  160. package/runner/types.js.flow +37 -0
  161. package/transforms/ApplyFragmentArgumentTransform.js.flow +526 -0
  162. package/transforms/ClientExtensionsTransform.js.flow +224 -0
  163. package/transforms/ConnectionTransform.js.flow +855 -0
  164. package/transforms/DeclarativeConnectionMutationTransform.js.flow +246 -0
  165. package/transforms/DeferStreamTransform.js.flow +265 -0
  166. package/transforms/DisallowIdAsAlias.js.flow +47 -0
  167. package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
  168. package/transforms/FieldHandleTransform.js.flow +79 -0
  169. package/transforms/FilterCompilerDirectivesTransform.js.flow +33 -0
  170. package/transforms/FilterDirectivesTransform.js.flow +45 -0
  171. package/transforms/FlattenTransform.js.flow +454 -0
  172. package/transforms/GenerateIDFieldTransform.js.flow +152 -0
  173. package/transforms/GenerateTypeNameTransform.js.flow +161 -0
  174. package/transforms/InlineDataFragmentTransform.js.flow +125 -0
  175. package/transforms/InlineFragmentsTransform.js.flow +71 -0
  176. package/transforms/MaskTransform.js.flow +126 -0
  177. package/transforms/MatchTransform.js.flow +589 -0
  178. package/transforms/ReactFlightComponentTransform.js.flow +195 -0
  179. package/transforms/RefetchableFragmentTransform.js.flow +272 -0
  180. package/transforms/RelayDirectiveTransform.js.flow +97 -0
  181. package/transforms/RequiredFieldTransform.js.flow +415 -0
  182. package/transforms/SkipClientExtensionsTransform.js.flow +54 -0
  183. package/transforms/SkipHandleFieldTransform.js.flow +44 -0
  184. package/transforms/SkipRedundantNodesTransform.js.flow +257 -0
  185. package/transforms/SkipSplitOperationTransform.js.flow +37 -0
  186. package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
  187. package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
  188. package/transforms/SplitModuleImportTransform.js.flow +98 -0
  189. package/transforms/TestOperationTransform.js.flow +142 -0
  190. package/transforms/TransformUtils.js.flow +26 -0
  191. package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
  192. package/transforms/ValidateRequiredArgumentsTransform.js.flow +127 -0
  193. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +112 -0
  194. package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
  195. package/transforms/query-generators/FetchableQueryGenerator.js.flow +189 -0
  196. package/transforms/query-generators/NodeQueryGenerator.js.flow +219 -0
  197. package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
  198. package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
  199. package/transforms/query-generators/index.js.flow +90 -0
  200. package/transforms/query-generators/utils.js.flow +76 -0
  201. package/util/CodeMarker.js.flow +79 -0
  202. package/util/DefaultHandleKey.js.flow +17 -0
  203. package/util/RelayCompilerCache.js.flow +88 -0
  204. package/util/Rollout.js.flow +39 -0
  205. package/util/TimeReporter.js.flow +79 -0
  206. package/util/areEqualOSS.js.flow +123 -0
  207. package/util/argumentContainsVariables.js.flow +38 -0
  208. package/util/dedupeJSONStringify.js.flow +152 -0
  209. package/util/generateAbstractTypeRefinementKey.js.flow +29 -0
  210. package/util/getDefinitionNodeHash.js.flow +25 -0
  211. package/util/getModuleName.js.flow +39 -0
  212. package/util/joinArgumentDefinitions.js.flow +105 -0
  213. package/util/md5.js.flow +22 -0
  214. package/util/murmurHash.js.flow +94 -0
  215. package/util/nullthrowsOSS.js.flow +25 -0
  216. package/util/orList.js.flow +37 -0
  217. package/util/partitionArray.js.flow +37 -0
@@ -0,0 +1,589 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow strict-local
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const IRTransformer = require('../core/IRTransformer');
16
+
17
+ const getLiteralArgumentValues = require('../core/getLiteralArgumentValues');
18
+ const getNormalizationOperationName = require('../core/getNormalizationOperationName');
19
+
20
+ const {createCompilerError, createUserError} = require('../core/CompilerError');
21
+ const {getModuleComponentKey, getModuleOperationKey} = require('relay-runtime');
22
+
23
+ import type CompilerContext from '../core/CompilerContext';
24
+ import type {
25
+ InlineFragment,
26
+ FragmentSpread,
27
+ LinkedField,
28
+ Location,
29
+ ScalarField,
30
+ } from '../core/IR';
31
+ import type {TypeID} from '../core/Schema';
32
+
33
+ const SUPPORTED_ARGUMENT_NAME = 'supported';
34
+
35
+ const JS_FIELD_TYPE = 'JSDependency';
36
+ const JS_FIELD_MODULE_ARG = 'module';
37
+ const JS_FIELD_ID_ARG = 'id';
38
+ const JS_FIELD_NAME = 'js';
39
+
40
+ const SCHEMA_EXTENSION = `
41
+ directive @match(key: String) on FIELD
42
+
43
+ directive @module(
44
+ name: String!
45
+ ) on FRAGMENT_SPREAD
46
+ `;
47
+
48
+ type State = {|
49
+ +documentName: string,
50
+ +parentType: TypeID,
51
+ +path: Array<LinkedField>,
52
+ +moduleKey: ?string,
53
+ +matchesForPath: Map<
54
+ string,
55
+ {|
56
+ +key: string,
57
+ +location: Location,
58
+ +types: Map<
59
+ string,
60
+ {|+location: Location, +fragment: string, +module: string|},
61
+ >,
62
+ |},
63
+ >,
64
+ |};
65
+
66
+ /**
67
+ * This transform rewrites LinkedField nodes with @match and rewrites them
68
+ * into `LinkedField` nodes with a `supported` argument.
69
+ */
70
+ function matchTransform(context: CompilerContext): CompilerContext {
71
+ return IRTransformer.transform(
72
+ context,
73
+ {
74
+ // TODO: type IRTransformer to allow changing result type
75
+ FragmentSpread: (visitFragmentSpread: $FlowFixMe),
76
+ LinkedField: visitLinkedField,
77
+ InlineFragment: visitInlineFragment,
78
+ ScalarField: visitScalarField,
79
+ },
80
+ node => ({
81
+ documentName: node.name,
82
+ matchesForPath: new Map(),
83
+ moduleKey: null,
84
+ parentType: node.type,
85
+ path: [],
86
+ }),
87
+ );
88
+ }
89
+
90
+ function visitInlineFragment(
91
+ node: InlineFragment,
92
+ state: State,
93
+ ): InlineFragment {
94
+ return this.traverse(node, {
95
+ ...state,
96
+ parentType: node.typeCondition,
97
+ });
98
+ }
99
+
100
+ function visitScalarField(field: ScalarField): ScalarField {
101
+ const context: CompilerContext = this.getContext();
102
+ const schema = context.getSchema();
103
+
104
+ if (field.name === JS_FIELD_NAME) {
105
+ const jsModuleType = schema.getTypeFromString(JS_FIELD_TYPE);
106
+ if (jsModuleType == null || !schema.isServerType(jsModuleType)) {
107
+ throw new createUserError(
108
+ `'${JS_FIELD_NAME}' should be defined on the server schema.`,
109
+ [field.loc],
110
+ );
111
+ }
112
+
113
+ if (
114
+ schema.isScalar(jsModuleType) &&
115
+ schema.areEqualTypes(schema.getRawType(field.type), jsModuleType)
116
+ ) {
117
+ throw new createUserError(
118
+ `Direct use of the '${JS_FIELD_NAME}' field is not allowed, use ` +
119
+ '@match/@module instead.',
120
+ [field.loc],
121
+ );
122
+ }
123
+ }
124
+ return field;
125
+ }
126
+
127
+ function visitLinkedField(node: LinkedField, state: State): LinkedField {
128
+ const context: CompilerContext = this.getContext();
129
+ const schema = context.getSchema();
130
+
131
+ const matchDirective = node.directives.find(
132
+ directive => directive.name === 'match',
133
+ );
134
+ let moduleKey = null;
135
+ if (matchDirective != null) {
136
+ ({key: moduleKey} = getLiteralArgumentValues(matchDirective.args));
137
+ if (
138
+ moduleKey != null &&
139
+ (typeof moduleKey !== 'string' ||
140
+ !moduleKey.startsWith(state.documentName))
141
+ ) {
142
+ throw createUserError(
143
+ "Expected the 'key' argument of @match to be a literal string starting " +
144
+ `with the document name, e.g. '${state.documentName}_<localName>'.`,
145
+ [
146
+ (
147
+ matchDirective.args.find(arg => arg.name === 'key') ??
148
+ matchDirective
149
+ ).loc,
150
+ ],
151
+ );
152
+ }
153
+ }
154
+
155
+ state.path.push(node);
156
+ const transformedNode: LinkedField = this.traverse(node, {
157
+ ...state,
158
+ moduleKey,
159
+ parentType: node.type,
160
+ });
161
+ state.path.pop();
162
+
163
+ if (matchDirective == null) {
164
+ return transformedNode;
165
+ }
166
+
167
+ const {parentType} = state;
168
+ const rawType = schema.getRawType(parentType);
169
+ if (!(schema.isInterface(rawType) || schema.isObject(rawType))) {
170
+ throw createUserError(
171
+ `@match used on incompatible field '${transformedNode.name}'.` +
172
+ '@match may only be used with fields whose parent type is an ' +
173
+ `interface or object, got invalid type '${schema.getTypeString(
174
+ parentType,
175
+ )}'.`,
176
+ [node.loc],
177
+ );
178
+ }
179
+
180
+ const currentField = schema.getFieldConfig(
181
+ schema.expectField(
182
+ schema.assertCompositeType(rawType),
183
+ transformedNode.name,
184
+ ),
185
+ );
186
+
187
+ const supportedArgumentDefinition = currentField.args.find(
188
+ ({name}) => name === SUPPORTED_ARGUMENT_NAME,
189
+ );
190
+ if (supportedArgumentDefinition == null) {
191
+ if (moduleKey == null) {
192
+ throw createUserError(
193
+ '@match on a field without the `supported` argument is a no-op, please remove the `@match`.',
194
+ [node.loc],
195
+ );
196
+ }
197
+ return transformedNode;
198
+ }
199
+
200
+ const supportedArgType = schema.getNullableType(
201
+ supportedArgumentDefinition.type,
202
+ );
203
+ const supportedArgOfType =
204
+ supportedArgType != null && schema.isList(supportedArgType)
205
+ ? schema.getListItemType(supportedArgType)
206
+ : null;
207
+ if (
208
+ supportedArgType == null ||
209
+ supportedArgOfType == null ||
210
+ !schema.isString(schema.getNullableType(supportedArgOfType))
211
+ ) {
212
+ throw createUserError(
213
+ `@match used on incompatible field '${transformedNode.name}'. ` +
214
+ '@match may only be used with fields that accept a ' +
215
+ "'supported: [String!]!' argument.",
216
+ [node.loc],
217
+ );
218
+ }
219
+
220
+ const rawFieldType = schema.getRawType(transformedNode.type);
221
+
222
+ if (!schema.isAbstractType(rawFieldType)) {
223
+ throw createUserError(
224
+ `@match used on incompatible field '${transformedNode.name}'.` +
225
+ '@match may only be used with fields that return a union or interface.',
226
+ [node.loc],
227
+ );
228
+ }
229
+
230
+ const seenTypes: Map<TypeID, InlineFragment> = new Map();
231
+ const selections = [];
232
+ transformedNode.selections.forEach(matchSelection => {
233
+ if (
234
+ matchSelection.kind === 'ScalarField' &&
235
+ matchSelection.name === '__typename'
236
+ ) {
237
+ selections.push(matchSelection);
238
+ return;
239
+ }
240
+ const moduleImport =
241
+ matchSelection.kind === 'InlineFragment'
242
+ ? matchSelection.selections[0]
243
+ : null;
244
+ if (
245
+ matchSelection.kind !== 'InlineFragment' ||
246
+ moduleImport == null ||
247
+ moduleImport.kind !== 'ModuleImport'
248
+ ) {
249
+ throw createUserError(
250
+ 'Invalid @match selection: all selections should be ' +
251
+ 'fragment spreads with @module.',
252
+ [matchSelection.loc],
253
+ );
254
+ }
255
+ const matchedType = matchSelection.typeCondition;
256
+ seenTypes.set(matchedType, matchSelection);
257
+ selections.push(matchSelection);
258
+ });
259
+
260
+ if (seenTypes.size === 0) {
261
+ throw createUserError(
262
+ 'Invalid @match selection: expected at least one @module selection. ' +
263
+ "Remove @match or add a '...Fragment @module()' selection.",
264
+ [matchDirective.loc],
265
+ );
266
+ }
267
+
268
+ const supportedArg = transformedNode.args.find(
269
+ arg => arg.name === SUPPORTED_ARGUMENT_NAME,
270
+ );
271
+ if (supportedArg != null) {
272
+ throw createUserError(
273
+ `Invalid @match selection: the '${SUPPORTED_ARGUMENT_NAME}' argument ` +
274
+ 'is automatically added and cannot be supplied explicitly.',
275
+ [supportedArg.loc],
276
+ );
277
+ }
278
+
279
+ return {
280
+ kind: 'LinkedField',
281
+ alias: transformedNode.alias,
282
+ args: [
283
+ ...transformedNode.args,
284
+ {
285
+ kind: 'Argument',
286
+ name: SUPPORTED_ARGUMENT_NAME,
287
+ type: supportedArgumentDefinition.type,
288
+ value: {
289
+ kind: 'Literal',
290
+ loc: node.loc,
291
+ value: Array.from(seenTypes.keys()).map(type =>
292
+ schema.getTypeString(type),
293
+ ),
294
+ },
295
+ loc: node.loc,
296
+ },
297
+ ],
298
+ connection: false,
299
+ directives: [],
300
+ handles: null,
301
+ loc: node.loc,
302
+ metadata: null,
303
+ name: transformedNode.name,
304
+ type: transformedNode.type,
305
+ selections,
306
+ };
307
+ }
308
+
309
+ // Transform @module
310
+ function visitFragmentSpread(
311
+ spread: FragmentSpread,
312
+ {documentName, path, matchesForPath, moduleKey: moduleKeyFromParent}: State,
313
+ ): FragmentSpread | InlineFragment {
314
+ const transformedNode: FragmentSpread = this.traverse(spread);
315
+
316
+ const moduleDirective = transformedNode.directives.find(
317
+ directive => directive.name === 'module',
318
+ );
319
+ if (moduleDirective == null) {
320
+ return transformedNode;
321
+ }
322
+ if (spread.args.length !== 0) {
323
+ throw createUserError(
324
+ '@module does not support @arguments.',
325
+ [spread.args[0]?.loc].filter(Boolean),
326
+ );
327
+ }
328
+
329
+ const context: CompilerContext = this.getContext();
330
+ const schema = context.getSchema();
331
+
332
+ const jsModuleType = schema.asScalarFieldType(
333
+ schema.getTypeFromString(JS_FIELD_TYPE),
334
+ );
335
+ if (jsModuleType == null || !schema.isServerType(jsModuleType)) {
336
+ throw new createUserError(
337
+ `'${JS_FIELD_NAME}' should be defined on the server schema.`,
338
+ [spread.loc],
339
+ );
340
+ }
341
+
342
+ if (!schema.isScalar(jsModuleType)) {
343
+ throw createUserError(
344
+ 'Using @module requires the schema to define a scalar ' +
345
+ `'${JS_FIELD_TYPE}' type.`,
346
+ );
347
+ }
348
+
349
+ const fragment = context.getFragment(spread.name, spread.loc);
350
+ if (!schema.isObject(fragment.type)) {
351
+ throw createUserError(
352
+ `@module used on invalid fragment spread '...${spread.name}'. @module ` +
353
+ 'may only be used with fragments on a concrete (object) type, ' +
354
+ `but the fragment has abstract type '${schema.getTypeString(
355
+ fragment.type,
356
+ )}'.`,
357
+ [spread.loc, fragment.loc],
358
+ );
359
+ }
360
+ const field = schema.getFieldByName(fragment.type, JS_FIELD_NAME);
361
+ if (!field) {
362
+ throw createUserError(
363
+ `@module used on invalid fragment spread '...${spread.name}'. @module ` +
364
+ `requires the fragment type '${schema.getTypeString(
365
+ fragment.type,
366
+ )}' to have a ` +
367
+ `'${JS_FIELD_NAME}(${JS_FIELD_MODULE_ARG}: String! ` +
368
+ `[${JS_FIELD_ID_ARG}: String]): ${JS_FIELD_TYPE}' field (your ` +
369
+ "schema may choose to omit the 'id' argument but if present it " +
370
+ "must accept a 'String').",
371
+ [moduleDirective.loc],
372
+ );
373
+ }
374
+ const jsField = schema.getFieldConfig(field);
375
+
376
+ const jsFieldModuleArg = jsField
377
+ ? jsField.args.find(arg => arg.name === JS_FIELD_MODULE_ARG)
378
+ : null;
379
+ const jsFieldIdArg = jsField
380
+ ? jsField.args.find(arg => arg.name === JS_FIELD_ID_ARG)
381
+ : null;
382
+ if (
383
+ jsFieldModuleArg == null ||
384
+ !schema.isString(schema.getNullableType(jsFieldModuleArg.type)) ||
385
+ (jsFieldIdArg != null && !schema.isString(jsFieldIdArg.type)) ||
386
+ jsField.type !== jsModuleType
387
+ ) {
388
+ throw createUserError(
389
+ `@module used on invalid fragment spread '...${spread.name}'. @module ` +
390
+ `requires the fragment type '${schema.getTypeString(
391
+ fragment.type,
392
+ )}' to have a ` +
393
+ `'${JS_FIELD_NAME}(${JS_FIELD_MODULE_ARG}: String! ` +
394
+ `[${JS_FIELD_ID_ARG}: String]): ${JS_FIELD_TYPE}' field (your ` +
395
+ "schema may choose to omit the 'id' argument but if present it " +
396
+ "must accept a 'String').",
397
+ [moduleDirective.loc],
398
+ );
399
+ }
400
+
401
+ if (spread.directives.length !== 1) {
402
+ throw createUserError(
403
+ `@module used on invalid fragment spread '...${spread.name}'. @module ` +
404
+ 'may not have additional directives.',
405
+ [spread.loc],
406
+ );
407
+ }
408
+ const {name: moduleName} = getLiteralArgumentValues(moduleDirective.args);
409
+ if (typeof moduleName !== 'string') {
410
+ throw createUserError(
411
+ "Expected the 'name' argument of @module to be a literal string",
412
+ [(moduleDirective.args.find(arg => arg.name === 'name') ?? spread).loc],
413
+ );
414
+ }
415
+ const parentField = path[path.length - 1];
416
+ const moduleKey = moduleKeyFromParent ?? documentName;
417
+ const aliasPath = path.map(x => x.alias).join('.');
418
+ const moduleId =
419
+ aliasPath === '' ? documentName : `${documentName}.${aliasPath}`;
420
+ const typeName = schema.getTypeString(fragment.type);
421
+
422
+ let matches = matchesForPath.get(aliasPath);
423
+ if (matches == null) {
424
+ if (matchesForPath.size !== 0) {
425
+ const existingMatchWithKey = Array.from(matchesForPath.values()).find(
426
+ entry => entry.key === moduleKey,
427
+ );
428
+ if (existingMatchWithKey != null) {
429
+ if (parentField == null) {
430
+ throw createCompilerError(
431
+ 'Cannot have @module selections at multiple paths unless the selections are within fields.',
432
+ [spread.loc],
433
+ );
434
+ }
435
+ throw createUserError(
436
+ 'Invalid @module selection: documents with multiple fields ' +
437
+ "containing 3D selections must specify a unique 'key' value " +
438
+ `for each field: use '${parentField.alias} @match(key: "${documentName}_<localName>")'.`,
439
+ [parentField.loc],
440
+ );
441
+ }
442
+ }
443
+
444
+ matches = {
445
+ key: moduleKey,
446
+ location: parentField?.loc ?? spread.loc,
447
+ types: new Map(),
448
+ };
449
+ matchesForPath.set(aliasPath, matches);
450
+ }
451
+ if (moduleKey !== matches.key) {
452
+ // The user can't override the key locally (per @module),
453
+ // so this is just an internal sanity check
454
+ throw createCompilerError(
455
+ 'Invalid @module selection: expected all selections at path ' +
456
+ `'${aliasPath} to have the same 'key', got '${moduleKey}' and '${matches.key}'.`,
457
+ [parentField?.loc ?? spread.loc],
458
+ );
459
+ }
460
+ const previousMatchForType = matches.types.get(typeName);
461
+ if (
462
+ previousMatchForType != null &&
463
+ (previousMatchForType.fragment !== spread.name ||
464
+ previousMatchForType.module !== moduleName)
465
+ ) {
466
+ throw createUserError(
467
+ 'Invalid @module selection: concrete type ' +
468
+ `'${typeName}' was matched multiple times at path ` +
469
+ `'${aliasPath}' but with a different fragment or module name.`,
470
+ [spread.loc, previousMatchForType.location],
471
+ );
472
+ }
473
+ matches.types.set(typeName, {
474
+ location: spread.loc,
475
+ fragment: spread.name,
476
+ module: moduleName,
477
+ });
478
+
479
+ const normalizationName =
480
+ getNormalizationOperationName(spread.name) + '.graphql';
481
+ const componentKey = getModuleComponentKey(moduleKey);
482
+ const componentField: ScalarField = {
483
+ alias: componentKey,
484
+ args: [
485
+ {
486
+ kind: 'Argument',
487
+ name: JS_FIELD_MODULE_ARG,
488
+ type: jsFieldModuleArg.type,
489
+ value: {
490
+ kind: 'Literal',
491
+ loc: moduleDirective.args[0]?.loc ?? moduleDirective.loc,
492
+ value: moduleName,
493
+ },
494
+ loc: moduleDirective.loc,
495
+ },
496
+ jsFieldIdArg != null
497
+ ? {
498
+ kind: 'Argument',
499
+ name: JS_FIELD_ID_ARG,
500
+ type: jsFieldIdArg.type,
501
+ value: {
502
+ kind: 'Literal',
503
+ loc: moduleDirective.args[0]?.loc ?? moduleDirective.loc,
504
+ value: moduleId,
505
+ },
506
+ loc: moduleDirective.loc,
507
+ }
508
+ : null,
509
+ ].filter(Boolean),
510
+ directives: [],
511
+ handles: null,
512
+ kind: 'ScalarField',
513
+ loc: moduleDirective.loc,
514
+ metadata: {skipNormalizationNode: true},
515
+ name: JS_FIELD_NAME,
516
+ type: jsModuleType,
517
+ };
518
+ const operationKey = getModuleOperationKey(moduleKey);
519
+ const operationField: ScalarField = {
520
+ alias: operationKey,
521
+ args: [
522
+ {
523
+ kind: 'Argument',
524
+ name: JS_FIELD_MODULE_ARG,
525
+ type: jsFieldModuleArg.type,
526
+ value: {
527
+ kind: 'Literal',
528
+ loc: moduleDirective.loc,
529
+ value: normalizationName,
530
+ },
531
+ loc: moduleDirective.loc,
532
+ },
533
+ jsFieldIdArg != null
534
+ ? {
535
+ kind: 'Argument',
536
+ name: JS_FIELD_ID_ARG,
537
+ type: jsFieldIdArg.type,
538
+ value: {
539
+ kind: 'Literal',
540
+ loc: moduleDirective.args[0]?.loc ?? moduleDirective.loc,
541
+ value: moduleId,
542
+ },
543
+ loc: moduleDirective.loc,
544
+ }
545
+ : null,
546
+ ].filter(Boolean),
547
+ directives: [],
548
+ handles: null,
549
+ kind: 'ScalarField',
550
+ loc: moduleDirective.loc,
551
+ metadata: {skipNormalizationNode: true},
552
+ name: JS_FIELD_NAME,
553
+ type: jsModuleType,
554
+ };
555
+
556
+ return {
557
+ kind: 'InlineFragment',
558
+ directives: [],
559
+ loc: moduleDirective.loc,
560
+ metadata: null,
561
+ selections: [
562
+ {
563
+ kind: 'ModuleImport',
564
+ loc: moduleDirective.loc,
565
+ key: moduleKey,
566
+ id: moduleId,
567
+ module: moduleName,
568
+ sourceDocument: documentName,
569
+ name: spread.name,
570
+ selections: [
571
+ {
572
+ ...spread,
573
+ directives: spread.directives.filter(
574
+ directive => directive !== moduleDirective,
575
+ ),
576
+ },
577
+ operationField,
578
+ componentField,
579
+ ],
580
+ },
581
+ ],
582
+ typeCondition: fragment.type,
583
+ };
584
+ }
585
+
586
+ module.exports = {
587
+ SCHEMA_EXTENSION,
588
+ transform: matchTransform,
589
+ };