relay-compiler 0.0.0-main-f0b65f0b → 0.0.0-main-e0026a71

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