relay-compiler 10.0.1 → 10.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. package/bin/relay-compiler +1347 -604
  2. package/codegen/NormalizationCodeGenerator.js.flow +12 -4
  3. package/codegen/ReaderCodeGenerator.js.flow +38 -3
  4. package/codegen/RelayFileWriter.js.flow +2 -0
  5. package/codegen/writeRelayGeneratedFile.js.flow +1 -1
  6. package/core/ASTCache.js.flow +1 -0
  7. package/core/CompilerContext.js.flow +1 -0
  8. package/core/IR.js.flow +0 -1
  9. package/core/IRPrinter.js.flow +3 -8
  10. package/core/RelayIRTransforms.js.flow +7 -0
  11. package/core/Schema.js.flow +55 -1
  12. package/index.js +1 -1
  13. package/language/javascript/FindGraphQLTags.js.flow +2 -97
  14. package/language/javascript/RelayFlowBabelFactories.js.flow +11 -15
  15. package/language/javascript/RelayFlowGenerator.js.flow +76 -19
  16. package/language/javascript/RelayFlowTypeTransformers.js.flow +4 -4
  17. package/lib/bin/RelayCompilerMain.js +5 -5
  18. package/lib/codegen/CodegenRunner.js +2 -2
  19. package/lib/codegen/NormalizationCodeGenerator.js +20 -8
  20. package/lib/codegen/ReaderCodeGenerator.js +43 -8
  21. package/lib/codegen/RelayFileWriter.js +2 -2
  22. package/lib/codegen/compileRelayArtifacts.js +2 -2
  23. package/lib/codegen/sortObjectByKey.js +2 -2
  24. package/lib/codegen/writeRelayGeneratedFile.js +2 -2
  25. package/lib/core/ASTCache.js +1 -0
  26. package/lib/core/CompilerContext.js +1 -0
  27. package/lib/core/CompilerError.js +2 -2
  28. package/lib/core/IRPrinter.js +3 -4
  29. package/lib/core/RelayGraphQLEnumsGenerator.js +2 -2
  30. package/lib/core/RelayIRTransforms.js +10 -4
  31. package/lib/core/RelayParser.js +4 -4
  32. package/lib/core/Schema.js +48 -7
  33. package/lib/core/getFieldDefinition.js +2 -2
  34. package/lib/core/inferRootArgumentDefinitions.js +4 -4
  35. package/lib/language/javascript/FindGraphQLTags.js +3 -69
  36. package/lib/language/javascript/RelayFlowBabelFactories.js +5 -5
  37. package/lib/language/javascript/RelayFlowGenerator.js +80 -21
  38. package/lib/runner/Artifacts.js +2 -2
  39. package/lib/runner/BufferedFilesystem.js +2 -2
  40. package/lib/runner/GraphQLASTNodeGroup.js +2 -2
  41. package/lib/runner/GraphQLNodeMap.js +2 -2
  42. package/lib/runner/Sources.js +21 -2
  43. package/lib/runner/StrictMap.js +2 -2
  44. package/lib/runner/getChangedNodeNames.js +2 -2
  45. package/lib/transforms/ApplyFragmentArgumentTransform.js +14 -14
  46. package/lib/transforms/ClientExtensionsTransform.js +5 -3
  47. package/lib/transforms/ConnectionTransform.js +8 -9
  48. package/lib/transforms/DeclarativeConnectionMutationTransform.js +113 -55
  49. package/lib/transforms/DeferStreamTransform.js +2 -2
  50. package/lib/transforms/DisallowTypenameOnRoot.js +2 -2
  51. package/lib/transforms/FieldHandleTransform.js +2 -2
  52. package/lib/transforms/FilterCompilerDirectivesTransform.js +29 -0
  53. package/lib/transforms/FlattenTransform.js +13 -12
  54. package/lib/transforms/GenerateIDFieldTransform.js +2 -2
  55. package/lib/transforms/GenerateTypeNameTransform.js +3 -3
  56. package/lib/transforms/InlineDataFragmentTransform.js +2 -2
  57. package/lib/transforms/MaskTransform.js +3 -3
  58. package/lib/transforms/MatchTransform.js +7 -3
  59. package/lib/transforms/ReactFlightComponentTransform.js +162 -0
  60. package/lib/transforms/RefetchableFragmentTransform.js +4 -4
  61. package/lib/transforms/RelayDirectiveTransform.js +2 -2
  62. package/lib/transforms/RequiredFieldTransform.js +380 -0
  63. package/lib/transforms/SkipHandleFieldTransform.js +1 -1
  64. package/lib/transforms/SkipRedundantNodesTransform.js +3 -1
  65. package/lib/transforms/SkipUnreachableNodeTransform.js +1 -1
  66. package/lib/transforms/SkipUnusedVariablesTransform.js +3 -3
  67. package/lib/transforms/TestOperationTransform.js +2 -2
  68. package/lib/transforms/ValidateGlobalVariablesTransform.js +2 -2
  69. package/lib/transforms/ValidateRequiredArgumentsTransform.js +2 -2
  70. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +2 -2
  71. package/lib/transforms/ValidateUnusedVariablesTransform.js +2 -2
  72. package/lib/transforms/query-generators/FetchableQueryGenerator.js +1 -1
  73. package/lib/transforms/query-generators/NodeQueryGenerator.js +1 -1
  74. package/lib/transforms/query-generators/index.js +2 -2
  75. package/lib/transforms/query-generators/utils.js +2 -2
  76. package/package.json +3 -3
  77. package/relay-compiler.js +4 -4
  78. package/relay-compiler.min.js +4 -4
  79. package/runner/Sources.js.flow +14 -0
  80. package/transforms/ClientExtensionsTransform.js.flow +2 -0
  81. package/transforms/ConnectionTransform.js.flow +0 -1
  82. package/transforms/DeclarativeConnectionMutationTransform.js.flow +137 -48
  83. package/transforms/FieldHandleTransform.js.flow +0 -1
  84. package/transforms/FilterCompilerDirectivesTransform.js.flow +33 -0
  85. package/transforms/FlattenTransform.js.flow +3 -2
  86. package/transforms/MatchTransform.js.flow +6 -0
  87. package/transforms/ReactFlightComponentTransform.js.flow +195 -0
  88. package/transforms/RequiredFieldTransform.js.flow +415 -0
  89. package/transforms/SkipRedundantNodesTransform.js.flow +3 -0
@@ -0,0 +1,415 @@
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
+
17
+ const partitionArray = require('../util/partitionArray');
18
+
19
+ const {createUserError, createCompilerError} = require('../core/CompilerError');
20
+ const {RelayFeatureFlags} = require('relay-runtime');
21
+
22
+ import type CompilerContext from '../core/CompilerContext';
23
+ import type {
24
+ LinkedField,
25
+ ScalarField,
26
+ Field,
27
+ Location,
28
+ InlineFragment,
29
+ Fragment,
30
+ Root,
31
+ Metadata,
32
+ } from '../core/IR';
33
+ import type {Schema} from '../core/Schema';
34
+ import type {RequiredFieldAction} from 'relay-runtime';
35
+
36
+ type Path = string;
37
+ type Alias = string;
38
+
39
+ type PathRequiredMap = Map<Path, Field>;
40
+
41
+ export type RequiredDirectiveMetadata = {|
42
+ action: RequiredFieldAction,
43
+ actionLoc: Location,
44
+ directiveLoc: Location,
45
+ path: string,
46
+ |};
47
+
48
+ type State = {|
49
+ schema: Schema,
50
+ documentName: string,
51
+ path: Array<string>,
52
+ pathRequiredMap: PathRequiredMap,
53
+ currentNodeRequiredChildren: Map<Alias, Field>,
54
+ requiredChildrenMap: Map<Path, Map<Alias, Field>>,
55
+ parentAbstractInlineFragment: ?InlineFragment,
56
+ |};
57
+
58
+ const SCHEMA_EXTENSION = `
59
+ enum RequiredFieldAction {
60
+ NONE
61
+ LOG
62
+ THROW
63
+ }
64
+ directive @required(
65
+ action: RequiredFieldAction!
66
+ ) on FIELD
67
+ `;
68
+
69
+ /**
70
+ * This transform rewrites ScalarField and LinkedField nodes with a @required
71
+ * directive into fields with the directives stripped and sets the `required`
72
+ * and `path` metadata values.
73
+ */
74
+ function requiredFieldTransform(context: CompilerContext): CompilerContext {
75
+ const schema = context.getSchema();
76
+ return IRTransformer.transform(
77
+ context,
78
+ {
79
+ LinkedField: visitLinkedField,
80
+ ScalarField: vistitScalarField,
81
+ InlineFragment: visitInlineFragment,
82
+ Fragment: visitFragment,
83
+ Root: visitRoot,
84
+ },
85
+ node => ({
86
+ schema,
87
+ documentName: node.name,
88
+ path: [],
89
+ pathRequiredMap: new Map(),
90
+ currentNodeRequiredChildren: new Map(),
91
+ requiredChildrenMap: new Map(),
92
+ parentAbstractInlineFragment: null,
93
+ }),
94
+ );
95
+ }
96
+
97
+ function visitFragment(fragment: Fragment, state: State) {
98
+ return addChildrenCanBubbleMetadata(this.traverse(fragment, state), state);
99
+ }
100
+
101
+ function visitRoot(root: Root, state: State) {
102
+ return addChildrenCanBubbleMetadata(this.traverse(root, state), state);
103
+ }
104
+
105
+ function visitInlineFragment(fragment: InlineFragment, state: State) {
106
+ // Ideally we could allow @required when the direct parent inline fragment was
107
+ // on a concrete type, but we would need to solve this bug in our Flow type
108
+ // generation first: T65695438
109
+ const parentAbstractInlineFragment =
110
+ state.parentAbstractInlineFragment ??
111
+ getAbstractInlineFragment(fragment, state.schema);
112
+
113
+ return this.traverse(fragment, {...state, parentAbstractInlineFragment});
114
+ }
115
+
116
+ function getAbstractInlineFragment(
117
+ fragment: InlineFragment,
118
+ schema: Schema,
119
+ ): ?InlineFragment {
120
+ const {typeCondition} = fragment;
121
+ if (schema.isAbstractType(typeCondition)) {
122
+ return fragment;
123
+ }
124
+ return null;
125
+ }
126
+
127
+ // Convert action to a number so that we can numerically compare their severity.
128
+ function getActionSeverity(action: RequiredFieldAction): number {
129
+ switch (action) {
130
+ case 'NONE':
131
+ return 0;
132
+ case 'LOG':
133
+ return 1;
134
+ case 'THROW':
135
+ return 2;
136
+ default:
137
+ (action: empty);
138
+ throw createCompilerError(`Unhandled action type ${action}`);
139
+ }
140
+ }
141
+
142
+ function visitLinkedField(field: LinkedField, state: State): LinkedField {
143
+ const path = [...state.path, field.alias];
144
+ const newState = {
145
+ ...state,
146
+ currentNodeRequiredChildren: new Map(),
147
+ path,
148
+ parentAbstractInlineFragment: null,
149
+ };
150
+
151
+ let newField = this.traverse(field, newState);
152
+
153
+ const pathName = path.join('.');
154
+ assertCompatibleRequiredChildren(field, pathName, newState);
155
+ newField = applyDirectives(newField, pathName, state.documentName);
156
+ assertCompatibleNullability(newField, pathName, newState.pathRequiredMap);
157
+
158
+ const directiveMetadata = getRequiredDirectiveMetadata(newField);
159
+ if (directiveMetadata != null) {
160
+ assertParentIsNotInvalidInlineFragmet(
161
+ state.schema,
162
+ directiveMetadata,
163
+ state.parentAbstractInlineFragment,
164
+ );
165
+ state.currentNodeRequiredChildren.set(field.alias, newField);
166
+
167
+ const severity = getActionSeverity(directiveMetadata.action);
168
+
169
+ // Assert that all @required children have at least this severity.
170
+ newState.currentNodeRequiredChildren.forEach(childField => {
171
+ const childMetadata = getRequiredDirectiveMetadata(childField);
172
+ if (childMetadata == null) {
173
+ return;
174
+ }
175
+ if (getActionSeverity(childMetadata.action) < severity) {
176
+ throw createUserError(
177
+ `The @required field [1] may not have an \`action\` less severe than that of its @required parent [2]. [1] should probably be \`action: ${directiveMetadata.action}\`.`,
178
+ [childMetadata.actionLoc, directiveMetadata.actionLoc],
179
+ );
180
+ }
181
+ });
182
+ }
183
+
184
+ state.requiredChildrenMap.set(pathName, newState.currentNodeRequiredChildren);
185
+ return addChildrenCanBubbleMetadata(newField, newState);
186
+ }
187
+
188
+ function vistitScalarField(field: ScalarField, state: State): ScalarField {
189
+ const pathName = [...state.path, field.alias].join('.');
190
+ const newField = applyDirectives(field, pathName, state.documentName);
191
+ const directiveMetadata = getRequiredDirectiveMetadata(newField);
192
+ if (directiveMetadata != null) {
193
+ assertParentIsNotInvalidInlineFragmet(
194
+ state.schema,
195
+ directiveMetadata,
196
+ state.parentAbstractInlineFragment,
197
+ );
198
+ state.currentNodeRequiredChildren.set(field.alias, newField);
199
+ }
200
+ assertCompatibleNullability(newField, pathName, state.pathRequiredMap);
201
+ return newField;
202
+ }
203
+
204
+ function addChildrenCanBubbleMetadata<T: {|+metadata: Metadata|}>(
205
+ node: T,
206
+ state: State,
207
+ ): T {
208
+ for (const child of state.currentNodeRequiredChildren.values()) {
209
+ const requiredMetadata = getRequiredDirectiveMetadata(child);
210
+ if (requiredMetadata != null && requiredMetadata.action !== 'THROW') {
211
+ const metadata = {...node.metadata, childrenCanBubbleNull: true};
212
+ return {...node, metadata};
213
+ }
214
+ }
215
+
216
+ return node;
217
+ }
218
+
219
+ function assertParentIsNotInvalidInlineFragmet(
220
+ schema: Schema,
221
+ directiveMetadata: RequiredDirectiveMetadata,
222
+ parentAbstractInlineFragment: ?InlineFragment,
223
+ ) {
224
+ if (parentAbstractInlineFragment == null) {
225
+ return;
226
+ }
227
+ const {typeCondition} = parentAbstractInlineFragment;
228
+ if (schema.isUnion(typeCondition)) {
229
+ throw createUserError(
230
+ 'The @required directive [1] may not be used anywhere within an inline fragment on a union type [2].',
231
+ [directiveMetadata.directiveLoc, parentAbstractInlineFragment.loc],
232
+ );
233
+ } else if (schema.isInterface(typeCondition)) {
234
+ throw createUserError(
235
+ 'The @required directive [1] may not be used anywhere within an inline fragment on an interface type [2].',
236
+ [directiveMetadata.directiveLoc, parentAbstractInlineFragment.loc],
237
+ );
238
+ } else {
239
+ throw createCompilerError('Unexpected abstract inline fragment type.', [
240
+ parentAbstractInlineFragment.loc,
241
+ ]);
242
+ }
243
+ }
244
+
245
+ // Check that this field's nullability matches all other instances.
246
+ function assertCompatibleNullability(
247
+ field: Field,
248
+ pathName: string,
249
+ pathRequiredMap: PathRequiredMap,
250
+ ): void {
251
+ const existingField = pathRequiredMap.get(pathName);
252
+ if (existingField == null) {
253
+ pathRequiredMap.set(pathName, field);
254
+ return;
255
+ }
256
+
257
+ const requiredMetadata = getRequiredDirectiveMetadata(field);
258
+ const existingRequiredMetadata = getRequiredDirectiveMetadata(existingField);
259
+
260
+ if (requiredMetadata?.action === existingRequiredMetadata?.action) {
261
+ return;
262
+ }
263
+
264
+ if (requiredMetadata == null) {
265
+ throw createUserError(
266
+ `The field "${field.alias}" is @required in [1] but not in [2].`,
267
+ [existingField.loc, field.loc],
268
+ );
269
+ }
270
+ if (existingRequiredMetadata == null) {
271
+ throw createUserError(
272
+ `The field "${field.alias}" is @required in [1] but not in [2].`,
273
+ [field.loc, existingField.loc],
274
+ );
275
+ }
276
+ throw createUserError(
277
+ `The field "${field.alias}" has a different @required action in [1] than in [2].`,
278
+ [requiredMetadata.actionLoc, existingRequiredMetadata.actionLoc],
279
+ );
280
+ }
281
+
282
+ // Metadata is untyped, so we use this utility function to do the type coersion.
283
+ function getRequiredDirectiveMetadata(
284
+ field: Field,
285
+ ): ?RequiredDirectiveMetadata {
286
+ return (field.metadata?.required: $FlowFixMe);
287
+ }
288
+
289
+ // Check that this field has the same required children as all other instances.
290
+ function assertCompatibleRequiredChildren(
291
+ field: LinkedField,
292
+ fieldPath: string,
293
+ {currentNodeRequiredChildren, pathRequiredMap, requiredChildrenMap}: State,
294
+ ) {
295
+ const previouslyRequiredChildren = requiredChildrenMap.get(fieldPath);
296
+
297
+ if (previouslyRequiredChildren == null) {
298
+ return;
299
+ }
300
+
301
+ // Check if this field has a required child field which was previously omitted.
302
+ for (const [path, childField] of currentNodeRequiredChildren) {
303
+ if (!previouslyRequiredChildren.has(path)) {
304
+ const otherParent = pathRequiredMap.get(fieldPath);
305
+ if (otherParent == null) {
306
+ throw createCompilerError(
307
+ `Could not find other parent node at path "${fieldPath}".`,
308
+ [childField.loc],
309
+ );
310
+ }
311
+ throw createMissingRequiredFieldError(childField, otherParent);
312
+ }
313
+ }
314
+
315
+ // Check if a previous reference to this field had a required child field which we are missing.
316
+ for (const [path, childField] of previouslyRequiredChildren) {
317
+ if (!currentNodeRequiredChildren.has(path)) {
318
+ throw createMissingRequiredFieldError(childField, field);
319
+ }
320
+ }
321
+ }
322
+
323
+ function createMissingRequiredFieldError(
324
+ requiredChild: Field,
325
+ missingParent: Field,
326
+ ) {
327
+ const {alias} = requiredChild;
328
+ return createUserError(
329
+ `The field "${alias}" is marked as @required in [1] but is missing in [2].`,
330
+ [requiredChild.loc, missingParent.loc],
331
+ );
332
+ }
333
+
334
+ // TODO T74397896: Remove prefix gating once @required is rolled out more broadly.
335
+ function featureIsEnabled(documentName: string): boolean {
336
+ const featureFlag = RelayFeatureFlags.ENABLE_REQUIRED_DIRECTIVES;
337
+ if (typeof featureFlag === 'boolean') {
338
+ return featureFlag;
339
+ } else if (featureFlag === 'LIMITED') {
340
+ return documentName.startsWith('RelayRequiredTest');
341
+ } else if (typeof featureFlag === 'string') {
342
+ return featureFlag
343
+ .split('|')
344
+ .some(prefix => documentName.startsWith(prefix));
345
+ }
346
+ return false;
347
+ }
348
+
349
+ // Strip and validate @required directives, and convert them to metadata.
350
+ function applyDirectives<T: ScalarField | LinkedField>(
351
+ field: T,
352
+ pathName: string,
353
+ documentName: string,
354
+ ): T {
355
+ const [requiredDirectives, otherDirectives] = partitionArray(
356
+ field.directives,
357
+ directive => directive.name === 'required',
358
+ );
359
+
360
+ if (requiredDirectives.length === 0) {
361
+ return field;
362
+ }
363
+
364
+ if (!featureIsEnabled(documentName)) {
365
+ throw new createUserError(
366
+ // Purposefully don't include details in this error message, since we
367
+ // don't want folks adopting this feature until it's been tested more.
368
+ 'The @required directive is experimental and not yet supported for use in product code',
369
+ requiredDirectives.map(x => x.loc),
370
+ );
371
+ }
372
+
373
+ if (requiredDirectives.length > 1) {
374
+ throw new createUserError(
375
+ 'Did not expect multiple @required directives.',
376
+ requiredDirectives.map(x => x.loc),
377
+ );
378
+ }
379
+
380
+ const requiredDirective = requiredDirectives[0];
381
+ const arg = requiredDirective.args[0];
382
+ // I would expect this check to be handled by the schema validation, but...
383
+ if (arg == null) {
384
+ throw createUserError(
385
+ 'The @required directive requires an `action` argument.',
386
+ [requiredDirective.loc],
387
+ );
388
+ }
389
+ if (arg.value.kind !== 'Literal') {
390
+ throw createUserError(
391
+ 'Expected @required `action` argument to be a literal.',
392
+ [arg.value.loc],
393
+ );
394
+ }
395
+
396
+ return {
397
+ ...field,
398
+ directives: otherDirectives,
399
+ metadata: {
400
+ ...field.metadata,
401
+ required: {
402
+ action: arg.value.value,
403
+ actionLoc: arg.loc,
404
+ directiveLoc: requiredDirective.loc,
405
+ path: pathName,
406
+ },
407
+ },
408
+ };
409
+ }
410
+
411
+ // Transform @required directive to metadata
412
+ module.exports = {
413
+ SCHEMA_EXTENSION,
414
+ transform: requiredFieldTransform,
415
+ };
@@ -30,6 +30,7 @@ import type {Fragment, Node, Root, SplitOperation, Selection} from '../core/IR';
30
30
  * or nested maps for items with subselections (linked fields, inline fragments,
31
31
  * etc).
32
32
  */
33
+ // $FlowFixMe[value-as-type]
33
34
  type SelectionMap = IMap<string, ?SelectionMap>;
34
35
 
35
36
  /**
@@ -168,6 +169,7 @@ function transformNode<T: Node>(
168
169
  const isEmptySelectionMap = selectionMap.size === 0;
169
170
  let result;
170
171
  if (isEmptySelectionMap) {
172
+ // $FlowFixMe[escaped-generic]
171
173
  result = cache.get(node);
172
174
  if (result != null) {
173
175
  return result;
@@ -229,6 +231,7 @@ function transformNode<T: Node>(
229
231
  const nextNode: any = selections.length ? {...node, selections} : null;
230
232
  result = {selectionMap, node: nextNode};
231
233
  if (isEmptySelectionMap) {
234
+ // $FlowFixMe[escaped-generic]
232
235
  cache.set(node, result);
233
236
  }
234
237
  return result;