relay-compiler 11.0.0 → 13.0.0-rc.0

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