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,257 @@
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
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const IRTransformer = require('../core/IRTransformer');
16
+
17
+ import type CompilerContext from '../core/CompilerContext';
18
+ const IMap = require('immutable').Map;
19
+ const partitionArray = require('../util/partitionArray');
20
+ const getIdentifierForSelection = require('../core/getIdentifierForSelection');
21
+ const invariant = require('invariant');
22
+
23
+ import type {Schema} from '../core/Schema';
24
+
25
+ import type {Fragment, Node, Root, SplitOperation, Selection} from '../core/IR';
26
+
27
+ /**
28
+ * A simplified representation of a document: keys in the map are unique
29
+ * identifiers for the selections of a node, values are either null (for scalars)
30
+ * or nested maps for items with subselections (linked fields, inline fragments,
31
+ * etc).
32
+ */
33
+ // $FlowFixMe[value-as-type]
34
+ type SelectionMap = IMap<string, ?SelectionMap>;
35
+
36
+ /**
37
+ * A transform that removes redundant fields and fragment spreads. Redundancy is
38
+ * defined in this context as any selection that is guaranteed to already be
39
+ * fetched by an ancestor selection. This can occur in two cases:
40
+ *
41
+ * 1. Simple duplicates at the same level of the document can always be skipped:
42
+ *
43
+ * ```
44
+ * fragment Foo on FooType {
45
+ * id
46
+ * id
47
+ * ...Bar
48
+ * ...Bar
49
+ * }
50
+ * ```
51
+ *
52
+ * Becomes
53
+ *
54
+ * ```
55
+ * fragment Foo on FooType {
56
+ * id
57
+ * ...Bar
58
+ * }
59
+ * ```
60
+ *
61
+ * 2. Inline fragments and conditions introduce the possibility for duplication
62
+ * at different levels of the tree. Whenever a selection is fetched in a parent,
63
+ * it is redundant to also fetch it in a child:
64
+ *
65
+ * ```
66
+ * fragment Foo on FooType {
67
+ * id
68
+ * ... on OtherType {
69
+ * id # 1
70
+ * }
71
+ * ... on FooType @include(if: $cond) {
72
+ * id # 2
73
+ * }
74
+ * }
75
+ * ```
76
+ *
77
+ * Becomes:
78
+ *
79
+ * ```
80
+ * fragment Foo on FooType {
81
+ * id
82
+ * }
83
+ * ```
84
+ *
85
+ * In this example:
86
+ * - 1 can be skipped because `id` is already fetched by the parent. Even
87
+ * though the type is different (FooType/OtherType), the inline fragment
88
+ * cannot match without the outer fragment matching so the outer `id` is
89
+ * guaranteed to already be fetched.
90
+ * - 2 can be skipped for similar reasons: it doesn't matter if the condition
91
+ * holds, `id` is already fetched by the parent regardless.
92
+ *
93
+ * This transform also handles more complicated cases in which selections are
94
+ * nested:
95
+ *
96
+ * ```
97
+ * fragment Foo on FooType {
98
+ * a {
99
+ * bb
100
+ * }
101
+ * ... on OtherType {
102
+ * a {
103
+ * bb # 1
104
+ * cc
105
+ * }
106
+ * }
107
+ * }
108
+ * ```
109
+ *
110
+ * Becomes
111
+ *
112
+ * ```
113
+ * fragment Foo on FooType {
114
+ * a {
115
+ * bb
116
+ * }
117
+ * ... on OtherType {
118
+ * a {
119
+ * cc
120
+ * }
121
+ * }
122
+ * }
123
+ * ```
124
+ *
125
+ * 1 can be skipped because it is already fetched at the outer level.
126
+ */
127
+ function skipRedundantNodesTransform(
128
+ context: CompilerContext,
129
+ ): CompilerContext {
130
+ return IRTransformer.transform(context, {
131
+ Root: visitNode,
132
+ SplitOperation: visitNode,
133
+ Fragment: visitNode,
134
+ });
135
+ }
136
+
137
+ let cache = new Map();
138
+ function visitNode<T: Fragment | Root | SplitOperation>(node: T): ?T {
139
+ cache = new Map();
140
+ const context: CompilerContext = this.getContext();
141
+ return transformNode(context.getSchema(), node, new IMap()).node;
142
+ }
143
+
144
+ /**
145
+ * The most straightforward approach would be two passes: one to record the
146
+ * structure of the document, one to prune duplicates. This implementation uses
147
+ * a single pass. Selections are sorted with fields first, "conditionals"
148
+ * (inline fragments & conditions) last. This means that all fields that are
149
+ * guaranteed to be fetched are encountered prior to any duplicates that may be
150
+ * fetched within a conditional.
151
+ *
152
+ * Because selections fetched within a conditional are not guaranteed to be
153
+ * fetched in the parent, a fork of the selection map is created when entering a
154
+ * conditional. The sort ensures that guaranteed fields have already been seen
155
+ * prior to the clone.
156
+ */
157
+ function transformNode<T: Node>(
158
+ schema: Schema,
159
+ node: T,
160
+ selectionMap: SelectionMap,
161
+ ): {
162
+ selectionMap: SelectionMap,
163
+ node: ?T,
164
+ ...
165
+ } {
166
+ // This will optimize a traversal of the same subselections.
167
+ // If it's the same node, and selectionMap is empty
168
+ // result of transformNode has to be the same.
169
+ const isEmptySelectionMap = selectionMap.size === 0;
170
+ let result;
171
+ if (isEmptySelectionMap) {
172
+ // $FlowFixMe[escaped-generic]
173
+ result = cache.get(node);
174
+ if (result != null) {
175
+ return result;
176
+ }
177
+ }
178
+ const selections = [];
179
+ sortSelections(node.selections).forEach(selection => {
180
+ const identifier = getIdentifierForSelection(schema, selection);
181
+ switch (selection.kind) {
182
+ case 'ScalarField':
183
+ case 'FragmentSpread': {
184
+ if (!selectionMap.has(identifier)) {
185
+ selections.push(selection);
186
+ selectionMap = selectionMap.set(identifier, null);
187
+ }
188
+ break;
189
+ }
190
+ case 'Defer':
191
+ case 'Stream':
192
+ case 'ModuleImport':
193
+ case 'ClientExtension':
194
+ case 'InlineDataFragmentSpread':
195
+ case 'LinkedField': {
196
+ const transformed = transformNode(
197
+ schema,
198
+ selection,
199
+ selectionMap.get(identifier) || new IMap(),
200
+ );
201
+ if (transformed.node) {
202
+ selections.push(transformed.node);
203
+ selectionMap = selectionMap.set(identifier, transformed.selectionMap);
204
+ }
205
+ break;
206
+ }
207
+ case 'InlineFragment':
208
+ case 'Condition': {
209
+ // Fork the selection map to prevent conditional selections from
210
+ // affecting the outer "guaranteed" selections.
211
+ const transformed = transformNode(
212
+ schema,
213
+ selection,
214
+ selectionMap.get(identifier) || selectionMap,
215
+ );
216
+ if (transformed.node) {
217
+ selections.push(transformed.node);
218
+ selectionMap = selectionMap.set(identifier, transformed.selectionMap);
219
+ }
220
+ break;
221
+ }
222
+ default:
223
+ (selection: empty);
224
+ invariant(
225
+ false,
226
+ 'SkipRedundantNodesTransform: Unexpected node kind `%s`.',
227
+ selection.kind,
228
+ );
229
+ }
230
+ });
231
+ const nextNode: any = selections.length ? {...node, selections} : null;
232
+ result = {selectionMap, node: nextNode};
233
+ if (isEmptySelectionMap) {
234
+ // $FlowFixMe[escaped-generic]
235
+ cache.set(node, result);
236
+ }
237
+ return result;
238
+ }
239
+
240
+ /**
241
+ * Sort inline fragments and conditions after other selections.
242
+ */
243
+ function sortSelections(
244
+ selections: $ReadOnlyArray<Selection>,
245
+ ): $ReadOnlyArray<Selection> {
246
+ const isScalarOrLinkedField = selection =>
247
+ selection.kind === 'ScalarField' || selection.kind === 'LinkedField';
248
+ const [scalarsAndLinkedFields, rest] = partitionArray(
249
+ selections,
250
+ isScalarOrLinkedField,
251
+ );
252
+ return [...scalarsAndLinkedFields, ...rest];
253
+ }
254
+
255
+ module.exports = {
256
+ transform: skipRedundantNodesTransform,
257
+ };
@@ -0,0 +1,37 @@
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
+ import type CompilerContext from '../core/CompilerContext';
18
+
19
+ function skipNode() {
20
+ return null;
21
+ }
22
+
23
+ /**
24
+ * A transform that removes field `splitOperations`. Intended for use when e.g.
25
+ * printing queries to send to a GraphQL server.
26
+ */
27
+ function skipSplitOperationTransform(
28
+ context: CompilerContext,
29
+ ): CompilerContext {
30
+ return IRTransformer.transform(context, {
31
+ SplitOperation: skipNode,
32
+ });
33
+ }
34
+
35
+ module.exports = {
36
+ transform: skipSplitOperationTransform,
37
+ };
@@ -0,0 +1,149 @@
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 invariant = require('invariant');
18
+
19
+ import type CompilerContext from '../core/CompilerContext';
20
+ import type {Condition, Fragment, Node, Selection} from '../core/IR';
21
+
22
+ type ConditionResult = 'fail' | 'pass' | 'variable';
23
+
24
+ const FAIL = 'fail';
25
+ const PASS = 'pass';
26
+ const VARIABLE = 'variable';
27
+
28
+ /**
29
+ * A tranform that removes unreachable IR nodes from all documents in a corpus.
30
+ * The following nodes are removed:
31
+ * - Any node with `@include(if: false)`
32
+ * - Any node with `@skip(if: true)`
33
+ * - Any node with empty `selections`
34
+ */
35
+ function skipUnreachableNodeTransform(
36
+ context: CompilerContext,
37
+ ): CompilerContext {
38
+ const fragments: Map<string, ?Fragment> = new Map();
39
+ const nextContext = IRTransformer.transform(context, {
40
+ Root: node => transformNode(context, fragments, node),
41
+ SplitOperation: node => transformNode(context, fragments, node),
42
+ // Fragments are included below where referenced.
43
+ // Unreferenced fragments are not included.
44
+ Fragment: id => null,
45
+ });
46
+ return (Array.from(fragments.values()): Array<?Fragment>).reduce(
47
+ (ctx: CompilerContext, fragment) => (fragment ? ctx.add(fragment) : ctx),
48
+ nextContext,
49
+ );
50
+ }
51
+
52
+ function transformNode<T: Node>(
53
+ context: CompilerContext,
54
+ fragments: Map<string, ?Fragment>,
55
+ node: T,
56
+ ): ?T {
57
+ const queue: Array<Selection> = [...node.selections];
58
+ let selections;
59
+ while (queue.length) {
60
+ const selection: Selection = queue.shift();
61
+ let nextSelection;
62
+ switch (selection.kind) {
63
+ case 'Condition':
64
+ const match = testCondition(selection);
65
+ if (match === PASS) {
66
+ queue.unshift(...selection.selections);
67
+ } else if (match === VARIABLE) {
68
+ nextSelection = transformNode(context, fragments, selection);
69
+ }
70
+ break;
71
+ case 'FragmentSpread': {
72
+ // Skip fragment spreads if the referenced fragment is empty
73
+ if (!fragments.has(selection.name)) {
74
+ const fragment = context.getFragment(selection.name);
75
+ const nextFragment = transformNode(context, fragments, fragment);
76
+ fragments.set(selection.name, nextFragment);
77
+ }
78
+ if (fragments.get(selection.name)) {
79
+ nextSelection = selection;
80
+ }
81
+ break;
82
+ }
83
+ case 'ClientExtension':
84
+ nextSelection = transformNode(context, fragments, selection);
85
+ break;
86
+ case 'ModuleImport':
87
+ nextSelection = transformNode(context, fragments, selection);
88
+ break;
89
+ case 'LinkedField':
90
+ nextSelection = transformNode(context, fragments, selection);
91
+ break;
92
+ case 'InlineFragment':
93
+ // TODO combine with the LinkedField case when flow supports this
94
+ nextSelection = transformNode(context, fragments, selection);
95
+ break;
96
+ case 'Defer':
97
+ nextSelection = transformNode(context, fragments, selection);
98
+ break;
99
+ case 'Stream':
100
+ nextSelection = transformNode(context, fragments, selection);
101
+ break;
102
+ case 'ScalarField':
103
+ nextSelection = selection;
104
+ break;
105
+ case 'InlineDataFragmentSpread':
106
+ invariant(
107
+ false,
108
+ 'SkipUnreachableNodeTransform: Did not expect an ' +
109
+ 'InlineDataFragmentSpread here. Only expecting ' +
110
+ 'InlineDataFragmentSpread in reader ASTs and this transform to ' +
111
+ 'run only on normalization ASTs.',
112
+ );
113
+ // fallthrough
114
+ default:
115
+ (selection.kind: empty);
116
+ invariant(
117
+ false,
118
+ 'SkipUnreachableNodeTransform: Unexpected selection kind `%s`.',
119
+ selection.kind,
120
+ );
121
+ }
122
+ if (nextSelection) {
123
+ selections = selections || [];
124
+ selections.push(nextSelection);
125
+ }
126
+ }
127
+ if (selections) {
128
+ return ({
129
+ ...node,
130
+ selections,
131
+ }: $FlowIssue);
132
+ }
133
+ return null;
134
+ }
135
+
136
+ /**
137
+ * Determines whether a condition statically passes/fails or is unknown
138
+ * (variable).
139
+ */
140
+ function testCondition(condition: Condition): ConditionResult {
141
+ if (condition.condition.kind === 'Variable') {
142
+ return VARIABLE;
143
+ }
144
+ return condition.condition.value === condition.passingValue ? PASS : FAIL;
145
+ }
146
+
147
+ module.exports = {
148
+ transform: skipUnreachableNodeTransform,
149
+ };
@@ -0,0 +1,59 @@
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 inferRootArgumentDefinitions = require('../core/inferRootArgumentDefinitions');
16
+
17
+ import type CompilerContext from '../core/CompilerContext';
18
+ import type {Root} from '../core/IR';
19
+
20
+ /**
21
+ * Refines the argument definitions for operations to remove unused arguments
22
+ * due to statically pruned conditional branches (e.g. because of overriding
23
+ * a variable used in `@include()` to be false).
24
+ */
25
+ function skipUnusedVariablesTransform(
26
+ context: CompilerContext,
27
+ ): CompilerContext {
28
+ const contextWithUsedArguments = inferRootArgumentDefinitions(context);
29
+ return context.withMutations(ctx => {
30
+ let nextContext = ctx;
31
+ for (const node of nextContext.documents()) {
32
+ if (node.kind !== 'Root') {
33
+ continue;
34
+ }
35
+ const usedArguments = new Set(
36
+ contextWithUsedArguments
37
+ .getRoot(node.name)
38
+ .argumentDefinitions.map(argDef => argDef.name),
39
+ );
40
+ // Remove unused argument definitions
41
+ const usedArgumentDefinitions = node.argumentDefinitions.filter(argDef =>
42
+ usedArguments.has(argDef.name),
43
+ );
44
+ if (usedArgumentDefinitions.length !== node.argumentDefinitions.length) {
45
+ nextContext = nextContext.replace(
46
+ ({
47
+ ...node,
48
+ argumentDefinitions: usedArgumentDefinitions,
49
+ }: Root),
50
+ );
51
+ }
52
+ }
53
+ return nextContext;
54
+ });
55
+ }
56
+
57
+ module.exports = {
58
+ transform: skipUnusedVariablesTransform,
59
+ };
@@ -0,0 +1,98 @@
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 getNormalizationOperationName = require('../core/getNormalizationOperationName');
18
+
19
+ import type CompilerContext from '../core/CompilerContext';
20
+ import type {
21
+ LinkedField,
22
+ InlineFragment,
23
+ ModuleImport,
24
+ SplitOperation,
25
+ } from '../core/IR';
26
+ import type {CompositeTypeID} from '../core/Schema';
27
+
28
+ type State = {|
29
+ parentType: CompositeTypeID,
30
+ splitOperations: Map<string, SplitOperation>,
31
+ |};
32
+
33
+ /**
34
+ * This transform creates a SplitOperation root for every ModuleImport.
35
+ */
36
+ function splitMatchTransform(context: CompilerContext): CompilerContext {
37
+ const splitOperations = new Map();
38
+ const transformedContext = IRTransformer.transform(
39
+ context,
40
+ {
41
+ LinkedField: visitLinkedField,
42
+ InlineFragment: visitInlineFragment,
43
+ ModuleImport: visitModuleImport,
44
+ },
45
+ node => ({
46
+ parentType: node.type,
47
+ splitOperations,
48
+ }),
49
+ );
50
+ return transformedContext.addAll(Array.from(splitOperations.values()));
51
+ }
52
+
53
+ function visitLinkedField(field: LinkedField, state: State): LinkedField {
54
+ return this.traverse(field, {
55
+ parentType: field.type,
56
+ splitOperations: state.splitOperations,
57
+ });
58
+ }
59
+
60
+ function visitInlineFragment(
61
+ fragment: InlineFragment,
62
+ state: State,
63
+ ): InlineFragment {
64
+ return this.traverse(fragment, {
65
+ parentType: fragment.typeCondition,
66
+ splitOperations: state.splitOperations,
67
+ });
68
+ }
69
+
70
+ function visitModuleImport(node: ModuleImport, state: State): ModuleImport {
71
+ // It's possible for the same fragment to be selected in multiple usages
72
+ // of @module: skip processing a node if its SplitOperation has already
73
+ // been generated
74
+ const normalizationName = getNormalizationOperationName(node.name);
75
+ const createdSplitOperation = state.splitOperations.get(normalizationName);
76
+ if (createdSplitOperation) {
77
+ createdSplitOperation.parentSources.add(node.sourceDocument);
78
+ return node;
79
+ }
80
+ const transformedNode = this.traverse(node, state);
81
+ const splitOperation: SplitOperation = {
82
+ kind: 'SplitOperation',
83
+ name: normalizationName,
84
+ selections: transformedNode.selections,
85
+ loc: {kind: 'Derived', source: node.loc},
86
+ parentSources: new Set([node.sourceDocument]),
87
+ metadata: {
88
+ derivedFrom: transformedNode.name,
89
+ },
90
+ type: state.parentType,
91
+ };
92
+ state.splitOperations.set(normalizationName, splitOperation);
93
+ return transformedNode;
94
+ }
95
+
96
+ module.exports = {
97
+ transform: splitMatchTransform,
98
+ };