@ardatan/relay-compiler 12.0.0

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