relay-compiler 7.0.0 → 9.1.0

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