relay-compiler 0.0.0-main-8ff54d69 → 0.0.0-main-38f1c96e

Sign up to get free protection for your applications and to get access to all the features.
Files changed (263) 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 -24
  6. package/win-x64/relay.exe +0 -0
  7. package/bin/RelayCompilerBin.js.flow +0 -168
  8. package/bin/RelayCompilerMain.js.flow +0 -515
  9. package/bin/__fixtures__/plugin-module.js.flow +0 -17
  10. package/bin/relay-compiler +0 -19068
  11. package/codegen/CodegenDirectory.js.flow +0 -372
  12. package/codegen/CodegenRunner.js.flow +0 -424
  13. package/codegen/CodegenTypes.js.flow +0 -28
  14. package/codegen/CodegenWatcher.js.flow +0 -250
  15. package/codegen/NormalizationCodeGenerator.js.flow +0 -566
  16. package/codegen/ReaderCodeGenerator.js.flow +0 -510
  17. package/codegen/RelayCodeGenerator.js.flow +0 -85
  18. package/codegen/RelayFileWriter.js.flow +0 -365
  19. package/codegen/SourceControl.js.flow +0 -58
  20. package/codegen/compileRelayArtifacts.js.flow +0 -181
  21. package/codegen/createPrintRequireModuleDependency.js.flow +0 -19
  22. package/codegen/sortObjectByKey.js.flow +0 -25
  23. package/codegen/writeRelayGeneratedFile.js.flow +0 -235
  24. package/core/ASTCache.js.flow +0 -73
  25. package/core/ASTConvert.js.flow +0 -230
  26. package/core/CompilerContext.js.flow +0 -189
  27. package/core/CompilerError.js.flow +0 -255
  28. package/core/DotGraphQLParser.js.flow +0 -37
  29. package/core/GraphQLCompilerProfiler.js.flow +0 -341
  30. package/core/GraphQLDerivedFromMetadata.js.flow +0 -36
  31. package/core/GraphQLWatchmanClient.js.flow +0 -109
  32. package/core/IR.js.flow +0 -326
  33. package/core/IRPrinter.js.flow +0 -472
  34. package/core/IRTransformer.js.flow +0 -376
  35. package/core/IRValidator.js.flow +0 -259
  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 -118
  40. package/core/RelayGraphQLEnumsGenerator.js.flow +0 -55
  41. package/core/RelayIRTransforms.js.flow +0 -138
  42. package/core/RelayParser.js.flow +0 -1741
  43. package/core/RelaySourceModuleParser.js.flow +0 -133
  44. package/core/Schema.js.flow +0 -2035
  45. package/core/SchemaUtils.js.flow +0 -120
  46. package/core/filterContextForNode.js.flow +0 -49
  47. package/core/getFieldDefinition.js.flow +0 -156
  48. package/core/getIdentifierForArgumentValue.js.flow +0 -49
  49. package/core/getIdentifierForSelection.js.flow +0 -68
  50. package/core/getLiteralArgumentValues.js.flow +0 -32
  51. package/core/getNormalizationOperationName.js.flow +0 -19
  52. package/core/inferRootArgumentDefinitions.js.flow +0 -322
  53. package/index.js.flow +0 -198
  54. package/language/RelayLanguagePluginInterface.js.flow +0 -283
  55. package/language/javascript/FindGraphQLTags.js.flow +0 -136
  56. package/language/javascript/RelayFlowBabelFactories.js.flow +0 -176
  57. package/language/javascript/RelayFlowGenerator.js.flow +0 -1096
  58. package/language/javascript/RelayFlowTypeTransformers.js.flow +0 -181
  59. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +0 -33
  60. package/language/javascript/formatGeneratedModule.js.flow +0 -65
  61. package/lib/bin/RelayCompilerBin.js +0 -143
  62. package/lib/bin/RelayCompilerMain.js +0 -488
  63. package/lib/bin/__fixtures__/plugin-module.js +0 -16
  64. package/lib/codegen/CodegenDirectory.js +0 -335
  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 -487
  69. package/lib/codegen/ReaderCodeGenerator.js +0 -473
  70. package/lib/codegen/RelayCodeGenerator.js +0 -75
  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 -206
  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 -252
  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 -388
  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 -183
  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 -1381
  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 -50
  100. package/lib/core/getFieldDefinition.js +0 -145
  101. package/lib/core/getIdentifierForArgumentValue.js +0 -54
  102. package/lib/core/getIdentifierForSelection.js +0 -49
  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 -26
  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 -856
  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 -262
  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/test-utils/TestSchema.js +0 -27
  130. package/lib/test-utils/parseGraphQLText.js +0 -30
  131. package/lib/transforms/ApplyFragmentArgumentTransform.js +0 -393
  132. package/lib/transforms/ClientExtensionsTransform.js +0 -221
  133. package/lib/transforms/ConnectionTransform.js +0 -639
  134. package/lib/transforms/DeclarativeConnectionMutationTransform.js +0 -218
  135. package/lib/transforms/DeferStreamTransform.js +0 -246
  136. package/lib/transforms/DisallowIdAsAlias.js +0 -40
  137. package/lib/transforms/DisallowTypenameOnRoot.js +0 -53
  138. package/lib/transforms/FieldHandleTransform.js +0 -79
  139. package/lib/transforms/FilterCompilerDirectivesTransform.js +0 -29
  140. package/lib/transforms/FilterDirectivesTransform.js +0 -42
  141. package/lib/transforms/FlattenTransform.js +0 -306
  142. package/lib/transforms/GenerateIDFieldTransform.js +0 -135
  143. package/lib/transforms/GenerateTypeNameTransform.js +0 -149
  144. package/lib/transforms/InlineDataFragmentTransform.js +0 -100
  145. package/lib/transforms/InlineFragmentsTransform.js +0 -61
  146. package/lib/transforms/MaskTransform.js +0 -117
  147. package/lib/transforms/MatchTransform.js +0 -434
  148. package/lib/transforms/ReactFlightComponentTransform.js +0 -158
  149. package/lib/transforms/RefetchableFragmentTransform.js +0 -249
  150. package/lib/transforms/RelayDirectiveTransform.js +0 -83
  151. package/lib/transforms/RequiredFieldTransform.js +0 -369
  152. package/lib/transforms/SkipClientExtensionsTransform.js +0 -46
  153. package/lib/transforms/SkipHandleFieldTransform.js +0 -45
  154. package/lib/transforms/SkipRedundantNodesTransform.js +0 -261
  155. package/lib/transforms/SkipSplitOperationTransform.js +0 -32
  156. package/lib/transforms/SkipUnreachableNodeTransform.js +0 -158
  157. package/lib/transforms/SkipUnusedVariablesTransform.js +0 -75
  158. package/lib/transforms/SplitModuleImportTransform.js +0 -82
  159. package/lib/transforms/TestOperationTransform.js +0 -144
  160. package/lib/transforms/TransformUtils.js +0 -21
  161. package/lib/transforms/ValidateGlobalVariablesTransform.js +0 -92
  162. package/lib/transforms/ValidateRequiredArgumentsTransform.js +0 -114
  163. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +0 -108
  164. package/lib/transforms/ValidateUnusedVariablesTransform.js +0 -96
  165. package/lib/transforms/query-generators/FetchableQueryGenerator.js +0 -157
  166. package/lib/transforms/query-generators/NodeQueryGenerator.js +0 -166
  167. package/lib/transforms/query-generators/QueryQueryGenerator.js +0 -48
  168. package/lib/transforms/query-generators/ViewerQueryGenerator.js +0 -77
  169. package/lib/transforms/query-generators/index.js +0 -60
  170. package/lib/transforms/query-generators/utils.js +0 -92
  171. package/lib/util/CodeMarker.js +0 -80
  172. package/lib/util/DefaultHandleKey.js +0 -15
  173. package/lib/util/RelayCompilerCache.js +0 -97
  174. package/lib/util/Rollout.js +0 -40
  175. package/lib/util/TimeReporter.js +0 -83
  176. package/lib/util/areEqualArgValues.js +0 -135
  177. package/lib/util/argumentContainsVariables.js +0 -37
  178. package/lib/util/dedupeJSONStringify.js +0 -160
  179. package/lib/util/generateAbstractTypeRefinementKey.js +0 -24
  180. package/lib/util/getDefinitionNodeHash.js +0 -22
  181. package/lib/util/getModuleName.js +0 -32
  182. package/lib/util/joinArgumentDefinitions.js +0 -67
  183. package/lib/util/md5.js +0 -17
  184. package/lib/util/murmurHash.js +0 -86
  185. package/lib/util/nullthrowsOSS.js +0 -23
  186. package/lib/util/orList.js +0 -36
  187. package/lib/util/partitionArray.js +0 -35
  188. package/relay-compiler.js +0 -17
  189. package/relay-compiler.min.js +0 -22
  190. package/reporters/ConsoleReporter.js.flow +0 -81
  191. package/reporters/MultiReporter.js.flow +0 -43
  192. package/reporters/Reporter.js.flow +0 -19
  193. package/runner/Artifacts.js.flow +0 -215
  194. package/runner/BufferedFilesystem.js.flow +0 -194
  195. package/runner/GraphQLASTNodeGroup.js.flow +0 -174
  196. package/runner/GraphQLASTUtils.js.flow +0 -26
  197. package/runner/GraphQLNodeMap.js.flow +0 -55
  198. package/runner/Sources.js.flow +0 -227
  199. package/runner/StrictMap.js.flow +0 -96
  200. package/runner/compileArtifacts.js.flow +0 -75
  201. package/runner/extractAST.js.flow +0 -98
  202. package/runner/getChangedNodeNames.js.flow +0 -48
  203. package/runner/getSchemaInstance.js.flow +0 -36
  204. package/runner/types.js.flow +0 -37
  205. package/test-utils/TestSchema.js.flow +0 -30
  206. package/test-utils/parseGraphQLText.js.flow +0 -41
  207. package/transforms/ApplyFragmentArgumentTransform.js.flow +0 -524
  208. package/transforms/ClientExtensionsTransform.js.flow +0 -224
  209. package/transforms/ConnectionTransform.js.flow +0 -850
  210. package/transforms/DeclarativeConnectionMutationTransform.js.flow +0 -245
  211. package/transforms/DeferStreamTransform.js.flow +0 -263
  212. package/transforms/DisallowIdAsAlias.js.flow +0 -46
  213. package/transforms/DisallowTypenameOnRoot.js.flow +0 -44
  214. package/transforms/FieldHandleTransform.js.flow +0 -77
  215. package/transforms/FilterCompilerDirectivesTransform.js.flow +0 -33
  216. package/transforms/FilterDirectivesTransform.js.flow +0 -45
  217. package/transforms/FlattenTransform.js.flow +0 -458
  218. package/transforms/GenerateIDFieldTransform.js.flow +0 -151
  219. package/transforms/GenerateTypeNameTransform.js.flow +0 -159
  220. package/transforms/InlineDataFragmentTransform.js.flow +0 -123
  221. package/transforms/InlineFragmentsTransform.js.flow +0 -70
  222. package/transforms/MaskTransform.js.flow +0 -124
  223. package/transforms/MatchTransform.js.flow +0 -587
  224. package/transforms/ReactFlightComponentTransform.js.flow +0 -207
  225. package/transforms/RefetchableFragmentTransform.js.flow +0 -266
  226. package/transforms/RelayDirectiveTransform.js.flow +0 -96
  227. package/transforms/RequiredFieldTransform.js.flow +0 -413
  228. package/transforms/SkipClientExtensionsTransform.js.flow +0 -54
  229. package/transforms/SkipHandleFieldTransform.js.flow +0 -44
  230. package/transforms/SkipRedundantNodesTransform.js.flow +0 -277
  231. package/transforms/SkipSplitOperationTransform.js.flow +0 -37
  232. package/transforms/SkipUnreachableNodeTransform.js.flow +0 -148
  233. package/transforms/SkipUnusedVariablesTransform.js.flow +0 -59
  234. package/transforms/SplitModuleImportTransform.js.flow +0 -97
  235. package/transforms/TestOperationTransform.js.flow +0 -142
  236. package/transforms/TransformUtils.js.flow +0 -26
  237. package/transforms/ValidateGlobalVariablesTransform.js.flow +0 -80
  238. package/transforms/ValidateRequiredArgumentsTransform.js.flow +0 -130
  239. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +0 -128
  240. package/transforms/ValidateUnusedVariablesTransform.js.flow +0 -88
  241. package/transforms/query-generators/FetchableQueryGenerator.js.flow +0 -188
  242. package/transforms/query-generators/NodeQueryGenerator.js.flow +0 -217
  243. package/transforms/query-generators/QueryQueryGenerator.js.flow +0 -57
  244. package/transforms/query-generators/ViewerQueryGenerator.js.flow +0 -97
  245. package/transforms/query-generators/index.js.flow +0 -89
  246. package/transforms/query-generators/utils.js.flow +0 -76
  247. package/util/CodeMarker.js.flow +0 -79
  248. package/util/DefaultHandleKey.js.flow +0 -17
  249. package/util/RelayCompilerCache.js.flow +0 -86
  250. package/util/Rollout.js.flow +0 -39
  251. package/util/TimeReporter.js.flow +0 -79
  252. package/util/areEqualArgValues.js.flow +0 -126
  253. package/util/argumentContainsVariables.js.flow +0 -38
  254. package/util/dedupeJSONStringify.js.flow +0 -156
  255. package/util/generateAbstractTypeRefinementKey.js.flow +0 -29
  256. package/util/getDefinitionNodeHash.js.flow +0 -24
  257. package/util/getModuleName.js.flow +0 -39
  258. package/util/joinArgumentDefinitions.js.flow +0 -105
  259. package/util/md5.js.flow +0 -19
  260. package/util/murmurHash.js.flow +0 -94
  261. package/util/nullthrowsOSS.js.flow +0 -25
  262. package/util/orList.js.flow +0 -37
  263. package/util/partitionArray.js.flow +0 -37
@@ -1,566 +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
- import type {ScalarField} from '../core/IR';
15
-
16
- import type {
17
- Argument,
18
- ArgumentValue,
19
- ClientExtension,
20
- Condition,
21
- Defer,
22
- InlineFragment,
23
- LinkedField,
24
- LocalArgumentDefinition,
25
- Metadata,
26
- ModuleImport,
27
- Root,
28
- Selection,
29
- SplitOperation,
30
- Stream,
31
- } from '../core/IR';
32
- import type {Schema, TypeID} from '../core/Schema';
33
- import type {
34
- NormalizationArgument,
35
- NormalizationDefer,
36
- NormalizationField,
37
- NormalizationLinkedField,
38
- NormalizationLinkedHandle,
39
- NormalizationLocalArgumentDefinition,
40
- NormalizationModuleImport,
41
- NormalizationOperation,
42
- NormalizationSelection,
43
- NormalizationSplitOperation,
44
- NormalizationStream,
45
- NormalizationTypeDiscriminator,
46
- } from 'relay-runtime';
47
-
48
- const {createCompilerError, createUserError} = require('../core/CompilerError');
49
- const argumentContainsVariables = require('../util/argumentContainsVariables');
50
- const generateAbstractTypeRefinementKey = require('../util/generateAbstractTypeRefinementKey');
51
- const partitionArray = require('../util/partitionArray');
52
- const sortObjectByKey = require('./sortObjectByKey');
53
- const {getStorageKey, stableCopy} = require('relay-runtime');
54
-
55
- /**
56
- * @public
57
- *
58
- * Converts an IR node into a plain JS object representation that can be
59
- * used at runtime.
60
- */
61
- declare function generate(schema: Schema, node: Root): NormalizationOperation;
62
- declare function generate(
63
- schema: Schema,
64
- node: SplitOperation,
65
- ): NormalizationSplitOperation;
66
- function generate(
67
- schema: Schema,
68
- node: Root | SplitOperation,
69
- ): NormalizationOperation | NormalizationSplitOperation {
70
- switch (node.kind) {
71
- case 'Root':
72
- return generateRoot(schema, node);
73
- case 'SplitOperation':
74
- return generateSplitOperation(schema, node);
75
- default:
76
- throw createCompilerError(
77
- `NormalizationCodeGenerator: Unsupported AST kind '${node.kind}'.`,
78
- [node.loc],
79
- );
80
- }
81
- }
82
-
83
- function generateRoot(schema: Schema, node: Root): NormalizationOperation {
84
- return {
85
- argumentDefinitions: generateArgumentDefinitions(
86
- schema,
87
- node.argumentDefinitions,
88
- ),
89
- kind: 'Operation',
90
- name: node.name,
91
- selections: generateSelections(schema, node.selections),
92
- };
93
- }
94
-
95
- function generateSplitOperation(
96
- schema: Schema,
97
- node: SplitOperation,
98
- ): NormalizationSplitOperation {
99
- return {
100
- kind: 'SplitOperation',
101
- metadata: sortObjectByKey(node.metadata),
102
- name: node.name,
103
- selections: generateSelections(schema, node.selections),
104
- };
105
- }
106
-
107
- function generateSelections(
108
- schema: Schema,
109
- selections: $ReadOnlyArray<Selection>,
110
- ): $ReadOnlyArray<NormalizationSelection> {
111
- const normalizationSelections: Array<NormalizationSelection> = [];
112
- selections.forEach(selection => {
113
- switch (selection.kind) {
114
- case 'Condition':
115
- normalizationSelections.push(generateCondition(schema, selection));
116
- break;
117
- case 'ClientExtension':
118
- normalizationSelections.push(
119
- generateClientExtension(schema, selection),
120
- );
121
- break;
122
- case 'ScalarField':
123
- // NOTE: Inline fragments in normalization ast have the abstractKey
124
- // but we skip the corresponding ScalarField for the type discriminator
125
- // selection, since it's guaranteed to be a duplicate of a parent __typename
126
- // selection.
127
- const abstractKey = selection.metadata?.abstractKey;
128
- if (typeof abstractKey === 'string') {
129
- normalizationSelections.push(generateTypeDiscriminator(abstractKey));
130
- } else {
131
- normalizationSelections.push(...generateScalarField(selection));
132
- }
133
- break;
134
- case 'ModuleImport':
135
- normalizationSelections.push(generateModuleImport(selection));
136
- break;
137
- case 'InlineFragment':
138
- normalizationSelections.push(generateInlineFragment(schema, selection));
139
- break;
140
- case 'LinkedField':
141
- normalizationSelections.push(...generateLinkedField(schema, selection));
142
- break;
143
- case 'Defer':
144
- normalizationSelections.push(generateDefer(schema, selection));
145
- break;
146
- case 'Stream':
147
- normalizationSelections.push(generateStream(schema, selection));
148
- break;
149
- case 'InlineDataFragmentSpread':
150
- case 'FragmentSpread':
151
- throw new createCompilerError(
152
- `NormalizationCodeGenerator: Unexpected IR node ${selection.kind}.`,
153
- [selection.loc],
154
- );
155
- default:
156
- (selection: empty);
157
- throw new Error();
158
- }
159
- });
160
- return normalizationSelections;
161
- }
162
-
163
- function generateArgumentDefinitions(
164
- schema: Schema,
165
- nodes: $ReadOnlyArray<LocalArgumentDefinition>,
166
- ): $ReadOnlyArray<NormalizationLocalArgumentDefinition> {
167
- return nodes.map(node => {
168
- return {
169
- defaultValue: stableCopy(node.defaultValue),
170
- kind: 'LocalArgument',
171
- name: node.name,
172
- };
173
- });
174
- }
175
-
176
- function generateClientExtension(
177
- schema: Schema,
178
- node: ClientExtension,
179
- ): NormalizationSelection {
180
- return {
181
- kind: 'ClientExtension',
182
- selections: generateSelections(schema, node.selections),
183
- };
184
- }
185
-
186
- function generateCondition(
187
- schema: Schema,
188
- node: Condition,
189
- ): NormalizationSelection {
190
- if (node.condition.kind !== 'Variable') {
191
- throw createCompilerError(
192
- "NormalizationCodeGenerator: Expected 'Condition' with static " +
193
- 'value to be pruned or inlined',
194
- [node.condition.loc],
195
- );
196
- }
197
- return {
198
- condition: node.condition.variableName,
199
- kind: 'Condition',
200
- passingValue: node.passingValue,
201
- selections: generateSelections(schema, node.selections),
202
- };
203
- }
204
-
205
- function generateDefer(schema: Schema, node: Defer): NormalizationDefer {
206
- if (
207
- !(
208
- node.if == null ||
209
- node.if.kind === 'Variable' ||
210
- (node.if.kind === 'Literal' && node.if.value === true)
211
- )
212
- ) {
213
- throw createCompilerError(
214
- 'NormalizationCodeGenerator: Expected @defer `if` condition to be ' +
215
- 'a variable, unspecified, or the literal `true`.',
216
- [node.if?.loc ?? node.loc],
217
- );
218
- }
219
- return {
220
- if:
221
- node.if != null && node.if.kind === 'Variable'
222
- ? node.if.variableName
223
- : null,
224
- kind: 'Defer',
225
- label: node.label,
226
- selections: generateSelections(schema, node.selections),
227
- };
228
- }
229
-
230
- function generateInlineFragment(
231
- schema: Schema,
232
- node: InlineFragment,
233
- ): NormalizationSelection {
234
- const rawType = schema.getRawType(node.typeCondition);
235
- const isAbstractType = schema.isAbstractType(rawType);
236
- const abstractKey = isAbstractType
237
- ? generateAbstractTypeRefinementKey(schema, rawType)
238
- : null;
239
- let selections = generateSelections(schema, node.selections);
240
-
241
- if (isAbstractType) {
242
- // Maintain a few invariants:
243
- // - InlineFragment (and `selections` arrays generally) cannot be empty
244
- // - Don't emit a TypeDiscriminator under an InlineFragment unless it has
245
- // a different abstractKey
246
- // This means we have to handle two cases:
247
- // - The inline fragment only contains a TypeDiscriminator with the same
248
- // abstractKey: replace the Fragment w the Discriminator
249
- // - The inline fragment contains other selections: return all the selections
250
- // minus any Discriminators w the same key
251
- const [discriminators, otherSelections] = partitionArray(
252
- selections,
253
- selection =>
254
- selection.kind === 'TypeDiscriminator' &&
255
- selection.abstractKey === abstractKey,
256
- );
257
- const discriminator = discriminators[0];
258
- if (discriminator != null && otherSelections.length === 0) {
259
- return discriminator;
260
- } else {
261
- selections = otherSelections;
262
- }
263
- }
264
-
265
- return {
266
- kind: 'InlineFragment',
267
- selections,
268
- type: schema.getTypeString(rawType),
269
- abstractKey,
270
- };
271
- }
272
-
273
- function generateLinkedField(
274
- schema: Schema,
275
- node: LinkedField,
276
- ): $ReadOnlyArray<NormalizationSelection> {
277
- // Note: it is important that the arguments of this field be sorted to
278
- // ensure stable generation of storage keys for equivalent arguments
279
- // which may have originally appeared in different orders across an app.
280
- const handles =
281
- (node.handles &&
282
- node.handles.map(handle => {
283
- let handleNode: NormalizationLinkedHandle = {
284
- alias: node.alias === node.name ? null : node.alias,
285
- args: generateArgs(node.args),
286
- filters: handle.filters,
287
- handle: handle.name,
288
- key: handle.key,
289
- kind: 'LinkedHandle',
290
- name: node.name,
291
- };
292
- // NOTE: this intentionally adds a dynamic key in order to avoid
293
- // triggering updates to existing queries that do not use dynamic
294
- // keys.
295
- if (handle.dynamicKey != null) {
296
- const dynamicKeyArgName = '__dynamicKey';
297
- handleNode = {
298
- ...handleNode,
299
- dynamicKey: {
300
- kind: 'Variable',
301
- name: dynamicKeyArgName,
302
- variableName: handle.dynamicKey.variableName,
303
- },
304
- };
305
- }
306
- if (handle.handleArgs != null) {
307
- const handleArgs = generateArgs(handle.handleArgs);
308
- if (handleArgs != null) {
309
- handleNode = {
310
- ...handleNode,
311
- handleArgs,
312
- };
313
- }
314
- }
315
- return handleNode;
316
- })) ||
317
- [];
318
- const type = schema.getRawType(node.type);
319
- let field: NormalizationLinkedField = {
320
- alias: node.alias === node.name ? null : node.alias,
321
- args: generateArgs(node.args),
322
- concreteType: !schema.isAbstractType(type)
323
- ? schema.getTypeString(type)
324
- : null,
325
- kind: 'LinkedField',
326
- name: node.name,
327
- plural: isPlural(schema, node.type),
328
- selections: generateSelections(schema, node.selections),
329
- storageKey: null,
330
- };
331
- // Precompute storageKey if possible
332
- const storageKey = getStaticStorageKey(field, node.metadata);
333
- if (storageKey != null) {
334
- field = {...field, storageKey};
335
- }
336
- return [field].concat(handles);
337
- }
338
-
339
- function generateModuleImport(node: ModuleImport): NormalizationModuleImport {
340
- const fragmentName = node.name;
341
- const regExpMatch = fragmentName.match(
342
- /^([a-zA-Z][a-zA-Z0-9]*)(?:_([a-zA-Z][_a-zA-Z0-9]*))?$/,
343
- );
344
- if (!regExpMatch) {
345
- throw createCompilerError(
346
- 'NormalizationCodeGenerator: @module fragments should be named ' +
347
- `'FragmentName_propName', got '${fragmentName}'.`,
348
- [node.loc],
349
- );
350
- }
351
- const fragmentPropName = regExpMatch[2];
352
- if (typeof fragmentPropName !== 'string') {
353
- throw createCompilerError(
354
- 'NormalizationCodeGenerator: @module fragments should be named ' +
355
- `'FragmentName_propName', got '${fragmentName}'.`,
356
- [node.loc],
357
- );
358
- }
359
- return {
360
- args: null,
361
- documentName: node.key,
362
- fragmentName,
363
- fragmentPropName,
364
- kind: 'ModuleImport',
365
- };
366
- }
367
-
368
- function generateTypeDiscriminator(
369
- abstractKey: string,
370
- ): NormalizationTypeDiscriminator {
371
- return {
372
- kind: 'TypeDiscriminator',
373
- abstractKey,
374
- };
375
- }
376
-
377
- function generateScalarField(node: ScalarField): Array<NormalizationSelection> {
378
- // flowlint-next-line sketchy-null-mixed:off
379
- if (node.metadata?.skipNormalizationNode) {
380
- return [];
381
- }
382
- // Note: it is important that the arguments of this field be sorted to
383
- // ensure stable generation of storage keys for equivalent arguments
384
- // which may have originally appeared in different orders across an app.
385
- const handles =
386
- (node.handles &&
387
- node.handles.map(handle => {
388
- if (handle.dynamicKey != null) {
389
- throw createUserError(
390
- 'Dynamic key values are not supported on scalar fields.',
391
- [handle.dynamicKey.loc],
392
- );
393
- }
394
- const nodeHandle = {
395
- alias: node.alias === node.name ? null : node.alias,
396
- args: generateArgs(node.args),
397
- filters: handle.filters,
398
- handle: handle.name,
399
- key: handle.key,
400
- kind: 'ScalarHandle',
401
- name: node.name,
402
- };
403
-
404
- if (handle.handleArgs != null) {
405
- // $FlowFixMe handleArgs exists in Handle
406
- nodeHandle.handleArgs = generateArgs(handle.handleArgs);
407
- }
408
-
409
- return nodeHandle;
410
- })) ||
411
- [];
412
- let field = {
413
- alias: node.alias === node.name ? null : node.alias,
414
- args: generateArgs(node.args),
415
- kind: 'ScalarField',
416
- name: node.name,
417
- storageKey: null,
418
- };
419
- // Precompute storageKey if possible
420
- const storageKey = getStaticStorageKey(field, node.metadata);
421
- if (storageKey != null) {
422
- field = {...field, storageKey};
423
- }
424
- if (node.metadata?.flight === true) {
425
- field = {...field, kind: 'FlightField'};
426
- }
427
- return [field].concat(handles);
428
- }
429
-
430
- function generateStream(schema: Schema, node: Stream): NormalizationStream {
431
- if (
432
- !(
433
- node.if == null ||
434
- node.if.kind === 'Variable' ||
435
- (node.if.kind === 'Literal' && node.if.value === true)
436
- )
437
- ) {
438
- throw createCompilerError(
439
- 'NormalizationCodeGenerator: Expected @stream `if` condition to be ' +
440
- 'a variable, unspecified, or the literal `true`.',
441
- [node.if?.loc ?? node.loc],
442
- );
443
- }
444
- return {
445
- if:
446
- node.if != null && node.if.kind === 'Variable'
447
- ? node.if.variableName
448
- : null,
449
- kind: 'Stream',
450
- label: node.label,
451
- selections: generateSelections(schema, node.selections),
452
- };
453
- }
454
-
455
- function generateArgumentValue(
456
- name: string,
457
- value: ArgumentValue,
458
- ): NormalizationArgument | null {
459
- switch (value.kind) {
460
- case 'Variable':
461
- return {
462
- kind: 'Variable',
463
- name: name,
464
- variableName: value.variableName,
465
- };
466
- case 'Literal':
467
- return value.value === null
468
- ? null
469
- : {
470
- kind: 'Literal',
471
- name: name,
472
- value: stableCopy(value.value),
473
- };
474
- case 'ObjectValue': {
475
- const objectKeys = value.fields.map(field => field.name).sort();
476
- const objectValues = new Map(
477
- value.fields.map(field => {
478
- return [field.name, field.value];
479
- }),
480
- );
481
- return {
482
- fields: objectKeys.map(fieldName => {
483
- const fieldValue = objectValues.get(fieldName);
484
- if (fieldValue == null) {
485
- throw createCompilerError('Expected to have object field value');
486
- }
487
- return (
488
- generateArgumentValue(fieldName, fieldValue) ?? {
489
- kind: 'Literal',
490
- name: fieldName,
491
- value: null,
492
- }
493
- );
494
- }),
495
- kind: 'ObjectValue',
496
- name: name,
497
- };
498
- }
499
- case 'ListValue': {
500
- return {
501
- items: value.items.map((item, index) => {
502
- return generateArgumentValue(`${name}.${index}`, item);
503
- }),
504
- kind: 'ListValue',
505
- name: name,
506
- };
507
- }
508
- default:
509
- throw createUserError(
510
- 'NormalizationCodeGenerator: Complex argument values (Lists or ' +
511
- 'InputObjects with nested variables) are not supported.',
512
- [value.loc],
513
- );
514
- }
515
- }
516
-
517
- function generateArgs(
518
- args: $ReadOnlyArray<Argument>,
519
- ): ?$ReadOnlyArray<NormalizationArgument> {
520
- const concreteArguments = [];
521
- args.forEach(arg => {
522
- const concreteArgument = generateArgumentValue(arg.name, arg.value);
523
- if (concreteArgument !== null) {
524
- concreteArguments.push(concreteArgument);
525
- }
526
- });
527
- return concreteArguments.length === 0
528
- ? null
529
- : concreteArguments.sort(nameComparator);
530
- }
531
-
532
- function nameComparator(
533
- a: {+name: string, ...},
534
- b: {+name: string, ...},
535
- ): number {
536
- return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
537
- }
538
-
539
- /**
540
- * Pre-computes storage key if possible and advantageous. Storage keys are
541
- * generated for fields with supplied arguments that are all statically known
542
- * (ie. literals, no variables) at build time.
543
- */
544
- function getStaticStorageKey(
545
- field: NormalizationField,
546
- metadata: Metadata,
547
- ): ?string {
548
- const metadataStorageKey = metadata?.storageKey;
549
- if (typeof metadataStorageKey === 'string') {
550
- return metadataStorageKey;
551
- }
552
- if (
553
- !field.args ||
554
- field.args.length === 0 ||
555
- field.args.some(argumentContainsVariables)
556
- ) {
557
- return null;
558
- }
559
- return getStorageKey(field, {});
560
- }
561
-
562
- function isPlural(schema: Schema, type: TypeID): boolean {
563
- return schema.isList(schema.getNullableType(type));
564
- }
565
-
566
- module.exports = {generate};