relay-compiler 12.0.0 → 13.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (262) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +108 -0
  3. package/cli.js +23 -0
  4. package/index.js +22 -4
  5. package/linux-x64/relay +0 -0
  6. package/macos-arm64/relay +0 -0
  7. package/macos-x64/relay +0 -0
  8. package/package.json +2 -26
  9. package/win-x64/relay.exe +0 -0
  10. package/bin/RelayCompilerBin.js.flow +0 -169
  11. package/bin/RelayCompilerMain.js.flow +0 -515
  12. package/bin/__fixtures__/plugin-module.js.flow +0 -17
  13. package/bin/relay-compiler +0 -19066
  14. package/codegen/CodegenDirectory.js.flow +0 -375
  15. package/codegen/CodegenRunner.js.flow +0 -432
  16. package/codegen/CodegenTypes.js.flow +0 -28
  17. package/codegen/CodegenWatcher.js.flow +0 -254
  18. package/codegen/NormalizationCodeGenerator.js.flow +0 -566
  19. package/codegen/ReaderCodeGenerator.js.flow +0 -512
  20. package/codegen/RelayCodeGenerator.js.flow +0 -85
  21. package/codegen/RelayFileWriter.js.flow +0 -367
  22. package/codegen/SourceControl.js.flow +0 -58
  23. package/codegen/compileRelayArtifacts.js.flow +0 -182
  24. package/codegen/createPrintRequireModuleDependency.js.flow +0 -19
  25. package/codegen/sortObjectByKey.js.flow +0 -25
  26. package/codegen/writeRelayGeneratedFile.js.flow +0 -239
  27. package/core/ASTCache.js.flow +0 -74
  28. package/core/ASTConvert.js.flow +0 -233
  29. package/core/CompilerContext.js.flow +0 -191
  30. package/core/CompilerError.js.flow +0 -255
  31. package/core/DotGraphQLParser.js.flow +0 -39
  32. package/core/GraphQLCompilerProfiler.js.flow +0 -341
  33. package/core/GraphQLDerivedFromMetadata.js.flow +0 -36
  34. package/core/GraphQLWatchmanClient.js.flow +0 -111
  35. package/core/IR.js.flow +0 -326
  36. package/core/IRPrinter.js.flow +0 -478
  37. package/core/IRTransformer.js.flow +0 -377
  38. package/core/IRValidator.js.flow +0 -260
  39. package/core/IRVisitor.js.flow +0 -150
  40. package/core/JSModuleParser.js.flow +0 -24
  41. package/core/RelayCompilerScope.js.flow +0 -199
  42. package/core/RelayFindGraphQLTags.js.flow +0 -119
  43. package/core/RelayGraphQLEnumsGenerator.js.flow +0 -55
  44. package/core/RelayIRTransforms.js.flow +0 -138
  45. package/core/RelayParser.js.flow +0 -1734
  46. package/core/RelaySourceModuleParser.js.flow +0 -135
  47. package/core/Schema.js.flow +0 -2037
  48. package/core/SchemaUtils.js.flow +0 -120
  49. package/core/filterContextForNode.js.flow +0 -50
  50. package/core/getFieldDefinition.js.flow +0 -156
  51. package/core/getIdentifierForArgumentValue.js.flow +0 -49
  52. package/core/getIdentifierForSelection.js.flow +0 -69
  53. package/core/getLiteralArgumentValues.js.flow +0 -32
  54. package/core/getNormalizationOperationName.js.flow +0 -19
  55. package/core/inferRootArgumentDefinitions.js.flow +0 -323
  56. package/index.js.flow +0 -200
  57. package/language/RelayLanguagePluginInterface.js.flow +0 -283
  58. package/language/javascript/FindGraphQLTags.js.flow +0 -137
  59. package/language/javascript/RelayFlowBabelFactories.js.flow +0 -176
  60. package/language/javascript/RelayFlowGenerator.js.flow +0 -1100
  61. package/language/javascript/RelayFlowTypeTransformers.js.flow +0 -184
  62. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +0 -34
  63. package/language/javascript/formatGeneratedModule.js.flow +0 -65
  64. package/lib/bin/RelayCompilerBin.js +0 -143
  65. package/lib/bin/RelayCompilerMain.js +0 -486
  66. package/lib/bin/__fixtures__/plugin-module.js +0 -16
  67. package/lib/codegen/CodegenDirectory.js +0 -336
  68. package/lib/codegen/CodegenRunner.js +0 -433
  69. package/lib/codegen/CodegenTypes.js +0 -11
  70. package/lib/codegen/CodegenWatcher.js +0 -271
  71. package/lib/codegen/NormalizationCodeGenerator.js +0 -480
  72. package/lib/codegen/ReaderCodeGenerator.js +0 -472
  73. package/lib/codegen/RelayCodeGenerator.js +0 -68
  74. package/lib/codegen/RelayFileWriter.js +0 -270
  75. package/lib/codegen/SourceControl.js +0 -60
  76. package/lib/codegen/compileRelayArtifacts.js +0 -157
  77. package/lib/codegen/createPrintRequireModuleDependency.js +0 -19
  78. package/lib/codegen/sortObjectByKey.js +0 -41
  79. package/lib/codegen/writeRelayGeneratedFile.js +0 -208
  80. package/lib/core/ASTCache.js +0 -70
  81. package/lib/core/ASTConvert.js +0 -198
  82. package/lib/core/CompilerContext.js +0 -165
  83. package/lib/core/CompilerError.js +0 -251
  84. package/lib/core/DotGraphQLParser.js +0 -40
  85. package/lib/core/GraphQLCompilerProfiler.js +0 -299
  86. package/lib/core/GraphQLDerivedFromMetadata.js +0 -31
  87. package/lib/core/GraphQLWatchmanClient.js +0 -150
  88. package/lib/core/IR.js +0 -11
  89. package/lib/core/IRPrinter.js +0 -389
  90. package/lib/core/IRTransformer.js +0 -345
  91. package/lib/core/IRValidator.js +0 -226
  92. package/lib/core/IRVisitor.js +0 -45
  93. package/lib/core/JSModuleParser.js +0 -18
  94. package/lib/core/RelayCompilerScope.js +0 -149
  95. package/lib/core/RelayFindGraphQLTags.js +0 -79
  96. package/lib/core/RelayGraphQLEnumsGenerator.js +0 -50
  97. package/lib/core/RelayIRTransforms.js +0 -109
  98. package/lib/core/RelayParser.js +0 -1382
  99. package/lib/core/RelaySourceModuleParser.js +0 -104
  100. package/lib/core/Schema.js +0 -1877
  101. package/lib/core/SchemaUtils.js +0 -98
  102. package/lib/core/filterContextForNode.js +0 -49
  103. package/lib/core/getFieldDefinition.js +0 -145
  104. package/lib/core/getIdentifierForArgumentValue.js +0 -53
  105. package/lib/core/getIdentifierForSelection.js +0 -48
  106. package/lib/core/getLiteralArgumentValues.js +0 -26
  107. package/lib/core/getNormalizationOperationName.js +0 -17
  108. package/lib/core/inferRootArgumentDefinitions.js +0 -351
  109. package/lib/index.js +0 -178
  110. package/lib/language/RelayLanguagePluginInterface.js +0 -14
  111. package/lib/language/javascript/FindGraphQLTags.js +0 -126
  112. package/lib/language/javascript/RelayFlowBabelFactories.js +0 -160
  113. package/lib/language/javascript/RelayFlowGenerator.js +0 -857
  114. package/lib/language/javascript/RelayFlowTypeTransformers.js +0 -119
  115. package/lib/language/javascript/RelayLanguagePluginJavaScript.js +0 -30
  116. package/lib/language/javascript/formatGeneratedModule.js +0 -36
  117. package/lib/reporters/ConsoleReporter.js +0 -61
  118. package/lib/reporters/MultiReporter.js +0 -45
  119. package/lib/reporters/Reporter.js +0 -11
  120. package/lib/runner/Artifacts.js +0 -323
  121. package/lib/runner/BufferedFilesystem.js +0 -261
  122. package/lib/runner/GraphQLASTNodeGroup.js +0 -256
  123. package/lib/runner/GraphQLASTUtils.js +0 -23
  124. package/lib/runner/GraphQLNodeMap.js +0 -81
  125. package/lib/runner/Sources.js +0 -271
  126. package/lib/runner/StrictMap.js +0 -134
  127. package/lib/runner/compileArtifacts.js +0 -39
  128. package/lib/runner/extractAST.js +0 -77
  129. package/lib/runner/getChangedNodeNames.js +0 -82
  130. package/lib/runner/getSchemaInstance.js +0 -30
  131. package/lib/runner/types.js +0 -12
  132. package/lib/transforms/ApplyFragmentArgumentTransform.js +0 -393
  133. package/lib/transforms/ClientExtensionsTransform.js +0 -222
  134. package/lib/transforms/ConnectionTransform.js +0 -643
  135. package/lib/transforms/DeclarativeConnectionMutationTransform.js +0 -221
  136. package/lib/transforms/DeferStreamTransform.js +0 -247
  137. package/lib/transforms/DisallowIdAsAlias.js +0 -41
  138. package/lib/transforms/DisallowTypenameOnRoot.js +0 -53
  139. package/lib/transforms/FieldHandleTransform.js +0 -81
  140. package/lib/transforms/FilterCompilerDirectivesTransform.js +0 -29
  141. package/lib/transforms/FilterDirectivesTransform.js +0 -41
  142. package/lib/transforms/FlattenTransform.js +0 -308
  143. package/lib/transforms/GenerateIDFieldTransform.js +0 -137
  144. package/lib/transforms/GenerateTypeNameTransform.js +0 -155
  145. package/lib/transforms/InlineDataFragmentTransform.js +0 -104
  146. package/lib/transforms/InlineFragmentsTransform.js +0 -63
  147. package/lib/transforms/MaskTransform.js +0 -121
  148. package/lib/transforms/MatchTransform.js +0 -438
  149. package/lib/transforms/ReactFlightComponentTransform.js +0 -161
  150. package/lib/transforms/RefetchableFragmentTransform.js +0 -249
  151. package/lib/transforms/RelayDirectiveTransform.js +0 -85
  152. package/lib/transforms/RequiredFieldTransform.js +0 -373
  153. package/lib/transforms/SkipClientExtensionsTransform.js +0 -49
  154. package/lib/transforms/SkipHandleFieldTransform.js +0 -45
  155. package/lib/transforms/SkipRedundantNodesTransform.js +0 -255
  156. package/lib/transforms/SkipSplitOperationTransform.js +0 -32
  157. package/lib/transforms/SkipUnreachableNodeTransform.js +0 -158
  158. package/lib/transforms/SkipUnusedVariablesTransform.js +0 -74
  159. package/lib/transforms/SplitModuleImportTransform.js +0 -85
  160. package/lib/transforms/TestOperationTransform.js +0 -145
  161. package/lib/transforms/TransformUtils.js +0 -21
  162. package/lib/transforms/ValidateGlobalVariablesTransform.js +0 -91
  163. package/lib/transforms/ValidateRequiredArgumentsTransform.js +0 -118
  164. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +0 -111
  165. package/lib/transforms/ValidateUnusedVariablesTransform.js +0 -96
  166. package/lib/transforms/query-generators/FetchableQueryGenerator.js +0 -157
  167. package/lib/transforms/query-generators/NodeQueryGenerator.js +0 -166
  168. package/lib/transforms/query-generators/QueryQueryGenerator.js +0 -48
  169. package/lib/transforms/query-generators/ViewerQueryGenerator.js +0 -77
  170. package/lib/transforms/query-generators/index.js +0 -60
  171. package/lib/transforms/query-generators/utils.js +0 -92
  172. package/lib/util/CodeMarker.js +0 -80
  173. package/lib/util/DefaultHandleKey.js +0 -15
  174. package/lib/util/RelayCompilerCache.js +0 -98
  175. package/lib/util/Rollout.js +0 -40
  176. package/lib/util/TimeReporter.js +0 -83
  177. package/lib/util/areEqualArgValues.js +0 -135
  178. package/lib/util/argumentContainsVariables.js +0 -37
  179. package/lib/util/dedupeJSONStringify.js +0 -160
  180. package/lib/util/generateAbstractTypeRefinementKey.js +0 -24
  181. package/lib/util/getDefinitionNodeHash.js +0 -22
  182. package/lib/util/getModuleName.js +0 -32
  183. package/lib/util/joinArgumentDefinitions.js +0 -66
  184. package/lib/util/md5.js +0 -17
  185. package/lib/util/murmurHash.js +0 -86
  186. package/lib/util/nullthrowsOSS.js +0 -23
  187. package/lib/util/orList.js +0 -36
  188. package/lib/util/partitionArray.js +0 -35
  189. package/relay-compiler.js +0 -17
  190. package/relay-compiler.min.js +0 -22
  191. package/reporters/ConsoleReporter.js.flow +0 -81
  192. package/reporters/MultiReporter.js.flow +0 -43
  193. package/reporters/Reporter.js.flow +0 -19
  194. package/runner/Artifacts.js.flow +0 -219
  195. package/runner/BufferedFilesystem.js.flow +0 -194
  196. package/runner/GraphQLASTNodeGroup.js.flow +0 -176
  197. package/runner/GraphQLASTUtils.js.flow +0 -26
  198. package/runner/GraphQLNodeMap.js.flow +0 -55
  199. package/runner/Sources.js.flow +0 -228
  200. package/runner/StrictMap.js.flow +0 -96
  201. package/runner/compileArtifacts.js.flow +0 -76
  202. package/runner/extractAST.js.flow +0 -100
  203. package/runner/getChangedNodeNames.js.flow +0 -48
  204. package/runner/getSchemaInstance.js.flow +0 -36
  205. package/runner/types.js.flow +0 -37
  206. package/transforms/ApplyFragmentArgumentTransform.js.flow +0 -526
  207. package/transforms/ClientExtensionsTransform.js.flow +0 -226
  208. package/transforms/ConnectionTransform.js.flow +0 -859
  209. package/transforms/DeclarativeConnectionMutationTransform.js.flow +0 -250
  210. package/transforms/DeferStreamTransform.js.flow +0 -266
  211. package/transforms/DisallowIdAsAlias.js.flow +0 -48
  212. package/transforms/DisallowTypenameOnRoot.js.flow +0 -45
  213. package/transforms/FieldHandleTransform.js.flow +0 -81
  214. package/transforms/FilterCompilerDirectivesTransform.js.flow +0 -33
  215. package/transforms/FilterDirectivesTransform.js.flow +0 -45
  216. package/transforms/FlattenTransform.js.flow +0 -462
  217. package/transforms/GenerateIDFieldTransform.js.flow +0 -154
  218. package/transforms/GenerateTypeNameTransform.js.flow +0 -167
  219. package/transforms/InlineDataFragmentTransform.js.flow +0 -129
  220. package/transforms/InlineFragmentsTransform.js.flow +0 -73
  221. package/transforms/MaskTransform.js.flow +0 -130
  222. package/transforms/MatchTransform.js.flow +0 -593
  223. package/transforms/ReactFlightComponentTransform.js.flow +0 -198
  224. package/transforms/RefetchableFragmentTransform.js.flow +0 -272
  225. package/transforms/RelayDirectiveTransform.js.flow +0 -99
  226. package/transforms/RequiredFieldTransform.js.flow +0 -419
  227. package/transforms/SkipClientExtensionsTransform.js.flow +0 -57
  228. package/transforms/SkipHandleFieldTransform.js.flow +0 -45
  229. package/transforms/SkipRedundantNodesTransform.js.flow +0 -259
  230. package/transforms/SkipSplitOperationTransform.js.flow +0 -37
  231. package/transforms/SkipUnreachableNodeTransform.js.flow +0 -149
  232. package/transforms/SkipUnusedVariablesTransform.js.flow +0 -59
  233. package/transforms/SplitModuleImportTransform.js.flow +0 -101
  234. package/transforms/TestOperationTransform.js.flow +0 -143
  235. package/transforms/TransformUtils.js.flow +0 -26
  236. package/transforms/ValidateGlobalVariablesTransform.js.flow +0 -81
  237. package/transforms/ValidateRequiredArgumentsTransform.js.flow +0 -131
  238. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +0 -115
  239. package/transforms/ValidateUnusedVariablesTransform.js.flow +0 -89
  240. package/transforms/query-generators/FetchableQueryGenerator.js.flow +0 -189
  241. package/transforms/query-generators/NodeQueryGenerator.js.flow +0 -219
  242. package/transforms/query-generators/QueryQueryGenerator.js.flow +0 -57
  243. package/transforms/query-generators/ViewerQueryGenerator.js.flow +0 -97
  244. package/transforms/query-generators/index.js.flow +0 -90
  245. package/transforms/query-generators/utils.js.flow +0 -76
  246. package/util/CodeMarker.js.flow +0 -79
  247. package/util/DefaultHandleKey.js.flow +0 -17
  248. package/util/RelayCompilerCache.js.flow +0 -88
  249. package/util/Rollout.js.flow +0 -39
  250. package/util/TimeReporter.js.flow +0 -79
  251. package/util/areEqualArgValues.js.flow +0 -126
  252. package/util/argumentContainsVariables.js.flow +0 -38
  253. package/util/dedupeJSONStringify.js.flow +0 -152
  254. package/util/generateAbstractTypeRefinementKey.js.flow +0 -29
  255. package/util/getDefinitionNodeHash.js.flow +0 -25
  256. package/util/getModuleName.js.flow +0 -39
  257. package/util/joinArgumentDefinitions.js.flow +0 -105
  258. package/util/md5.js.flow +0 -22
  259. package/util/murmurHash.js.flow +0 -94
  260. package/util/nullthrowsOSS.js.flow +0 -25
  261. package/util/orList.js.flow +0 -37
  262. package/util/partitionArray.js.flow +0 -37
@@ -1,33 +0,0 @@
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
- };
@@ -1,45 +0,0 @@
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
- };
@@ -1,462 +0,0 @@
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
- };
@@ -1,154 +0,0 @@
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
- };