relay-compiler 8.0.0 → 10.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. package/bin/RelayCompilerBin.js.flow +169 -0
  2. package/bin/RelayCompilerMain.js.flow +515 -0
  3. package/bin/__fixtures__/plugin-module.js.flow +17 -0
  4. package/bin/relay-compiler +8930 -8967
  5. package/codegen/CodegenDirectory.js.flow +375 -0
  6. package/codegen/CodegenRunner.js.flow +432 -0
  7. package/codegen/CodegenTypes.js.flow +28 -0
  8. package/codegen/CodegenWatcher.js.flow +254 -0
  9. package/codegen/NormalizationCodeGenerator.js.flow +563 -0
  10. package/codegen/ReaderCodeGenerator.js.flow +477 -0
  11. package/codegen/RelayCodeGenerator.js.flow +85 -0
  12. package/codegen/RelayFileWriter.js.flow +365 -0
  13. package/codegen/SourceControl.js.flow +58 -0
  14. package/codegen/compileRelayArtifacts.js.flow +182 -0
  15. package/codegen/createPrintRequireModuleDependency.js.flow +21 -0
  16. package/codegen/sortObjectByKey.js.flow +25 -0
  17. package/codegen/writeRelayGeneratedFile.js.flow +223 -0
  18. package/core/ASTCache.js.flow +73 -0
  19. package/core/ASTConvert.js.flow +233 -0
  20. package/core/CompilerContext.js.flow +190 -0
  21. package/core/CompilerError.js.flow +250 -0
  22. package/core/DotGraphQLParser.js.flow +39 -0
  23. package/core/GraphQLCompilerProfiler.js.flow +341 -0
  24. package/core/GraphQLDerivedFromMetadata.js.flow +36 -0
  25. package/core/GraphQLWatchmanClient.js.flow +111 -0
  26. package/core/IR.js.flow +327 -0
  27. package/core/IRPrinter.js.flow +482 -0
  28. package/core/IRTransformer.js.flow +377 -0
  29. package/core/IRValidator.js.flow +260 -0
  30. package/core/IRVisitor.js.flow +150 -0
  31. package/core/JSModuleParser.js.flow +24 -0
  32. package/core/RelayCompilerScope.js.flow +199 -0
  33. package/core/RelayFindGraphQLTags.js.flow +119 -0
  34. package/core/RelayGraphQLEnumsGenerator.js.flow +55 -0
  35. package/core/RelayIRTransforms.js.flow +131 -0
  36. package/core/RelayParser.js.flow +1731 -0
  37. package/core/RelaySourceModuleParser.js.flow +135 -0
  38. package/core/Schema.js.flow +1983 -0
  39. package/core/SchemaUtils.js.flow +120 -0
  40. package/core/filterContextForNode.js.flow +50 -0
  41. package/core/getFieldDefinition.js.flow +156 -0
  42. package/core/getIdentifierForArgumentValue.js.flow +49 -0
  43. package/core/getIdentifierForSelection.js.flow +69 -0
  44. package/core/getLiteralArgumentValues.js.flow +32 -0
  45. package/core/getNormalizationOperationName.js.flow +19 -0
  46. package/core/inferRootArgumentDefinitions.js.flow +323 -0
  47. package/index.js +1 -1
  48. package/index.js.flow +200 -0
  49. package/language/RelayLanguagePluginInterface.js.flow +283 -0
  50. package/language/javascript/FindGraphQLTags.js.flow +232 -0
  51. package/language/javascript/RelayFlowBabelFactories.js.flow +180 -0
  52. package/language/javascript/RelayFlowGenerator.js.flow +1042 -0
  53. package/language/javascript/RelayFlowTypeTransformers.js.flow +184 -0
  54. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +34 -0
  55. package/language/javascript/formatGeneratedModule.js.flow +65 -0
  56. package/lib/bin/RelayCompilerBin.js +24 -7
  57. package/lib/bin/RelayCompilerMain.js +141 -136
  58. package/lib/codegen/CodegenDirectory.js +13 -8
  59. package/lib/codegen/CodegenRunner.js +37 -76
  60. package/lib/codegen/CodegenWatcher.js +13 -21
  61. package/lib/codegen/NormalizationCodeGenerator.js +117 -140
  62. package/lib/codegen/ReaderCodeGenerator.js +76 -117
  63. package/lib/codegen/RelayCodeGenerator.js +17 -6
  64. package/lib/codegen/RelayFileWriter.js +19 -40
  65. package/lib/codegen/compileRelayArtifacts.js +16 -30
  66. package/lib/codegen/sortObjectByKey.js +43 -0
  67. package/lib/codegen/writeRelayGeneratedFile.js +86 -95
  68. package/lib/core/ASTCache.js +2 -4
  69. package/lib/core/CompilerContext.js +2 -4
  70. package/lib/core/CompilerError.js +27 -54
  71. package/lib/core/GraphQLCompilerProfiler.js +8 -12
  72. package/lib/core/GraphQLDerivedFromMetadata.js +1 -10
  73. package/lib/core/GraphQLWatchmanClient.js +4 -12
  74. package/lib/core/IRPrinter.js +23 -21
  75. package/lib/core/IRTransformer.js +8 -16
  76. package/lib/core/IRValidator.js +1 -9
  77. package/lib/core/IRVisitor.js +0 -2
  78. package/lib/core/RelayCompilerScope.js +4 -4
  79. package/lib/core/RelayGraphQLEnumsGenerator.js +12 -15
  80. package/lib/core/RelayIRTransforms.js +16 -14
  81. package/lib/core/RelayParser.js +53 -89
  82. package/lib/core/RelaySourceModuleParser.js +3 -3
  83. package/lib/core/Schema.js +61 -73
  84. package/lib/core/SchemaUtils.js +15 -1
  85. package/lib/core/getFieldDefinition.js +12 -15
  86. package/lib/core/getIdentifierForSelection.js +2 -4
  87. package/lib/core/inferRootArgumentDefinitions.js +33 -73
  88. package/lib/index.js +4 -5
  89. package/lib/language/javascript/FindGraphQLTags.js +4 -3
  90. package/lib/language/javascript/RelayFlowGenerator.js +82 -171
  91. package/lib/language/javascript/RelayFlowTypeTransformers.js +1 -3
  92. package/lib/language/javascript/RelayLanguagePluginJavaScript.js +6 -4
  93. package/lib/language/javascript/formatGeneratedModule.js +11 -2
  94. package/lib/reporters/ConsoleReporter.js +1 -3
  95. package/lib/reporters/MultiReporter.js +1 -3
  96. package/lib/runner/Artifacts.js +69 -170
  97. package/lib/runner/BufferedFilesystem.js +32 -66
  98. package/lib/runner/GraphQLASTNodeGroup.js +54 -120
  99. package/lib/runner/GraphQLNodeMap.js +14 -19
  100. package/lib/runner/Sources.js +51 -85
  101. package/lib/runner/StrictMap.js +21 -37
  102. package/lib/runner/getChangedNodeNames.js +30 -62
  103. package/lib/transforms/ApplyFragmentArgumentTransform.js +73 -59
  104. package/lib/transforms/ClientExtensionsTransform.js +12 -16
  105. package/lib/transforms/ConnectionTransform.js +30 -37
  106. package/lib/transforms/DeclarativeConnectionMutationTransform.js +167 -0
  107. package/lib/transforms/DeferStreamTransform.js +30 -73
  108. package/lib/transforms/DisallowTypenameOnRoot.js +55 -0
  109. package/lib/transforms/FieldHandleTransform.js +6 -2
  110. package/lib/transforms/FlattenTransform.js +18 -45
  111. package/lib/transforms/GenerateIDFieldTransform.js +56 -35
  112. package/lib/transforms/GenerateTypeNameTransform.js +84 -10
  113. package/lib/transforms/InlineDataFragmentTransform.js +9 -4
  114. package/lib/transforms/MaskTransform.js +17 -17
  115. package/lib/transforms/MatchTransform.js +110 -32
  116. package/lib/transforms/RefetchableFragmentTransform.js +21 -38
  117. package/lib/transforms/RelayDirectiveTransform.js +8 -3
  118. package/lib/transforms/SkipClientExtensionsTransform.js +8 -0
  119. package/lib/transforms/SkipHandleFieldTransform.js +6 -2
  120. package/lib/transforms/SkipRedundantNodesTransform.js +7 -4
  121. package/lib/transforms/SkipSplitOperationTransform.js +32 -0
  122. package/lib/transforms/SkipUnreachableNodeTransform.js +9 -10
  123. package/lib/transforms/SkipUnusedVariablesTransform.js +18 -17
  124. package/lib/transforms/SplitModuleImportTransform.js +2 -2
  125. package/lib/transforms/TestOperationTransform.js +26 -22
  126. package/lib/transforms/ValidateGlobalVariablesTransform.js +18 -30
  127. package/lib/transforms/ValidateRequiredArgumentsTransform.js +12 -16
  128. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +16 -30
  129. package/lib/transforms/ValidateUnusedVariablesTransform.js +18 -30
  130. package/lib/transforms/query-generators/FetchableQueryGenerator.js +161 -0
  131. package/lib/transforms/query-generators/NodeQueryGenerator.js +22 -3
  132. package/lib/transforms/query-generators/QueryQueryGenerator.js +2 -1
  133. package/lib/transforms/query-generators/ViewerQueryGenerator.js +1 -0
  134. package/lib/transforms/query-generators/index.js +23 -6
  135. package/lib/transforms/query-generators/utils.js +17 -16
  136. package/lib/util/RelayCompilerCache.js +2 -4
  137. package/lib/util/argumentContainsVariables.js +37 -0
  138. package/lib/util/dedupeJSONStringify.js +15 -12
  139. package/lib/util/generateAbstractTypeRefinementKey.js +24 -0
  140. package/lib/util/getModuleName.js +3 -5
  141. package/lib/util/joinArgumentDefinitions.js +3 -1
  142. package/package.json +6 -6
  143. package/relay-compiler.js +4 -4
  144. package/relay-compiler.min.js +4 -4
  145. package/reporters/ConsoleReporter.js.flow +81 -0
  146. package/reporters/MultiReporter.js.flow +43 -0
  147. package/reporters/Reporter.js.flow +19 -0
  148. package/runner/Artifacts.js.flow +219 -0
  149. package/runner/BufferedFilesystem.js.flow +194 -0
  150. package/runner/GraphQLASTNodeGroup.js.flow +176 -0
  151. package/runner/GraphQLASTUtils.js.flow +26 -0
  152. package/runner/GraphQLNodeMap.js.flow +55 -0
  153. package/runner/Sources.js.flow +214 -0
  154. package/runner/StrictMap.js.flow +96 -0
  155. package/runner/compileArtifacts.js.flow +76 -0
  156. package/runner/extractAST.js.flow +100 -0
  157. package/runner/getChangedNodeNames.js.flow +48 -0
  158. package/runner/getSchemaInstance.js.flow +36 -0
  159. package/runner/types.js.flow +37 -0
  160. package/transforms/ApplyFragmentArgumentTransform.js.flow +526 -0
  161. package/transforms/ClientExtensionsTransform.js.flow +222 -0
  162. package/transforms/ConnectionTransform.js.flow +856 -0
  163. package/transforms/DeclarativeConnectionMutationTransform.js.flow +157 -0
  164. package/transforms/DeferStreamTransform.js.flow +265 -0
  165. package/transforms/DisallowIdAsAlias.js.flow +47 -0
  166. package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
  167. package/transforms/FieldHandleTransform.js.flow +80 -0
  168. package/transforms/FilterDirectivesTransform.js.flow +45 -0
  169. package/transforms/FlattenTransform.js.flow +453 -0
  170. package/transforms/GenerateIDFieldTransform.js.flow +152 -0
  171. package/transforms/GenerateTypeNameTransform.js.flow +161 -0
  172. package/transforms/InlineDataFragmentTransform.js.flow +125 -0
  173. package/transforms/InlineFragmentsTransform.js.flow +71 -0
  174. package/transforms/MaskTransform.js.flow +126 -0
  175. package/transforms/MatchTransform.js.flow +583 -0
  176. package/transforms/RefetchableFragmentTransform.js.flow +272 -0
  177. package/transforms/RelayDirectiveTransform.js.flow +97 -0
  178. package/transforms/SkipClientExtensionsTransform.js.flow +54 -0
  179. package/transforms/SkipHandleFieldTransform.js.flow +44 -0
  180. package/transforms/SkipRedundantNodesTransform.js.flow +254 -0
  181. package/transforms/SkipSplitOperationTransform.js.flow +37 -0
  182. package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
  183. package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
  184. package/transforms/SplitModuleImportTransform.js.flow +98 -0
  185. package/transforms/TestOperationTransform.js.flow +142 -0
  186. package/transforms/TransformUtils.js.flow +26 -0
  187. package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
  188. package/transforms/ValidateRequiredArgumentsTransform.js.flow +127 -0
  189. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +112 -0
  190. package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
  191. package/transforms/query-generators/FetchableQueryGenerator.js.flow +189 -0
  192. package/transforms/query-generators/NodeQueryGenerator.js.flow +219 -0
  193. package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
  194. package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
  195. package/transforms/query-generators/index.js.flow +90 -0
  196. package/transforms/query-generators/utils.js.flow +76 -0
  197. package/util/CodeMarker.js.flow +79 -0
  198. package/{lib/core/GraphQLIR.js → util/DefaultHandleKey.js.flow} +9 -2
  199. package/util/RelayCompilerCache.js.flow +88 -0
  200. package/util/Rollout.js.flow +39 -0
  201. package/util/TimeReporter.js.flow +79 -0
  202. package/util/areEqualOSS.js.flow +123 -0
  203. package/util/argumentContainsVariables.js.flow +38 -0
  204. package/util/dedupeJSONStringify.js.flow +152 -0
  205. package/util/generateAbstractTypeRefinementKey.js.flow +29 -0
  206. package/util/getDefinitionNodeHash.js.flow +25 -0
  207. package/util/getModuleName.js.flow +39 -0
  208. package/util/joinArgumentDefinitions.js.flow +105 -0
  209. package/util/md5.js.flow +22 -0
  210. package/util/murmurHash.js.flow +94 -0
  211. package/util/nullthrowsOSS.js.flow +25 -0
  212. package/util/orList.js.flow +37 -0
  213. package/util/partitionArray.js.flow +37 -0
  214. package/lib/core/GraphQLCompilerContext.js +0 -165
  215. package/lib/core/GraphQLIRPrinter.js +0 -371
  216. package/lib/core/GraphQLIRTransformer.js +0 -344
  217. package/lib/core/GraphQLIRValidator.js +0 -218
  218. package/lib/core/GraphQLIRVisitor.js +0 -46
  219. package/lib/core/RelayCompilerError.js +0 -277
  220. package/lib/transforms/ConnectionFieldTransform.js +0 -276
@@ -0,0 +1,856 @@
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
+ const RelayParser = require('../core/RelayParser');
17
+ const SchemaUtils = require('../core/SchemaUtils');
18
+
19
+ const getLiteralArgumentValues = require('../core/getLiteralArgumentValues');
20
+
21
+ const {createCompilerError, createUserError} = require('../core/CompilerError');
22
+ const {parse} = require('graphql');
23
+ const {ConnectionInterface, RelayFeatureFlags} = require('relay-runtime');
24
+
25
+ import type CompilerContext from '../core/CompilerContext';
26
+ import type {
27
+ Argument,
28
+ Directive,
29
+ Fragment,
30
+ Handle,
31
+ InlineFragment,
32
+ LinkedField,
33
+ Root,
34
+ Selection,
35
+ Variable,
36
+ Location,
37
+ Defer,
38
+ } from '../core/IR';
39
+ import type {Schema, CompositeTypeID} from '../core/Schema';
40
+ import type {ConnectionMetadata} from 'relay-runtime';
41
+
42
+ type Options = {
43
+ documentName: string,
44
+ // The current path
45
+ path: Array<?string>,
46
+ // Metadata recorded for @connection fields
47
+ connectionMetadata: Array<ConnectionMetadata>,
48
+ ...
49
+ };
50
+
51
+ type ConnectionArguments = {|
52
+ handler: ?string,
53
+ key: string,
54
+ dynamicKey: Variable | null,
55
+ filters: ?$ReadOnlyArray<string>,
56
+ stream: ?{|
57
+ if: ?Argument,
58
+ initialCount: ?Argument,
59
+ useCustomizedBatch: ?Argument,
60
+ label: string,
61
+ |},
62
+ |};
63
+
64
+ const AFTER = 'after';
65
+ const BEFORE = 'before';
66
+ const FIRST = 'first';
67
+ const KEY = 'key';
68
+ const LAST = 'last';
69
+
70
+ const CONNECTION = 'connection';
71
+ const STREAM_CONNECTION = 'stream_connection';
72
+ const HANDLER = 'handler';
73
+
74
+ /**
75
+ * @public
76
+ *
77
+ * Transforms fields with the `@connection` directive:
78
+ * - Verifies that the field type is connection-like.
79
+ * - Adds a `handle` property to the field, either the user-provided `handle`
80
+ * argument or the default value "connection".
81
+ * - Inserts a sub-fragment on the field to ensure that standard connection
82
+ * fields are fetched (e.g. cursors, node ids, page info).
83
+ */
84
+ function connectionTransform(context: CompilerContext): CompilerContext {
85
+ return IRTransformer.transform(
86
+ context,
87
+ {
88
+ Fragment: visitFragmentOrRoot,
89
+ LinkedField: visitLinkedField,
90
+ Root: visitFragmentOrRoot,
91
+ },
92
+ node => ({
93
+ documentName: node.name,
94
+ path: [],
95
+ connectionMetadata: [],
96
+ }),
97
+ );
98
+ }
99
+
100
+ const SCHEMA_EXTENSION = `
101
+ directive @connection(
102
+ key: String!
103
+ filters: [String]
104
+ handler: String
105
+ dynamicKey_UNSTABLE: String
106
+ ) on FIELD
107
+
108
+ directive @stream_connection(
109
+ key: String!
110
+ filters: [String]
111
+ handler: String
112
+ initial_count: Int!
113
+ if: Boolean = true
114
+ use_customized_batch: Boolean = false
115
+ dynamicKey_UNSTABLE: String
116
+ ) on FIELD
117
+ `;
118
+
119
+ /**
120
+ * @internal
121
+ */
122
+ function visitFragmentOrRoot<N: Fragment | Root>(
123
+ node: N,
124
+ options: Options,
125
+ ): ?N {
126
+ const transformedNode = this.traverse(node, options);
127
+ const connectionMetadata = options.connectionMetadata;
128
+ if (connectionMetadata.length) {
129
+ return {
130
+ ...transformedNode,
131
+ metadata: {
132
+ ...transformedNode.metadata,
133
+ connection: connectionMetadata,
134
+ },
135
+ };
136
+ }
137
+ return transformedNode;
138
+ }
139
+
140
+ /**
141
+ * @internal
142
+ */
143
+ function visitLinkedField(field: LinkedField, options: Options): LinkedField {
144
+ const context: CompilerContext = this.getContext();
145
+ const schema = context.getSchema();
146
+
147
+ const nullableType = schema.getNullableType(field.type);
148
+
149
+ const isPlural = schema.isList(nullableType);
150
+ const path = options.path.concat(isPlural ? null : field.alias || field.name);
151
+ let transformedField: LinkedField = this.traverse(field, {
152
+ ...options,
153
+ path,
154
+ });
155
+ const connectionDirective = field.directives.find(
156
+ directive =>
157
+ directive.name === CONNECTION || directive.name === STREAM_CONNECTION,
158
+ );
159
+ if (!connectionDirective) {
160
+ return transformedField;
161
+ }
162
+ if (!schema.isObject(nullableType) && !schema.isInterface(nullableType)) {
163
+ throw new createUserError(
164
+ `@${connectionDirective.name} used on invalid field '${field.name}'. ` +
165
+ 'Expected the return type to be a non-plural interface or object, ' +
166
+ `got '${schema.getTypeString(field.type)}'.`,
167
+ [transformedField.loc],
168
+ );
169
+ }
170
+
171
+ validateConnectionSelection(transformedField);
172
+ validateConnectionType(
173
+ schema,
174
+ transformedField,
175
+ schema.assertCompositeType(nullableType),
176
+ connectionDirective,
177
+ );
178
+
179
+ const connectionArguments = buildConnectionArguments(
180
+ transformedField,
181
+ connectionDirective,
182
+ );
183
+
184
+ const connectionMetadata = buildConnectionMetadata(
185
+ transformedField,
186
+ path,
187
+ connectionArguments.stream != null,
188
+ );
189
+ options.connectionMetadata.push(connectionMetadata);
190
+
191
+ const handle: Handle = {
192
+ name: connectionArguments.handler ?? CONNECTION,
193
+ key: connectionArguments.key,
194
+ dynamicKey: connectionArguments.dynamicKey,
195
+ filters: connectionArguments.filters,
196
+ };
197
+
198
+ const {direction} = connectionMetadata;
199
+ if (direction != null) {
200
+ const selections = transformConnectionSelections(
201
+ this.getContext(),
202
+ transformedField,
203
+ schema.assertCompositeType(nullableType),
204
+ direction,
205
+ connectionArguments,
206
+ connectionDirective.loc,
207
+ options.documentName,
208
+ );
209
+ transformedField = {
210
+ ...transformedField,
211
+ selections,
212
+ };
213
+ }
214
+ return {
215
+ ...transformedField,
216
+ directives: transformedField.directives.filter(
217
+ directive => directive !== connectionDirective,
218
+ ),
219
+ connection: true,
220
+ handles: transformedField.handles
221
+ ? [...transformedField.handles, handle]
222
+ : [handle],
223
+ };
224
+ }
225
+
226
+ function buildConnectionArguments(
227
+ field: LinkedField,
228
+ connectionDirective: Directive,
229
+ ): ConnectionArguments {
230
+ const {
231
+ handler,
232
+ key,
233
+ label,
234
+ filters: literalFilters,
235
+ } = getLiteralArgumentValues(connectionDirective.args);
236
+ if (handler != null && typeof handler !== 'string') {
237
+ const handleArg = connectionDirective.args.find(
238
+ arg => arg.name === 'handler',
239
+ );
240
+ throw createUserError(
241
+ `Expected the ${HANDLER} argument to @${connectionDirective.name} to ` +
242
+ `be a string literal for field ${field.name}.`,
243
+ [handleArg?.value?.loc ?? connectionDirective.loc],
244
+ );
245
+ }
246
+ if (typeof key !== 'string') {
247
+ const keyArg = connectionDirective.args.find(arg => arg.name === 'key');
248
+ throw createUserError(
249
+ `Expected the ${KEY} argument to @${connectionDirective.name} to be a ` +
250
+ `string literal for field ${field.name}.`,
251
+ [keyArg?.value?.loc ?? connectionDirective.loc],
252
+ );
253
+ }
254
+ const postfix = field.alias || field.name;
255
+ if (!key.endsWith('_' + postfix)) {
256
+ const keyArg = connectionDirective.args.find(arg => arg.name === 'key');
257
+ throw createUserError(
258
+ `Expected the ${KEY} argument to @${connectionDirective.name} to be of ` +
259
+ `form <SomeName>_${postfix}, got '${key}'. ` +
260
+ 'For a detailed explanation, check out ' +
261
+ 'https://relay.dev/docs/en/pagination-container#connection',
262
+ [keyArg?.value?.loc ?? connectionDirective.loc],
263
+ );
264
+ }
265
+ if (
266
+ literalFilters != null &&
267
+ (!Array.isArray(literalFilters) ||
268
+ literalFilters.some(filter => typeof filter !== 'string'))
269
+ ) {
270
+ const filtersArg = connectionDirective.args.find(
271
+ arg => arg.name === 'filters',
272
+ );
273
+ throw createUserError(
274
+ `Expected the 'filters' argument to @${connectionDirective.name} to be ` +
275
+ 'a string literal.',
276
+ [filtersArg?.value?.loc ?? connectionDirective.loc],
277
+ );
278
+ }
279
+
280
+ let filters = literalFilters;
281
+ if (filters == null) {
282
+ const generatedFilters = field.args
283
+ .filter(
284
+ arg =>
285
+ !ConnectionInterface.isConnectionCall({
286
+ name: arg.name,
287
+ value: null,
288
+ }),
289
+ )
290
+ .map(arg => arg.name);
291
+ filters = generatedFilters.length !== 0 ? generatedFilters : null;
292
+ }
293
+
294
+ let stream = null;
295
+ if (connectionDirective.name === STREAM_CONNECTION) {
296
+ const initialCountArg = connectionDirective.args.find(
297
+ arg => arg.name === 'initial_count',
298
+ );
299
+ const useCustomizedBatchArg = connectionDirective.args.find(
300
+ arg => arg.name === 'use_customized_batch',
301
+ );
302
+ const ifArg = connectionDirective.args.find(arg => arg.name === 'if');
303
+ stream = {
304
+ if: ifArg,
305
+ initialCount: initialCountArg,
306
+ useCustomizedBatch: useCustomizedBatchArg,
307
+ label: key,
308
+ };
309
+ }
310
+
311
+ // T45504512: new connection model
312
+ const dynamicKeyArg = connectionDirective.args.find(
313
+ arg => arg.name === 'dynamicKey_UNSTABLE',
314
+ );
315
+ let dynamicKey: Variable | null = null;
316
+ if (dynamicKeyArg != null) {
317
+ if (
318
+ RelayFeatureFlags.ENABLE_VARIABLE_CONNECTION_KEY &&
319
+ dynamicKeyArg.value.kind === 'Variable'
320
+ ) {
321
+ dynamicKey = dynamicKeyArg.value;
322
+ } else {
323
+ throw createUserError(
324
+ `Unsupported 'dynamicKey_UNSTABLE' argument to @${connectionDirective.name}. This argument is only valid when the feature flag is enabled and ` +
325
+ 'the variable must be a variable',
326
+ [connectionDirective.loc],
327
+ );
328
+ }
329
+ }
330
+
331
+ return {
332
+ handler,
333
+ key,
334
+ dynamicKey,
335
+ filters: (filters: $FlowFixMe),
336
+ stream,
337
+ };
338
+ }
339
+
340
+ function buildConnectionMetadata(
341
+ field: LinkedField,
342
+ path: Array<?string>,
343
+ stream: boolean,
344
+ ): ConnectionMetadata {
345
+ const pathHasPlural = path.includes(null);
346
+ const firstArg = findArg(field, FIRST);
347
+ const lastArg = findArg(field, LAST);
348
+ let direction = null;
349
+ let countArg = null;
350
+ let cursorArg = null;
351
+ if (firstArg && !lastArg) {
352
+ direction = 'forward';
353
+ countArg = firstArg;
354
+ cursorArg = findArg(field, AFTER);
355
+ } else if (lastArg && !firstArg) {
356
+ direction = 'backward';
357
+ countArg = lastArg;
358
+ cursorArg = findArg(field, BEFORE);
359
+ } else if (lastArg && firstArg) {
360
+ direction = 'bidirectional';
361
+ // TODO(T26511885) Maybe add connection metadata to this case
362
+ }
363
+ const countVariable =
364
+ countArg && countArg.value.kind === 'Variable'
365
+ ? countArg.value.variableName
366
+ : null;
367
+ const cursorVariable =
368
+ cursorArg && cursorArg.value.kind === 'Variable'
369
+ ? cursorArg.value.variableName
370
+ : null;
371
+ if (stream) {
372
+ return {
373
+ count: countVariable,
374
+ cursor: cursorVariable,
375
+ direction,
376
+ path: pathHasPlural ? null : (path: $FlowFixMe),
377
+ stream: true,
378
+ };
379
+ }
380
+ return {
381
+ count: countVariable,
382
+ cursor: cursorVariable,
383
+ direction,
384
+ path: pathHasPlural ? null : (path: $FlowFixMe),
385
+ };
386
+ }
387
+
388
+ /**
389
+ * @internal
390
+ *
391
+ * Transforms the selections on a connection field, generating fields necessary
392
+ * for pagination (edges.cursor, pageInfo, etc) and adding/merging them with
393
+ * existing selections.
394
+ */
395
+ function transformConnectionSelections(
396
+ context: CompilerContext,
397
+ field: LinkedField,
398
+ nullableType: CompositeTypeID,
399
+ direction: 'forward' | 'backward' | 'bidirectional',
400
+ connectionArguments: ConnectionArguments,
401
+ directiveLocation: Location,
402
+ documentName: string,
403
+ ): $ReadOnlyArray<Selection> {
404
+ const schema = context.getSchema();
405
+ const derivedFieldLocation = {kind: 'Derived', source: field.loc};
406
+ const derivedDirectiveLocation = {
407
+ kind: 'Derived',
408
+ source: directiveLocation,
409
+ };
410
+ const {
411
+ CURSOR,
412
+ EDGES,
413
+ END_CURSOR,
414
+ HAS_NEXT_PAGE,
415
+ HAS_PREV_PAGE,
416
+ NODE,
417
+ PAGE_INFO,
418
+ START_CURSOR,
419
+ } = ConnectionInterface.get();
420
+
421
+ // Find existing edges/pageInfo selections
422
+ let edgesSelection: ?LinkedField;
423
+ let pageInfoSelection: ?LinkedField;
424
+ field.selections.forEach(selection => {
425
+ if (selection.kind === 'LinkedField') {
426
+ if (selection.name === EDGES) {
427
+ if (edgesSelection != null) {
428
+ throw createCompilerError(
429
+ `ConnectionTransform: Unexpected duplicate field '${EDGES}'.`,
430
+ [edgesSelection.loc, selection.loc],
431
+ );
432
+ }
433
+ edgesSelection = selection;
434
+ return;
435
+ } else if (selection.name === PAGE_INFO) {
436
+ if (pageInfoSelection != null) {
437
+ throw createCompilerError(
438
+ `ConnectionTransform: Unexpected duplicate field '${PAGE_INFO}'.`,
439
+ [pageInfoSelection.loc, selection.loc],
440
+ );
441
+ }
442
+ pageInfoSelection = selection;
443
+ return;
444
+ }
445
+ }
446
+ });
447
+ // If streaming is enabled, construct directives to apply to the edges/
448
+ // pageInfo fields
449
+ let streamDirective;
450
+ const stream = connectionArguments.stream;
451
+ if (stream != null) {
452
+ streamDirective = {
453
+ args: [
454
+ stream.if,
455
+ stream.initialCount,
456
+ stream.useCustomizedBatch,
457
+ {
458
+ kind: 'Argument',
459
+ loc: derivedDirectiveLocation,
460
+ name: 'label',
461
+ type: SchemaUtils.getNullableStringInput(schema),
462
+ value: {
463
+ kind: 'Literal',
464
+ loc: derivedDirectiveLocation,
465
+ value: stream.label,
466
+ },
467
+ },
468
+ ].filter(Boolean),
469
+ kind: 'Directive',
470
+ loc: derivedDirectiveLocation,
471
+ name: 'stream',
472
+ };
473
+ }
474
+ // For backwards compatibility with earlier versions of this transform,
475
+ // edges/pageInfo have to be generated as non-aliased fields (since product
476
+ // code may be accessing the non-aliased response keys). But for streaming
477
+ // mode we need to generate @stream/@defer directives on these fields *and*
478
+ // we prefer to avoid generating extra selections (we want one payload per
479
+ // item, not two as could happen with separate @stream directives on the
480
+ // aliased and non-aliased edges fields). So we keep things simple by
481
+ // disallowing aliases on edges/pageInfo in streaming mode.
482
+ if (edgesSelection && edgesSelection.alias !== edgesSelection.name) {
483
+ if (stream) {
484
+ throw createUserError(
485
+ `@stream_connection does not support aliasing the '${EDGES}' field.`,
486
+ [edgesSelection.loc],
487
+ );
488
+ }
489
+ edgesSelection = null;
490
+ }
491
+ if (pageInfoSelection && pageInfoSelection.alias !== pageInfoSelection.name) {
492
+ if (stream) {
493
+ throw createUserError(
494
+ `@stream_connection does not support aliasing the '${PAGE_INFO}' field.`,
495
+ [pageInfoSelection.loc],
496
+ );
497
+ }
498
+ pageInfoSelection = null;
499
+ }
500
+
501
+ // Separately create transformed versions of edges/pageInfo so that we can
502
+ // later replace the originals at the same point within the selection array
503
+ let transformedEdgesSelection: ?LinkedField = edgesSelection;
504
+ let transformedPageInfoSelection: ?(
505
+ | Defer
506
+ | InlineFragment
507
+ | LinkedField
508
+ ) = pageInfoSelection;
509
+ const edgesType = schema.getFieldConfig(
510
+ schema.expectField(nullableType, EDGES),
511
+ ).type;
512
+
513
+ const pageInfoType = schema.getFieldConfig(
514
+ schema.expectField(nullableType, PAGE_INFO),
515
+ ).type;
516
+
517
+ if (transformedEdgesSelection == null) {
518
+ transformedEdgesSelection = {
519
+ alias: EDGES,
520
+ args: [],
521
+ connection: false,
522
+ directives: [],
523
+ handles: null,
524
+ kind: 'LinkedField',
525
+ loc: derivedFieldLocation,
526
+ metadata: null,
527
+ name: EDGES,
528
+ selections: [],
529
+ type: schema.assertLinkedFieldType(edgesType),
530
+ };
531
+ }
532
+ if (transformedPageInfoSelection == null) {
533
+ transformedPageInfoSelection = {
534
+ alias: PAGE_INFO,
535
+ args: [],
536
+ connection: false,
537
+ directives: [],
538
+ handles: null,
539
+ kind: 'LinkedField',
540
+ loc: derivedFieldLocation,
541
+ metadata: null,
542
+ name: PAGE_INFO,
543
+ selections: [],
544
+ type: schema.assertLinkedFieldType(pageInfoType),
545
+ };
546
+ }
547
+
548
+ // Generate (additional) fields on pageInfo and add to the transformed
549
+ // pageInfo field
550
+ const pageInfoRawType = schema.getRawType(pageInfoType);
551
+ let pageInfoText;
552
+ if (direction === 'forward') {
553
+ pageInfoText = `fragment PageInfo on ${schema.getTypeString(
554
+ pageInfoRawType,
555
+ )} {
556
+ ${END_CURSOR}
557
+ ${HAS_NEXT_PAGE}
558
+ }`;
559
+ } else if (direction === 'backward') {
560
+ pageInfoText = `fragment PageInfo on ${schema.getTypeString(
561
+ pageInfoRawType,
562
+ )} {
563
+ ${HAS_PREV_PAGE}
564
+ ${START_CURSOR}
565
+ }`;
566
+ } else {
567
+ pageInfoText = `fragment PageInfo on ${schema.getTypeString(
568
+ pageInfoRawType,
569
+ )} {
570
+ ${END_CURSOR}
571
+ ${HAS_NEXT_PAGE}
572
+ ${HAS_PREV_PAGE}
573
+ ${START_CURSOR}
574
+ }`;
575
+ }
576
+ const pageInfoAst = parse(pageInfoText);
577
+ const pageInfoFragment = RelayParser.transform(schema, [
578
+ pageInfoAst.definitions[0],
579
+ ])[0];
580
+ if (transformedPageInfoSelection.kind !== 'LinkedField') {
581
+ throw createCompilerError(
582
+ 'ConnectionTransform: Expected generated pageInfo selection to be ' +
583
+ 'a LinkedField',
584
+ [field.loc],
585
+ );
586
+ }
587
+ transformedPageInfoSelection = {
588
+ ...transformedPageInfoSelection,
589
+ selections: [
590
+ ...transformedPageInfoSelection.selections,
591
+ {
592
+ directives: [],
593
+ kind: 'InlineFragment',
594
+ loc: derivedFieldLocation,
595
+ metadata: null,
596
+ selections: pageInfoFragment.selections,
597
+ typeCondition: pageInfoFragment.type,
598
+ },
599
+ ],
600
+ };
601
+ // When streaming the pageInfo field has to be deferred
602
+ if (stream != null) {
603
+ transformedPageInfoSelection = {
604
+ if: stream.if?.value ?? null,
605
+ label: `${documentName}$defer$${stream.label}$${PAGE_INFO}`,
606
+ kind: 'Defer',
607
+ loc: derivedFieldLocation,
608
+ selections: [transformedPageInfoSelection],
609
+ };
610
+ }
611
+
612
+ // Generate additional fields on edges and append to the transformed edges
613
+ // selection
614
+ const edgeText = `
615
+ fragment Edges on ${schema.getTypeString(schema.getRawType(edgesType))} {
616
+ ${CURSOR}
617
+ ${NODE} {
618
+ __typename # rely on GenerateRequisiteFieldTransform to add "id"
619
+ }
620
+ }
621
+ `;
622
+ const edgeAst = parse(edgeText);
623
+ const edgeFragment = RelayParser.transform(schema, [
624
+ edgeAst.definitions[0],
625
+ ])[0];
626
+ // When streaming the edges field needs @stream
627
+ transformedEdgesSelection = {
628
+ ...transformedEdgesSelection,
629
+ directives:
630
+ streamDirective != null
631
+ ? [...transformedEdgesSelection.directives, streamDirective]
632
+ : transformedEdgesSelection.directives,
633
+ selections: [
634
+ ...transformedEdgesSelection.selections,
635
+ {
636
+ directives: [],
637
+ kind: 'InlineFragment',
638
+ loc: derivedFieldLocation,
639
+ metadata: null,
640
+ selections: edgeFragment.selections,
641
+ typeCondition: edgeFragment.type,
642
+ },
643
+ ],
644
+ };
645
+ // Copy the original selections, replacing edges/pageInfo (if present)
646
+ // with the generated locations. This is to maintain the original field
647
+ // ordering.
648
+ const selections = field.selections.map(selection => {
649
+ if (
650
+ transformedEdgesSelection != null &&
651
+ edgesSelection != null &&
652
+ selection === edgesSelection
653
+ ) {
654
+ return transformedEdgesSelection;
655
+ } else if (
656
+ transformedPageInfoSelection != null &&
657
+ pageInfoSelection != null &&
658
+ selection === pageInfoSelection
659
+ ) {
660
+ return transformedPageInfoSelection;
661
+ } else {
662
+ return selection;
663
+ }
664
+ });
665
+ // If edges/pageInfo were missing, append the generated versions instead.
666
+ if (edgesSelection == null && transformedEdgesSelection != null) {
667
+ selections.push(transformedEdgesSelection);
668
+ }
669
+ if (pageInfoSelection == null && transformedPageInfoSelection != null) {
670
+ selections.push(transformedPageInfoSelection);
671
+ }
672
+ return selections;
673
+ }
674
+
675
+ function findArg(field: LinkedField, argName: string): ?Argument {
676
+ return field.args && field.args.find(arg => arg.name === argName);
677
+ }
678
+
679
+ /**
680
+ * @internal
681
+ *
682
+ * Validates that the selection is a valid connection:
683
+ * - Specifies a first or last argument to prevent accidental, unconstrained
684
+ * data access.
685
+ * - Has an `edges` selection, otherwise there is nothing to paginate.
686
+ *
687
+ * TODO: This implementation requires the edges field to be a direct selection
688
+ * and not contained within an inline fragment or fragment spread. It's
689
+ * technically possible to remove this restriction if this pattern becomes
690
+ * common/necessary.
691
+ */
692
+ function validateConnectionSelection(field: LinkedField): void {
693
+ const {EDGES} = ConnectionInterface.get();
694
+
695
+ if (!findArg(field, FIRST) && !findArg(field, LAST)) {
696
+ throw createUserError(
697
+ `Expected field '${field.name}' to have a '${FIRST}' or '${LAST}' ` +
698
+ 'argument.',
699
+ [field.loc],
700
+ );
701
+ }
702
+ if (
703
+ !field.selections.some(
704
+ selection => selection.kind === 'LinkedField' && selection.name === EDGES,
705
+ )
706
+ ) {
707
+ throw createUserError(
708
+ `Expected field '${field.name}' to have an '${EDGES}' selection.`,
709
+ [field.loc],
710
+ );
711
+ }
712
+ }
713
+
714
+ /**
715
+ * @internal
716
+ *
717
+ * Validates that the type satisfies the Connection specification:
718
+ * - The type has an edges field, and edges have scalar `cursor` and object
719
+ * `node` fields.
720
+ * - The type has a page info field which is an object with the correct
721
+ * subfields.
722
+ */
723
+ function validateConnectionType(
724
+ schema: Schema,
725
+ field: LinkedField,
726
+ nullableType: CompositeTypeID,
727
+ connectionDirective: Directive,
728
+ ): void {
729
+ const directiveName = connectionDirective.name;
730
+ const {
731
+ CURSOR,
732
+ EDGES,
733
+ END_CURSOR,
734
+ HAS_NEXT_PAGE,
735
+ HAS_PREV_PAGE,
736
+ NODE,
737
+ PAGE_INFO,
738
+ START_CURSOR,
739
+ } = ConnectionInterface.get();
740
+
741
+ const typeName = schema.getTypeString(nullableType);
742
+ if (!schema.hasField(nullableType, EDGES)) {
743
+ throw createUserError(
744
+ `@${directiveName} used on invalid field '${field.name}'. Expected the ` +
745
+ `field type '${typeName}' to have an '${EDGES}' field`,
746
+ [field.loc],
747
+ );
748
+ }
749
+
750
+ const edges = schema.getFieldConfig(schema.expectField(nullableType, EDGES));
751
+
752
+ const edgesType = schema.getNullableType(edges.type);
753
+ if (!schema.isList(edgesType)) {
754
+ throw createUserError(
755
+ `@${directiveName} used on invalid field '${field.name}'. Expected the ` +
756
+ `field type '${typeName}' to have an '${EDGES}' field that returns ` +
757
+ 'a list of objects.',
758
+ [field.loc],
759
+ );
760
+ }
761
+ let edgeType = schema.getNullableType(schema.getListItemType(edgesType));
762
+ if (!schema.isObject(edgeType) && !schema.isInterface(edgeType)) {
763
+ throw createUserError(
764
+ `@${directiveName} used on invalid field '${field.name}'. Expected the ` +
765
+ `field type '${typeName}' to have an '${EDGES}' field that returns ` +
766
+ 'a list of objects.',
767
+ [field.loc],
768
+ );
769
+ }
770
+ edgeType = schema.assertCompositeType(edgeType);
771
+
772
+ if (!schema.hasField(edgeType, NODE)) {
773
+ throw createUserError(
774
+ `@${directiveName} used on invalid field '${field.name}'. Expected the ` +
775
+ `field type '${typeName}' to have an '${EDGES} { ${NODE} }' field ` +
776
+ 'that returns an object, interface, or union.',
777
+ [field.loc],
778
+ );
779
+ }
780
+ const node = schema.getFieldConfig(schema.expectField(edgeType, NODE));
781
+
782
+ const nodeType = schema.getNullableType(node.type);
783
+ if (!(schema.isAbstractType(nodeType) || schema.isObject(nodeType))) {
784
+ throw createUserError(
785
+ `@${directiveName} used on invalid field '${field.name}'. Expected the ` +
786
+ `field type '${typeName}' to have an '${EDGES} { ${NODE} }' field ` +
787
+ 'that returns an object, interface, or union.',
788
+ [field.loc],
789
+ );
790
+ }
791
+
792
+ if (!schema.hasField(edgeType, CURSOR)) {
793
+ throw createUserError(
794
+ `@${directiveName} used on invalid field '${field.name}'. Expected the ` +
795
+ `field type '${typeName}' to have an '${EDGES} { ${CURSOR} }' field ` +
796
+ 'that returns a scalar value.',
797
+ [field.loc],
798
+ );
799
+ }
800
+ const cursor = schema.getFieldConfig(schema.expectField(edgeType, CURSOR));
801
+
802
+ if (!schema.isScalar(schema.getNullableType(cursor.type))) {
803
+ throw createUserError(
804
+ `@${directiveName} used on invalid field '${field.name}'. Expected the ` +
805
+ `field type '${typeName}' to have an '${EDGES} { ${CURSOR} }' field ` +
806
+ 'that returns a scalar value.',
807
+ [field.loc],
808
+ );
809
+ }
810
+
811
+ if (!schema.hasField(nullableType, PAGE_INFO)) {
812
+ throw createUserError(
813
+ `@${directiveName} used on invalid field '${field.name}'. Expected the ` +
814
+ `field type '${typeName}' to have a '${PAGE_INFO}' field that returns ` +
815
+ 'an object.',
816
+ [field.loc],
817
+ );
818
+ }
819
+
820
+ const pageInfo = schema.getFieldConfig(
821
+ schema.expectField(nullableType, PAGE_INFO),
822
+ );
823
+
824
+ const pageInfoType = schema.getNullableType(pageInfo.type);
825
+ if (!schema.isObject(pageInfoType)) {
826
+ throw createUserError(
827
+ `@${directiveName} used on invalid field '${field.name}'. Expected the ` +
828
+ `field type '${typeName}' to have a '${PAGE_INFO}' field that ` +
829
+ 'returns an object.',
830
+ [field.loc],
831
+ );
832
+ }
833
+
834
+ [END_CURSOR, HAS_NEXT_PAGE, HAS_PREV_PAGE, START_CURSOR].forEach(
835
+ fieldName => {
836
+ const pageInfoField = schema.getFieldConfig(
837
+ schema.expectField(schema.assertObjectType(pageInfoType), fieldName),
838
+ );
839
+ if (!schema.isScalar(schema.getNullableType(pageInfoField.type))) {
840
+ throw createUserError(
841
+ `@${directiveName} used on invalid field '${field.name}'. Expected ` +
842
+ `the field type '${typeName}' to have a '${PAGE_INFO} { ${fieldName} }' ` +
843
+ 'field returns a scalar.',
844
+ [field.loc],
845
+ );
846
+ }
847
+ },
848
+ );
849
+ }
850
+
851
+ module.exports = {
852
+ buildConnectionMetadata,
853
+ CONNECTION,
854
+ SCHEMA_EXTENSION,
855
+ transform: connectionTransform,
856
+ };