relay-compiler 12.0.0 → 13.0.0

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