relay-compiler 8.0.0 → 10.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) 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 +8930 -8967
  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 +563 -0
  10. package/codegen/ReaderCodeGenerator.js.flow +477 -0
  11. package/codegen/RelayCodeGenerator.js.flow +85 -0
  12. package/codegen/RelayFileWriter.js.flow +365 -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 +73 -0
  19. package/core/ASTConvert.js.flow +233 -0
  20. package/core/CompilerContext.js.flow +190 -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 +327 -0
  27. package/core/IRPrinter.js.flow +482 -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 +131 -0
  36. package/core/RelayParser.js.flow +1731 -0
  37. package/core/RelaySourceModuleParser.js.flow +135 -0
  38. package/core/Schema.js.flow +1983 -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 +232 -0
  51. package/language/javascript/RelayFlowBabelFactories.js.flow +180 -0
  52. package/language/javascript/RelayFlowGenerator.js.flow +1042 -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 +24 -7
  57. package/lib/bin/RelayCompilerMain.js +141 -136
  58. package/lib/codegen/CodegenDirectory.js +13 -8
  59. package/lib/codegen/CodegenRunner.js +37 -76
  60. package/lib/codegen/CodegenWatcher.js +13 -21
  61. package/lib/codegen/NormalizationCodeGenerator.js +117 -140
  62. package/lib/codegen/ReaderCodeGenerator.js +76 -117
  63. package/lib/codegen/RelayCodeGenerator.js +17 -6
  64. package/lib/codegen/RelayFileWriter.js +19 -40
  65. package/lib/codegen/compileRelayArtifacts.js +16 -30
  66. package/lib/codegen/sortObjectByKey.js +43 -0
  67. package/lib/codegen/writeRelayGeneratedFile.js +86 -95
  68. package/lib/core/ASTCache.js +2 -4
  69. package/lib/core/CompilerContext.js +2 -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 +23 -21
  75. package/lib/core/IRTransformer.js +8 -16
  76. package/lib/core/IRValidator.js +1 -9
  77. package/lib/core/IRVisitor.js +0 -2
  78. package/lib/core/RelayCompilerScope.js +4 -4
  79. package/lib/core/RelayGraphQLEnumsGenerator.js +12 -15
  80. package/lib/core/RelayIRTransforms.js +16 -14
  81. package/lib/core/RelayParser.js +53 -89
  82. package/lib/core/RelaySourceModuleParser.js +3 -3
  83. package/lib/core/Schema.js +61 -73
  84. package/lib/core/SchemaUtils.js +15 -1
  85. package/lib/core/getFieldDefinition.js +12 -15
  86. package/lib/core/getIdentifierForSelection.js +2 -4
  87. package/lib/core/inferRootArgumentDefinitions.js +33 -73
  88. package/lib/index.js +4 -5
  89. package/lib/language/javascript/FindGraphQLTags.js +4 -3
  90. package/lib/language/javascript/RelayFlowGenerator.js +82 -171
  91. package/lib/language/javascript/RelayFlowTypeTransformers.js +1 -3
  92. package/lib/language/javascript/RelayLanguagePluginJavaScript.js +6 -4
  93. package/lib/language/javascript/formatGeneratedModule.js +11 -2
  94. package/lib/reporters/ConsoleReporter.js +1 -3
  95. package/lib/reporters/MultiReporter.js +1 -3
  96. package/lib/runner/Artifacts.js +69 -170
  97. package/lib/runner/BufferedFilesystem.js +32 -66
  98. package/lib/runner/GraphQLASTNodeGroup.js +54 -120
  99. package/lib/runner/GraphQLNodeMap.js +14 -19
  100. package/lib/runner/Sources.js +51 -85
  101. package/lib/runner/StrictMap.js +21 -37
  102. package/lib/runner/getChangedNodeNames.js +30 -62
  103. package/lib/transforms/ApplyFragmentArgumentTransform.js +73 -59
  104. package/lib/transforms/ClientExtensionsTransform.js +12 -16
  105. package/lib/transforms/ConnectionTransform.js +30 -37
  106. package/lib/transforms/DeclarativeConnectionMutationTransform.js +167 -0
  107. package/lib/transforms/DeferStreamTransform.js +30 -73
  108. package/lib/transforms/DisallowTypenameOnRoot.js +55 -0
  109. package/lib/transforms/FieldHandleTransform.js +6 -2
  110. package/lib/transforms/FlattenTransform.js +18 -45
  111. package/lib/transforms/GenerateIDFieldTransform.js +56 -35
  112. package/lib/transforms/GenerateTypeNameTransform.js +84 -10
  113. package/lib/transforms/InlineDataFragmentTransform.js +9 -4
  114. package/lib/transforms/MaskTransform.js +17 -17
  115. package/lib/transforms/MatchTransform.js +110 -32
  116. package/lib/transforms/RefetchableFragmentTransform.js +21 -38
  117. package/lib/transforms/RelayDirectiveTransform.js +8 -3
  118. package/lib/transforms/SkipClientExtensionsTransform.js +8 -0
  119. package/lib/transforms/SkipHandleFieldTransform.js +6 -2
  120. package/lib/transforms/SkipRedundantNodesTransform.js +7 -4
  121. package/lib/transforms/SkipSplitOperationTransform.js +32 -0
  122. package/lib/transforms/SkipUnreachableNodeTransform.js +9 -10
  123. package/lib/transforms/SkipUnusedVariablesTransform.js +18 -17
  124. package/lib/transforms/SplitModuleImportTransform.js +2 -2
  125. package/lib/transforms/TestOperationTransform.js +26 -22
  126. package/lib/transforms/ValidateGlobalVariablesTransform.js +18 -30
  127. package/lib/transforms/ValidateRequiredArgumentsTransform.js +12 -16
  128. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +16 -30
  129. package/lib/transforms/ValidateUnusedVariablesTransform.js +18 -30
  130. package/lib/transforms/query-generators/FetchableQueryGenerator.js +161 -0
  131. package/lib/transforms/query-generators/NodeQueryGenerator.js +22 -3
  132. package/lib/transforms/query-generators/QueryQueryGenerator.js +2 -1
  133. package/lib/transforms/query-generators/ViewerQueryGenerator.js +1 -0
  134. package/lib/transforms/query-generators/index.js +23 -6
  135. package/lib/transforms/query-generators/utils.js +17 -16
  136. package/lib/util/RelayCompilerCache.js +2 -4
  137. package/lib/util/argumentContainsVariables.js +37 -0
  138. package/lib/util/dedupeJSONStringify.js +15 -12
  139. package/lib/util/generateAbstractTypeRefinementKey.js +24 -0
  140. package/lib/util/getModuleName.js +3 -5
  141. package/lib/util/joinArgumentDefinitions.js +3 -1
  142. package/package.json +6 -6
  143. package/relay-compiler.js +4 -4
  144. package/relay-compiler.min.js +4 -4
  145. package/reporters/ConsoleReporter.js.flow +81 -0
  146. package/reporters/MultiReporter.js.flow +43 -0
  147. package/reporters/Reporter.js.flow +19 -0
  148. package/runner/Artifacts.js.flow +219 -0
  149. package/runner/BufferedFilesystem.js.flow +194 -0
  150. package/runner/GraphQLASTNodeGroup.js.flow +176 -0
  151. package/runner/GraphQLASTUtils.js.flow +26 -0
  152. package/runner/GraphQLNodeMap.js.flow +55 -0
  153. package/runner/Sources.js.flow +214 -0
  154. package/runner/StrictMap.js.flow +96 -0
  155. package/runner/compileArtifacts.js.flow +76 -0
  156. package/runner/extractAST.js.flow +100 -0
  157. package/runner/getChangedNodeNames.js.flow +48 -0
  158. package/runner/getSchemaInstance.js.flow +36 -0
  159. package/runner/types.js.flow +37 -0
  160. package/transforms/ApplyFragmentArgumentTransform.js.flow +526 -0
  161. package/transforms/ClientExtensionsTransform.js.flow +222 -0
  162. package/transforms/ConnectionTransform.js.flow +856 -0
  163. package/transforms/DeclarativeConnectionMutationTransform.js.flow +157 -0
  164. package/transforms/DeferStreamTransform.js.flow +265 -0
  165. package/transforms/DisallowIdAsAlias.js.flow +47 -0
  166. package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
  167. package/transforms/FieldHandleTransform.js.flow +80 -0
  168. package/transforms/FilterDirectivesTransform.js.flow +45 -0
  169. package/transforms/FlattenTransform.js.flow +453 -0
  170. package/transforms/GenerateIDFieldTransform.js.flow +152 -0
  171. package/transforms/GenerateTypeNameTransform.js.flow +161 -0
  172. package/transforms/InlineDataFragmentTransform.js.flow +125 -0
  173. package/transforms/InlineFragmentsTransform.js.flow +71 -0
  174. package/transforms/MaskTransform.js.flow +126 -0
  175. package/transforms/MatchTransform.js.flow +583 -0
  176. package/transforms/RefetchableFragmentTransform.js.flow +272 -0
  177. package/transforms/RelayDirectiveTransform.js.flow +97 -0
  178. package/transforms/SkipClientExtensionsTransform.js.flow +54 -0
  179. package/transforms/SkipHandleFieldTransform.js.flow +44 -0
  180. package/transforms/SkipRedundantNodesTransform.js.flow +254 -0
  181. package/transforms/SkipSplitOperationTransform.js.flow +37 -0
  182. package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
  183. package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
  184. package/transforms/SplitModuleImportTransform.js.flow +98 -0
  185. package/transforms/TestOperationTransform.js.flow +142 -0
  186. package/transforms/TransformUtils.js.flow +26 -0
  187. package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
  188. package/transforms/ValidateRequiredArgumentsTransform.js.flow +127 -0
  189. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +112 -0
  190. package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
  191. package/transforms/query-generators/FetchableQueryGenerator.js.flow +189 -0
  192. package/transforms/query-generators/NodeQueryGenerator.js.flow +219 -0
  193. package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
  194. package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
  195. package/transforms/query-generators/index.js.flow +90 -0
  196. package/transforms/query-generators/utils.js.flow +76 -0
  197. package/util/CodeMarker.js.flow +79 -0
  198. package/{lib/core/GraphQLIR.js → util/DefaultHandleKey.js.flow} +9 -2
  199. package/util/RelayCompilerCache.js.flow +88 -0
  200. package/util/Rollout.js.flow +39 -0
  201. package/util/TimeReporter.js.flow +79 -0
  202. package/util/areEqualOSS.js.flow +123 -0
  203. package/util/argumentContainsVariables.js.flow +38 -0
  204. package/util/dedupeJSONStringify.js.flow +152 -0
  205. package/util/generateAbstractTypeRefinementKey.js.flow +29 -0
  206. package/util/getDefinitionNodeHash.js.flow +25 -0
  207. package/util/getModuleName.js.flow +39 -0
  208. package/util/joinArgumentDefinitions.js.flow +105 -0
  209. package/util/md5.js.flow +22 -0
  210. package/util/murmurHash.js.flow +94 -0
  211. package/util/nullthrowsOSS.js.flow +25 -0
  212. package/util/orList.js.flow +37 -0
  213. package/util/partitionArray.js.flow +37 -0
  214. package/lib/core/GraphQLCompilerContext.js +0 -165
  215. package/lib/core/GraphQLIRPrinter.js +0 -371
  216. package/lib/core/GraphQLIRTransformer.js +0 -344
  217. package/lib/core/GraphQLIRValidator.js +0 -218
  218. package/lib/core/GraphQLIRVisitor.js +0 -46
  219. package/lib/core/RelayCompilerError.js +0 -277
  220. package/lib/transforms/ConnectionFieldTransform.js +0 -276
@@ -0,0 +1,453 @@
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
+ * @format
8
+ * @flow
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const IRTransformer = require('../core/IRTransformer');
16
+
17
+ const areEqual = require('../util/areEqualOSS');
18
+ const getIdentifierForSelection = require('../core/getIdentifierForSelection');
19
+
20
+ const {createCompilerError, createUserError} = require('../core/CompilerError');
21
+
22
+ import type CompilerContext from '../core/CompilerContext';
23
+ import type {
24
+ Argument,
25
+ Field,
26
+ Handle,
27
+ InlineFragment,
28
+ LinkedField,
29
+ Node,
30
+ ScalarField,
31
+ Selection,
32
+ } from '../core/IR';
33
+ import type {Schema, TypeID} from '../core/Schema';
34
+
35
+ export type FlattenOptions = {|+isForCodegen?: boolean|};
36
+
37
+ type State = {|
38
+ +isForCodegen: boolean,
39
+ parentType: ?TypeID,
40
+ |};
41
+
42
+ /**
43
+ * Transform that flattens inline fragments, fragment spreads, and conditionals.
44
+ *
45
+ * Inline fragments are inlined (replaced with their selections) when:
46
+ * - The fragment type matches the type of its parent, and its `isForCodegen`,
47
+ * or if it's for printing, there is no directive on the inline fragment.
48
+ */
49
+ function flattenTransformImpl(
50
+ context: CompilerContext,
51
+ options?: FlattenOptions,
52
+ ): CompilerContext {
53
+ const state = {
54
+ isForCodegen: !!(options && options.isForCodegen),
55
+ parentType: null,
56
+ };
57
+ const visitorFn = memoizedFlattenSelection(new Map());
58
+ return IRTransformer.transform(
59
+ context,
60
+ {
61
+ Condition: visitorFn,
62
+ Defer: visitorFn,
63
+ Fragment: visitorFn,
64
+ InlineFragment: visitorFn,
65
+ InlineDataFragmentSpread: visitorFn,
66
+ LinkedField: visitorFn,
67
+ Root: visitorFn,
68
+ SplitOperation: visitorFn,
69
+ },
70
+ () => state,
71
+ );
72
+ }
73
+
74
+ function memoizedFlattenSelection(cache) {
75
+ return function flattenSelectionsFn<T: Node>(node: T, state: State): T {
76
+ const context: CompilerContext = this.getContext();
77
+ let nodeCache = cache.get(node);
78
+ if (nodeCache == null) {
79
+ nodeCache = new Map();
80
+ cache.set(node, nodeCache);
81
+ }
82
+ // Determine the current type.
83
+ const parentType = state.parentType;
84
+ const result = nodeCache.get(parentType);
85
+ if (result != null) {
86
+ return result;
87
+ }
88
+
89
+ const type =
90
+ node.kind === 'LinkedField' ||
91
+ node.kind === 'Fragment' ||
92
+ node.kind === 'Root' ||
93
+ node.kind === 'SplitOperation'
94
+ ? node.type
95
+ : node.kind === 'InlineFragment'
96
+ ? node.typeCondition
97
+ : parentType;
98
+ if (type == null) {
99
+ throw createCompilerError('FlattenTransform: Expected a parent type.', [
100
+ node.loc,
101
+ ]);
102
+ }
103
+
104
+ // Flatten the selections in this node, creating a new node with flattened
105
+ // selections if possible, then deeply traverse the flattened node, while
106
+ // keeping track of the parent type.
107
+ const nextSelections = new Map();
108
+ const hasFlattened = flattenSelectionsInto(
109
+ context.getSchema(),
110
+ nextSelections,
111
+ node,
112
+ state,
113
+ type,
114
+ );
115
+ const flattenedNode = hasFlattened
116
+ ? {...node, selections: Array.from(nextSelections.values())}
117
+ : node;
118
+ state.parentType = type;
119
+ const deeplyFlattenedNode = this.traverse(flattenedNode, state);
120
+ state.parentType = parentType;
121
+ nodeCache.set(parentType, deeplyFlattenedNode);
122
+ return deeplyFlattenedNode;
123
+ };
124
+ }
125
+
126
+ /**
127
+ * @private
128
+ */
129
+ function flattenSelectionsInto(
130
+ schema: Schema,
131
+ flattenedSelections: Map<string, Selection>,
132
+ node: Node,
133
+ state: State,
134
+ type: TypeID,
135
+ ): boolean {
136
+ let hasFlattened = false;
137
+ node.selections.forEach(selection => {
138
+ if (
139
+ selection.kind === 'InlineFragment' &&
140
+ shouldFlattenInlineFragment(schema, selection, state, type)
141
+ ) {
142
+ hasFlattened = true;
143
+ flattenSelectionsInto(
144
+ schema,
145
+ flattenedSelections,
146
+ selection,
147
+ state,
148
+ type,
149
+ );
150
+ return;
151
+ }
152
+ const nodeIdentifier = getIdentifierForSelection(schema, selection);
153
+ const flattenedSelection = flattenedSelections.get(nodeIdentifier);
154
+ // If this selection hasn't been seen before, keep track of it.
155
+ if (!flattenedSelection) {
156
+ flattenedSelections.set(nodeIdentifier, selection);
157
+ return;
158
+ }
159
+ // Otherwise a similar selection exists which should be merged.
160
+ hasFlattened = true;
161
+ if (flattenedSelection.kind === 'InlineFragment') {
162
+ if (selection.kind !== 'InlineFragment') {
163
+ throw createCompilerError(
164
+ `FlattenTransform: Expected an InlineFragment, got a '${selection.kind}'`,
165
+ [selection.loc],
166
+ );
167
+ }
168
+ flattenedSelections.set(nodeIdentifier, {
169
+ ...flattenedSelection,
170
+ selections: mergeSelections(
171
+ schema,
172
+ flattenedSelection,
173
+ selection,
174
+ state,
175
+ selection.typeCondition,
176
+ ),
177
+ });
178
+ } else if (flattenedSelection.kind === 'Condition') {
179
+ if (selection.kind !== 'Condition') {
180
+ throw createCompilerError(
181
+ `FlattenTransform: Expected a Condition, got a '${selection.kind}'`,
182
+ [selection.loc],
183
+ );
184
+ }
185
+ flattenedSelections.set(nodeIdentifier, {
186
+ ...flattenedSelection,
187
+ selections: mergeSelections(
188
+ schema,
189
+ flattenedSelection,
190
+ selection,
191
+ state,
192
+ type,
193
+ ),
194
+ });
195
+ } else if (flattenedSelection.kind === 'ClientExtension') {
196
+ if (selection.kind !== 'ClientExtension') {
197
+ throw createCompilerError(
198
+ `FlattenTransform: Expected a ClientExtension, got a '${selection.kind}'`,
199
+ [selection.loc],
200
+ );
201
+ }
202
+ flattenedSelections.set(nodeIdentifier, {
203
+ ...flattenedSelection,
204
+ selections: mergeSelections(
205
+ schema,
206
+ flattenedSelection,
207
+ selection,
208
+ state,
209
+ type,
210
+ ),
211
+ });
212
+ } else if (flattenedSelection.kind === 'FragmentSpread') {
213
+ // Ignore duplicate fragment spreads.
214
+ } else if (flattenedSelection.kind === 'ModuleImport') {
215
+ if (selection.kind !== 'ModuleImport') {
216
+ throw createCompilerError(
217
+ `FlattenTransform: Expected a ModuleImport, got a '${selection.kind}'`,
218
+ [selection.loc],
219
+ );
220
+ }
221
+ if (
222
+ selection.name !== flattenedSelection.name ||
223
+ selection.module !== flattenedSelection.module ||
224
+ selection.key !== flattenedSelection.key
225
+ ) {
226
+ throw createUserError(
227
+ 'Found conflicting @module selections: use a unique alias on the ' +
228
+ 'parent fields.',
229
+ [selection.loc, flattenedSelection.loc],
230
+ );
231
+ }
232
+ flattenedSelections.set(nodeIdentifier, {
233
+ ...flattenedSelection,
234
+ selections: mergeSelections(
235
+ schema,
236
+ flattenedSelection,
237
+ selection,
238
+ state,
239
+ type,
240
+ ),
241
+ });
242
+ } else if (flattenedSelection.kind === 'Defer') {
243
+ if (selection.kind !== 'Defer') {
244
+ throw createCompilerError(
245
+ `FlattenTransform: Expected a Defer, got a '${selection.kind}'`,
246
+ [selection.loc],
247
+ );
248
+ }
249
+ flattenedSelections.set(nodeIdentifier, {
250
+ kind: 'Defer',
251
+ ...flattenedSelection,
252
+ selections: mergeSelections(
253
+ schema,
254
+ flattenedSelection,
255
+ selection,
256
+ state,
257
+ type,
258
+ ),
259
+ });
260
+ } else if (flattenedSelection.kind === 'Stream') {
261
+ if (selection.kind !== 'Stream') {
262
+ throw createCompilerError(
263
+ `FlattenTransform: Expected a Stream, got a '${selection.kind}'`,
264
+ [selection.loc],
265
+ );
266
+ }
267
+ flattenedSelections.set(nodeIdentifier, {
268
+ kind: 'Stream',
269
+ ...flattenedSelection,
270
+ selections: mergeSelections(
271
+ schema,
272
+ flattenedSelection,
273
+ selection,
274
+ state,
275
+ type,
276
+ ),
277
+ });
278
+ } else if (flattenedSelection.kind === 'LinkedField') {
279
+ if (selection.kind !== 'LinkedField') {
280
+ throw createCompilerError(
281
+ `FlattenTransform: Expected a LinkedField, got a '${selection.kind}'`,
282
+ [selection.loc],
283
+ );
284
+ }
285
+ assertUniqueArgsForAlias(selection, flattenedSelection);
286
+ // NOTE: not using object spread here as this code is pretty hot
287
+ flattenedSelections.set(nodeIdentifier, {
288
+ kind: 'LinkedField',
289
+ alias: flattenedSelection.alias,
290
+ args: flattenedSelection.args,
291
+ connection: flattenedSelection.connection || selection.connection,
292
+ directives: flattenedSelection.directives,
293
+ handles: mergeHandles(flattenedSelection, selection),
294
+ loc: flattenedSelection.loc,
295
+ metadata: flattenedSelection.metadata,
296
+ name: flattenedSelection.name,
297
+ selections: mergeSelections(
298
+ schema,
299
+ flattenedSelection,
300
+ selection,
301
+ state,
302
+ selection.type,
303
+ ),
304
+ type: flattenedSelection.type,
305
+ });
306
+ } else if (flattenedSelection.kind === 'ScalarField') {
307
+ if (selection.kind !== 'ScalarField') {
308
+ throw createCompilerError(
309
+ `FlattenTransform: Expected a ScalarField, got a '${selection.kind}'`,
310
+ [selection.loc],
311
+ );
312
+ }
313
+ assertUniqueArgsForAlias(selection, flattenedSelection);
314
+ if (selection.handles && selection.handles.length > 0) {
315
+ flattenedSelections.set(nodeIdentifier, {
316
+ kind: 'ScalarField',
317
+ ...flattenedSelection,
318
+ handles: mergeHandles(selection, flattenedSelection),
319
+ });
320
+ }
321
+ } else if (flattenedSelection.kind === 'InlineDataFragmentSpread') {
322
+ throw createCompilerError(
323
+ 'FlattenTransform: did not expect an InlineDataFragmentSpread node. ' +
324
+ 'Only expecting InlineDataFragmentSpread in reader ASTs and this ' +
325
+ 'transform to run only on normalization ASTs.',
326
+ [selection.loc],
327
+ );
328
+ } else {
329
+ (flattenedSelection.kind: empty);
330
+ throw createCompilerError(
331
+ `FlattenTransform: Unknown kind '${flattenedSelection.kind}'`,
332
+ );
333
+ }
334
+ });
335
+ return hasFlattened;
336
+ }
337
+
338
+ /**
339
+ * @private
340
+ */
341
+ function mergeSelections(
342
+ schema: Schema,
343
+ nodeA: Node,
344
+ nodeB: Node,
345
+ state: State,
346
+ type: TypeID,
347
+ ): $ReadOnlyArray<Selection> {
348
+ const flattenedSelections = new Map();
349
+ flattenSelectionsInto(schema, flattenedSelections, nodeA, state, type);
350
+ flattenSelectionsInto(schema, flattenedSelections, nodeB, state, type);
351
+ return Array.from(flattenedSelections.values());
352
+ }
353
+
354
+ /**
355
+ * @private
356
+ * TODO(T19327202) This is redundant with OverlappingFieldsCanBeMergedRule once
357
+ * it can be enabled.
358
+ */
359
+ function assertUniqueArgsForAlias(field: Field, otherField: Field): void {
360
+ if (!areEqualFields(field, otherField)) {
361
+ throw createUserError(
362
+ 'Expected all fields on the same parent with the name or alias ' +
363
+ `'${field.alias}' to have the same name and arguments.`,
364
+ [field.loc, otherField.loc],
365
+ );
366
+ }
367
+ }
368
+
369
+ /**
370
+ * @private
371
+ */
372
+ function shouldFlattenInlineFragment(
373
+ schema: Schema,
374
+ fragment: InlineFragment,
375
+ state: State,
376
+ type: TypeID,
377
+ ): boolean {
378
+ return (
379
+ schema.areEqualTypes(fragment.typeCondition, schema.getRawType(type)) &&
380
+ (state.isForCodegen || fragment.directives.length === 0)
381
+ );
382
+ }
383
+
384
+ /**
385
+ * @private
386
+ *
387
+ * Verify that two fields are equal in all properties other than their
388
+ * selections.
389
+ */
390
+ function areEqualFields(thisField: Field, thatField: Field): boolean {
391
+ return (
392
+ thisField.kind === thatField.kind &&
393
+ thisField.name === thatField.name &&
394
+ thisField.alias === thatField.alias &&
395
+ areEqualArgs(thisField.args, thatField.args)
396
+ );
397
+ }
398
+
399
+ /**
400
+ * Verify that two sets of arguments are equivalent - same argument names
401
+ * and values. Notably this ignores the types of arguments and values, which
402
+ * may not always be inferred identically.
403
+ */
404
+ function areEqualArgs(
405
+ thisArgs: $ReadOnlyArray<Argument>,
406
+ thatArgs: $ReadOnlyArray<Argument>,
407
+ ): boolean {
408
+ return (
409
+ thisArgs.length === thatArgs.length &&
410
+ thisArgs.every((thisArg, index) => {
411
+ const thatArg = thatArgs[index];
412
+ return (
413
+ thisArg.name === thatArg.name &&
414
+ thisArg.value.kind === thatArg.value.kind &&
415
+ (thisArg.value: any).variableName ===
416
+ (thatArg.value: any).variableName &&
417
+ areEqual((thisArg.value: any).value, (thatArg.value: any).value)
418
+ );
419
+ })
420
+ );
421
+ }
422
+
423
+ /**
424
+ * @private
425
+ */
426
+ function mergeHandles<T: LinkedField | ScalarField>(
427
+ nodeA: T,
428
+ nodeB: T,
429
+ ): ?$ReadOnlyArray<Handle> {
430
+ if (!nodeA.handles) {
431
+ return nodeB.handles;
432
+ }
433
+ if (!nodeB.handles) {
434
+ return nodeA.handles;
435
+ }
436
+ const uniqueItems = new Map();
437
+ nodeA.handles
438
+ .concat(nodeB.handles)
439
+ .forEach(item => uniqueItems.set(item.name + item.key, item));
440
+ return Array.from(uniqueItems.values());
441
+ }
442
+
443
+ function transformWithOptions(
444
+ options: FlattenOptions,
445
+ ): (context: CompilerContext) => CompilerContext {
446
+ return function flattenTransform(context: CompilerContext): CompilerContext {
447
+ return flattenTransformImpl(context, options);
448
+ };
449
+ }
450
+
451
+ module.exports = {
452
+ transformWithOptions,
453
+ };
@@ -0,0 +1,152 @@
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 {generateIDField} = require('../core/SchemaUtils');
18
+ const {hasUnaliasedSelection} = require('./TransformUtils');
19
+
20
+ import type CompilerContext from '../core/CompilerContext';
21
+ import type {InlineFragment, LinkedField, ScalarField} from '../core/IR';
22
+ import type {CompositeTypeID} from '../core/Schema';
23
+
24
+ const ID = 'id';
25
+ const NODE_TYPE = 'Node';
26
+
27
+ type State = {|
28
+ idFieldForType: CompositeTypeID => ScalarField,
29
+ idFragmentForType: CompositeTypeID => InlineFragment,
30
+ |};
31
+
32
+ /**
33
+ * A transform that adds an `id` field on any type that has an id field but
34
+ * where there is no unaliased `id` selection.
35
+ */
36
+ function generateIDFieldTransform(context: CompilerContext): CompilerContext {
37
+ const schema = context.getSchema();
38
+
39
+ const typeToIDField = new Map();
40
+ function idFieldForType(type: CompositeTypeID): ScalarField {
41
+ let idField = typeToIDField.get(type);
42
+ if (idField == null) {
43
+ idField = generateIDField(schema, type);
44
+ typeToIDField.set(type, idField);
45
+ }
46
+ return idField;
47
+ }
48
+
49
+ const typeToIDFragment = new Map();
50
+ function idFragmentForType(type: CompositeTypeID): InlineFragment {
51
+ let fragment = typeToIDFragment.get(type);
52
+ if (fragment == null) {
53
+ fragment = {
54
+ kind: 'InlineFragment',
55
+ directives: [],
56
+ loc: {kind: 'Generated'},
57
+ metadata: null,
58
+ selections: [idFieldForType(type)],
59
+ typeCondition: type,
60
+ };
61
+ typeToIDFragment.set(type, fragment);
62
+ }
63
+ return fragment;
64
+ }
65
+
66
+ const state = {
67
+ idFieldForType,
68
+ idFragmentForType,
69
+ };
70
+ return IRTransformer.transform(
71
+ context,
72
+ {
73
+ LinkedField: visitLinkedField,
74
+ },
75
+ () => state,
76
+ );
77
+ }
78
+
79
+ function visitLinkedField(field: LinkedField, state: State): LinkedField {
80
+ const transformedNode = this.traverse(field, state);
81
+
82
+ // If the field already has an unaliased `id` field, do nothing
83
+ if (hasUnaliasedSelection(field, ID)) {
84
+ return transformedNode;
85
+ }
86
+
87
+ const context: CompilerContext = this.getContext();
88
+ const schema = context.getSchema();
89
+ const unmodifiedType = schema.assertCompositeType(
90
+ schema.getRawType(field.type),
91
+ );
92
+
93
+ // If the field type has an `id` subfield add an `id` selection
94
+ if (
95
+ schema.canHaveSelections(unmodifiedType) &&
96
+ schema.hasId(unmodifiedType)
97
+ ) {
98
+ return {
99
+ ...transformedNode,
100
+ selections: [
101
+ ...transformedNode.selections,
102
+ state.idFieldForType(unmodifiedType),
103
+ ],
104
+ };
105
+ }
106
+
107
+ // If the field type is abstract, then generate a `... on Node { id }`
108
+ // fragment if *any* concrete type implements Node. Then generate a
109
+ // `... on PossibleType { id }` for every concrete type that does *not*
110
+ // implement `Node`
111
+ const nodeType = schema.getTypeFromString(NODE_TYPE);
112
+ if (!nodeType) {
113
+ return transformedNode;
114
+ }
115
+
116
+ const nodeInterface = schema.assertInterfaceType(nodeType);
117
+
118
+ if (schema.isAbstractType(unmodifiedType)) {
119
+ const selections = [...transformedNode.selections];
120
+ if (schema.mayImplement(unmodifiedType, nodeInterface)) {
121
+ selections.push(state.idFragmentForType(nodeInterface));
122
+ }
123
+ Array.from(
124
+ schema
125
+ .getPossibleTypes(schema.assertAbstractType(unmodifiedType))
126
+ .values(),
127
+ )
128
+ .filter(
129
+ concreteType =>
130
+ !schema.implementsInterface(
131
+ schema.assertCompositeType(concreteType),
132
+ nodeInterface,
133
+ ) && schema.hasId(concreteType),
134
+ )
135
+ .sort((a, b) =>
136
+ schema.getTypeString(a) < schema.getTypeString(b) ? -1 : 1,
137
+ )
138
+ .forEach(concreteType => {
139
+ selections.push(state.idFragmentForType(concreteType));
140
+ });
141
+ return {
142
+ ...transformedNode,
143
+ selections,
144
+ };
145
+ }
146
+
147
+ return transformedNode;
148
+ }
149
+
150
+ module.exports = {
151
+ transform: generateIDFieldTransform,
152
+ };