relay-compiler 9.0.0 → 9.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (197) hide show
  1. package/bin/RelayCompilerBin.js.flow +169 -0
  2. package/bin/RelayCompilerMain.js.flow +508 -0
  3. package/bin/__fixtures__/plugin-module.js.flow +17 -0
  4. package/bin/relay-compiler +2002 -1733
  5. package/codegen/CodegenDirectory.js.flow +375 -0
  6. package/codegen/CodegenRunner.js.flow +431 -0
  7. package/codegen/CodegenTypes.js.flow +28 -0
  8. package/codegen/CodegenWatcher.js.flow +254 -0
  9. package/codegen/NormalizationCodeGenerator.js.flow +499 -0
  10. package/codegen/ReaderCodeGenerator.js.flow +453 -0
  11. package/codegen/RelayCodeGenerator.js.flow +76 -0
  12. package/codegen/RelayFileWriter.js.flow +366 -0
  13. package/codegen/SourceControl.js.flow +58 -0
  14. package/codegen/compileRelayArtifacts.js.flow +182 -0
  15. package/codegen/createPrintRequireModuleDependency.js.flow +21 -0
  16. package/codegen/writeRelayGeneratedFile.js.flow +194 -0
  17. package/core/ASTCache.js.flow +73 -0
  18. package/core/ASTConvert.js.flow +233 -0
  19. package/core/CompilerContext.js.flow +190 -0
  20. package/core/CompilerError.js.flow +250 -0
  21. package/core/DotGraphQLParser.js.flow +39 -0
  22. package/core/GraphQLCompilerProfiler.js.flow +341 -0
  23. package/core/GraphQLDerivedFromMetadata.js.flow +48 -0
  24. package/core/GraphQLWatchmanClient.js.flow +111 -0
  25. package/core/IR.js.flow +329 -0
  26. package/core/IRPrinter.js.flow +488 -0
  27. package/core/IRTransformer.js.flow +377 -0
  28. package/core/IRValidator.js.flow +260 -0
  29. package/core/IRVisitor.js.flow +150 -0
  30. package/core/JSModuleParser.js.flow +24 -0
  31. package/core/RelayCompilerScope.js.flow +199 -0
  32. package/core/RelayFindGraphQLTags.js.flow +119 -0
  33. package/core/RelayGraphQLEnumsGenerator.js.flow +55 -0
  34. package/core/RelayIRTransforms.js.flow +130 -0
  35. package/core/RelayParser.js.flow +1759 -0
  36. package/core/RelaySourceModuleParser.js.flow +135 -0
  37. package/core/Schema.js.flow +1985 -0
  38. package/core/SchemaUtils.js.flow +109 -0
  39. package/core/filterContextForNode.js.flow +50 -0
  40. package/core/getFieldDefinition.js.flow +156 -0
  41. package/core/getIdentifierForArgumentValue.js.flow +49 -0
  42. package/core/getIdentifierForSelection.js.flow +69 -0
  43. package/core/getLiteralArgumentValues.js.flow +32 -0
  44. package/core/getNormalizationOperationName.js.flow +19 -0
  45. package/core/inferRootArgumentDefinitions.js.flow +323 -0
  46. package/index.js +1 -1
  47. package/index.js.flow +202 -0
  48. package/language/RelayLanguagePluginInterface.js.flow +283 -0
  49. package/language/javascript/FindGraphQLTags.js.flow +233 -0
  50. package/language/javascript/RelayFlowBabelFactories.js.flow +180 -0
  51. package/language/javascript/RelayFlowGenerator.js.flow +1040 -0
  52. package/language/javascript/RelayFlowTypeTransformers.js.flow +184 -0
  53. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +34 -0
  54. package/language/javascript/formatGeneratedModule.js.flow +65 -0
  55. package/lib/bin/RelayCompilerBin.js +10 -0
  56. package/lib/bin/RelayCompilerMain.js +113 -119
  57. package/lib/codegen/CodegenDirectory.js +2 -6
  58. package/lib/codegen/CodegenRunner.js +34 -75
  59. package/lib/codegen/CodegenWatcher.js +13 -21
  60. package/lib/codegen/NormalizationCodeGenerator.js +43 -40
  61. package/lib/codegen/ReaderCodeGenerator.js +43 -35
  62. package/lib/codegen/RelayCodeGenerator.js +7 -5
  63. package/lib/codegen/RelayFileWriter.js +15 -36
  64. package/lib/codegen/compileRelayArtifacts.js +16 -30
  65. package/lib/codegen/writeRelayGeneratedFile.js +56 -98
  66. package/lib/core/ASTCache.js +1 -3
  67. package/lib/core/CompilerContext.js +1 -3
  68. package/lib/core/CompilerError.js +27 -54
  69. package/lib/core/GraphQLCompilerProfiler.js +8 -12
  70. package/lib/core/GraphQLWatchmanClient.js +4 -12
  71. package/lib/core/IRPrinter.js +5 -5
  72. package/lib/core/IRTransformer.js +8 -6
  73. package/lib/core/IRValidator.js +1 -3
  74. package/lib/core/RelayCompilerScope.js +4 -4
  75. package/lib/core/RelayGraphQLEnumsGenerator.js +12 -15
  76. package/lib/core/RelayIRTransforms.js +9 -5
  77. package/lib/core/RelayParser.js +44 -56
  78. package/lib/core/RelaySourceModuleParser.js +1 -3
  79. package/lib/core/Schema.js +78 -65
  80. package/lib/core/getFieldDefinition.js +12 -15
  81. package/lib/core/getIdentifierForSelection.js +1 -1
  82. package/lib/core/inferRootArgumentDefinitions.js +27 -36
  83. package/lib/language/javascript/RelayFlowGenerator.js +49 -78
  84. package/lib/language/javascript/RelayFlowTypeTransformers.js +1 -3
  85. package/lib/reporters/ConsoleReporter.js +1 -3
  86. package/lib/reporters/MultiReporter.js +1 -3
  87. package/lib/runner/Artifacts.js +69 -170
  88. package/lib/runner/BufferedFilesystem.js +32 -66
  89. package/lib/runner/GraphQLASTNodeGroup.js +54 -120
  90. package/lib/runner/GraphQLNodeMap.js +14 -19
  91. package/lib/runner/Sources.js +49 -78
  92. package/lib/runner/StrictMap.js +20 -36
  93. package/lib/runner/getChangedNodeNames.js +30 -62
  94. package/lib/transforms/ApplyFragmentArgumentTransform.js +33 -28
  95. package/lib/transforms/ClientExtensionsTransform.js +7 -12
  96. package/lib/transforms/ConnectionTransform.js +26 -24
  97. package/lib/transforms/DeferStreamTransform.js +20 -16
  98. package/lib/transforms/DisallowTypenameOnRoot.js +55 -0
  99. package/lib/transforms/FieldHandleTransform.js +6 -2
  100. package/lib/transforms/FlattenTransform.js +19 -14
  101. package/lib/transforms/GenerateIDFieldTransform.js +7 -3
  102. package/lib/transforms/GenerateTypeNameTransform.js +6 -2
  103. package/lib/transforms/InlineDataFragmentTransform.js +7 -3
  104. package/lib/transforms/MaskTransform.js +17 -17
  105. package/lib/transforms/MatchTransform.js +110 -32
  106. package/lib/transforms/RefetchableFragmentTransform.js +21 -17
  107. package/lib/transforms/RelayDirectiveTransform.js +11 -3
  108. package/lib/transforms/SkipClientExtensionsTransform.js +8 -0
  109. package/lib/transforms/SkipHandleFieldTransform.js +6 -2
  110. package/lib/transforms/SkipRedundantNodesTransform.js +6 -2
  111. package/lib/transforms/SkipSplitOperationTransform.js +32 -0
  112. package/lib/transforms/SkipUnreachableNodeTransform.js +9 -2
  113. package/lib/transforms/SkipUnusedVariablesTransform.js +18 -17
  114. package/lib/transforms/SplitModuleImportTransform.js +2 -2
  115. package/lib/transforms/TestOperationTransform.js +7 -3
  116. package/lib/transforms/ValidateGlobalVariablesTransform.js +18 -30
  117. package/lib/transforms/ValidateRequiredArgumentsTransform.js +12 -15
  118. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +16 -30
  119. package/lib/transforms/ValidateUnusedVariablesTransform.js +18 -30
  120. package/lib/transforms/query-generators/FetchableQueryGenerator.js +161 -0
  121. package/lib/transforms/query-generators/NodeQueryGenerator.js +7 -2
  122. package/lib/transforms/query-generators/QueryQueryGenerator.js +1 -0
  123. package/lib/transforms/query-generators/ViewerQueryGenerator.js +1 -0
  124. package/lib/transforms/query-generators/index.js +23 -6
  125. package/lib/transforms/query-generators/utils.js +12 -15
  126. package/lib/util/RelayCompilerCache.js +1 -3
  127. package/lib/util/dedupeJSONStringify.js +15 -12
  128. package/lib/util/getModuleName.js +1 -1
  129. package/package.json +2 -2
  130. package/relay-compiler.js +4 -4
  131. package/relay-compiler.min.js +4 -4
  132. package/reporters/ConsoleReporter.js.flow +81 -0
  133. package/reporters/MultiReporter.js.flow +43 -0
  134. package/reporters/Reporter.js.flow +19 -0
  135. package/runner/Artifacts.js.flow +219 -0
  136. package/runner/BufferedFilesystem.js.flow +194 -0
  137. package/runner/GraphQLASTNodeGroup.js.flow +176 -0
  138. package/runner/GraphQLASTUtils.js.flow +26 -0
  139. package/runner/GraphQLNodeMap.js.flow +55 -0
  140. package/runner/Sources.js.flow +218 -0
  141. package/runner/StrictMap.js.flow +96 -0
  142. package/runner/compileArtifacts.js.flow +76 -0
  143. package/runner/extractAST.js.flow +100 -0
  144. package/runner/getChangedNodeNames.js.flow +48 -0
  145. package/runner/getSchemaInstance.js.flow +36 -0
  146. package/runner/types.js.flow +37 -0
  147. package/transforms/ApplyFragmentArgumentTransform.js.flow +474 -0
  148. package/transforms/ClientExtensionsTransform.js.flow +220 -0
  149. package/transforms/ConnectionTransform.js.flow +869 -0
  150. package/transforms/DeferStreamTransform.js.flow +258 -0
  151. package/transforms/DisallowIdAsAlias.js.flow +47 -0
  152. package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
  153. package/transforms/FieldHandleTransform.js.flow +80 -0
  154. package/transforms/FilterDirectivesTransform.js.flow +45 -0
  155. package/transforms/FlattenTransform.js.flow +456 -0
  156. package/transforms/GenerateIDFieldTransform.js.flow +134 -0
  157. package/transforms/GenerateTypeNameTransform.js.flow +81 -0
  158. package/transforms/InlineDataFragmentTransform.js.flow +124 -0
  159. package/transforms/InlineFragmentsTransform.js.flow +71 -0
  160. package/transforms/MaskTransform.js.flow +126 -0
  161. package/transforms/MatchTransform.js.flow +583 -0
  162. package/transforms/RefetchableFragmentTransform.js.flow +272 -0
  163. package/transforms/RelayDirectiveTransform.js.flow +99 -0
  164. package/transforms/SkipClientExtensionsTransform.js.flow +54 -0
  165. package/transforms/SkipHandleFieldTransform.js.flow +44 -0
  166. package/transforms/SkipRedundantNodesTransform.js.flow +253 -0
  167. package/transforms/SkipSplitOperationTransform.js.flow +37 -0
  168. package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
  169. package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
  170. package/transforms/SplitModuleImportTransform.js.flow +98 -0
  171. package/transforms/TestOperationTransform.js.flow +138 -0
  172. package/transforms/TransformUtils.js.flow +26 -0
  173. package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
  174. package/transforms/ValidateRequiredArgumentsTransform.js.flow +127 -0
  175. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +112 -0
  176. package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
  177. package/transforms/query-generators/FetchableQueryGenerator.js.flow +190 -0
  178. package/transforms/query-generators/NodeQueryGenerator.js.flow +206 -0
  179. package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
  180. package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
  181. package/transforms/query-generators/index.js.flow +90 -0
  182. package/transforms/query-generators/utils.js.flow +72 -0
  183. package/util/CodeMarker.js.flow +79 -0
  184. package/util/DefaultHandleKey.js.flow +17 -0
  185. package/util/RelayCompilerCache.js.flow +88 -0
  186. package/util/Rollout.js.flow +39 -0
  187. package/util/TimeReporter.js.flow +79 -0
  188. package/util/areEqualOSS.js.flow +123 -0
  189. package/util/dedupeJSONStringify.js.flow +152 -0
  190. package/util/getDefinitionNodeHash.js.flow +25 -0
  191. package/util/getModuleName.js.flow +39 -0
  192. package/util/joinArgumentDefinitions.js.flow +99 -0
  193. package/util/md5.js.flow +22 -0
  194. package/util/murmurHash.js.flow +94 -0
  195. package/util/nullthrowsOSS.js.flow +25 -0
  196. package/util/orList.js.flow +37 -0
  197. package/util/partitionArray.js.flow +37 -0
@@ -0,0 +1,138 @@
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
+ * @emails oncall+relay
10
+ */
11
+
12
+ // flowlint ambiguous-object-type:error
13
+
14
+ 'use strict';
15
+
16
+ const IRTransformer = require('../core/IRTransformer');
17
+
18
+ import type CompilerContext from '../core/CompilerContext';
19
+ import type {Fragment, Root} from '../core/IR';
20
+ import type {Schema, TypeID} from '../core/Schema';
21
+
22
+ // The purpose of this directive is to add GraphQL type inform for fields in
23
+ // the operation selection in order to use in in RelayMockPayloadGenerator
24
+ // to generate better mock values, and expand the API of MockResolvers
25
+ const SCHEMA_EXTENSION =
26
+ 'directive @relay_test_operation on QUERY | MUTATION | SUBSCRIPTION';
27
+
28
+ function testOperationDirective(context: CompilerContext): CompilerContext {
29
+ return IRTransformer.transform(context, {
30
+ Fragment: node => node,
31
+ Root: visitRoot,
32
+ SplitOperation: node => node,
33
+ });
34
+ }
35
+
36
+ type TypeDetails = {|
37
+ +type: string,
38
+ +plural: boolean,
39
+ +nullable: boolean,
40
+ +enumValues: null | $ReadOnlyArray<string>,
41
+ |};
42
+
43
+ function getTypeDetails(schema: Schema, fieldType: TypeID): TypeDetails {
44
+ const nullableType = schema.getNullableType(fieldType);
45
+ const isNullable = !schema.isNonNull(fieldType);
46
+ const isPlural = schema.isList(nullableType);
47
+ const type = schema.getRawType(nullableType);
48
+
49
+ return {
50
+ type: schema.getTypeString(type),
51
+ enumValues: schema.isEnum(type)
52
+ ? schema.getEnumValues(schema.assertEnumType(type))
53
+ : null,
54
+ plural: isPlural,
55
+ nullable: isNullable,
56
+ };
57
+ }
58
+
59
+ function visitRoot(node: Root) {
60
+ const schema: Schema = this.getContext().getSchema();
61
+ const testDirective = node.directives.find(
62
+ directive => directive.name === 'relay_test_operation',
63
+ );
64
+ if (testDirective == null) {
65
+ return node;
66
+ }
67
+
68
+ const context = this.getContext();
69
+ const queue = [
70
+ {
71
+ selections: node.selections,
72
+ path: null,
73
+ },
74
+ ];
75
+ const selectionsTypeInfo = {};
76
+ while (queue.length > 0) {
77
+ const {selections: currentSelections, path} = queue.pop();
78
+ currentSelections.forEach(selection => {
79
+ switch (selection.kind) {
80
+ case 'FragmentSpread':
81
+ const fragment: ?Fragment = context.get(selection.name);
82
+ if (fragment != null) {
83
+ queue.unshift({
84
+ selections: fragment.selections,
85
+ path,
86
+ });
87
+ }
88
+ break;
89
+ case 'ScalarField': {
90
+ const nextPath =
91
+ path === null ? selection.alias : `${path}.${selection.alias}`;
92
+ selectionsTypeInfo[nextPath] = getTypeDetails(schema, selection.type);
93
+ break;
94
+ }
95
+ case 'LinkedField': {
96
+ const nextPath =
97
+ path === null ? selection.alias : `${path}.${selection.alias}`;
98
+ selectionsTypeInfo[nextPath] = getTypeDetails(schema, selection.type);
99
+ queue.unshift({
100
+ selections: selection.selections,
101
+ path: nextPath,
102
+ });
103
+ break;
104
+ }
105
+ case 'Condition':
106
+ case 'ClientExtension':
107
+ case 'Defer':
108
+ case 'InlineDataFragmentSpread':
109
+ case 'InlineFragment':
110
+ case 'ModuleImport':
111
+ case 'Stream':
112
+ queue.unshift({
113
+ selections: selection.selections,
114
+ path,
115
+ });
116
+ break;
117
+ default:
118
+ (selection: empty);
119
+ break;
120
+ }
121
+ });
122
+ }
123
+ return {
124
+ ...node,
125
+ directives: node.directives.filter(
126
+ directive => directive !== testDirective,
127
+ ),
128
+ metadata: {
129
+ ...(node.metadata || {}),
130
+ relayTestingSelectionTypeInfo: selectionsTypeInfo,
131
+ },
132
+ };
133
+ }
134
+
135
+ module.exports = {
136
+ SCHEMA_EXTENSION,
137
+ transform: testOperationDirective,
138
+ };
@@ -0,0 +1,26 @@
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 {LinkedField} from '../core/IR';
16
+
17
+ function hasUnaliasedSelection(field: LinkedField, fieldName: string): boolean {
18
+ return field.selections.some(
19
+ selection =>
20
+ selection.kind === 'ScalarField' &&
21
+ selection.alias === fieldName &&
22
+ selection.name === fieldName,
23
+ );
24
+ }
25
+
26
+ module.exports = {hasUnaliasedSelection};
@@ -0,0 +1,81 @@
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 inferRootArgumentDefinitions = require('../core/inferRootArgumentDefinitions');
16
+
17
+ const {
18
+ createUserError,
19
+ eachWithCombinedError,
20
+ } = require('../core/CompilerError');
21
+
22
+ import type CompilerContext from '../core/CompilerContext';
23
+ import type {ArgumentDefinition} from '../core/IR';
24
+
25
+ /**
26
+ * Validates that all global variables used in operations are defined at the
27
+ * root. This isn't a real transform as it returns the original context, but
28
+ * has to happen before other transforms strip certain variable usages.
29
+ */
30
+ function validateGlobalVariablesTransform(
31
+ context: CompilerContext,
32
+ ): CompilerContext {
33
+ const contextWithUsedArguments = inferRootArgumentDefinitions(context);
34
+ eachWithCombinedError(context.documents(), node => {
35
+ if (node.kind !== 'Root') {
36
+ return;
37
+ }
38
+ const nodeWithUsedArguments = contextWithUsedArguments.getRoot(node.name);
39
+ const definedArguments = argumentDefinitionsToMap(node.argumentDefinitions);
40
+ const usedArguments = argumentDefinitionsToMap(
41
+ nodeWithUsedArguments.argumentDefinitions,
42
+ );
43
+ // All used arguments must be defined
44
+ const undefinedVariables = [];
45
+ for (const argDef of usedArguments.values()) {
46
+ if (!definedArguments.has(argDef.name)) {
47
+ undefinedVariables.push(argDef);
48
+ }
49
+ }
50
+ if (undefinedVariables.length !== 0) {
51
+ throw createUserError(
52
+ `Operation '${
53
+ node.name
54
+ }' references undefined variable(s):\n${undefinedVariables
55
+ .map(
56
+ argDef =>
57
+ `- \$${argDef.name}: ${context
58
+ .getSchema()
59
+ .getTypeString(argDef.type)}`,
60
+ )
61
+ .join('\n')}.`,
62
+ undefinedVariables.map(argDef => argDef.loc),
63
+ );
64
+ }
65
+ });
66
+ return context;
67
+ }
68
+
69
+ function argumentDefinitionsToMap<T: ArgumentDefinition>(
70
+ argDefs: $ReadOnlyArray<T>,
71
+ ): Map<string, T> {
72
+ const map = new Map();
73
+ for (const argDef of argDefs) {
74
+ map.set(argDef.name, argDef);
75
+ }
76
+ return map;
77
+ }
78
+
79
+ module.exports = {
80
+ transform: validateGlobalVariablesTransform,
81
+ };
@@ -0,0 +1,127 @@
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 IRValidator = require('../core/IRValidator');
16
+
17
+ const {createUserError} = require('../core/CompilerError');
18
+ const {getFieldDefinitionStrict} = require('../core/getFieldDefinition');
19
+
20
+ import type CompilerContext from '../core/CompilerContext';
21
+ import type {
22
+ Directive,
23
+ Field,
24
+ Fragment,
25
+ Root,
26
+ SplitOperation,
27
+ } from '../core/IR';
28
+ import type {Schema, TypeID, Argument} from '../core/Schema';
29
+
30
+ type State = {|
31
+ +rootNode: Fragment | Root | SplitOperation,
32
+ +parentType: TypeID,
33
+ |};
34
+
35
+ /*
36
+ * Validate required arguments are provided after transforms filling in arguments
37
+ */
38
+ function validateRequiredArguments(context: CompilerContext): CompilerContext {
39
+ IRValidator.validate(
40
+ context,
41
+ {
42
+ Directive: visitDirective,
43
+ InlineFragment: visitInlineFragment,
44
+ LinkedField: visitField,
45
+ ScalarField: visitField,
46
+ // FragmentSpread validation is done in ApplyFragmentArgumentTransform
47
+ },
48
+ node => ({rootNode: node, parentType: node.type}),
49
+ );
50
+ return context;
51
+ }
52
+
53
+ function visitDirective(node: Directive, {rootNode}: State): void {
54
+ const context: CompilerContext = this.getContext();
55
+ const directiveDef = context.getSchema().getDirective(node.name);
56
+ if (directiveDef == null) {
57
+ return;
58
+ }
59
+ validateRequiredArgumentsOnNode(
60
+ context.getSchema(),
61
+ node,
62
+ directiveDef.args,
63
+ rootNode,
64
+ );
65
+ }
66
+
67
+ function visitInlineFragment(fragment, {rootNode}: State): void {
68
+ this.traverse(fragment, {
69
+ rootNode,
70
+ parentType: fragment.typeCondition,
71
+ });
72
+ }
73
+
74
+ function visitField(node: Field, {parentType, rootNode}: State): void {
75
+ const context: CompilerContext = this.getContext();
76
+ const schema = context.getSchema();
77
+ const definition = getFieldDefinitionStrict(schema, parentType, node.name);
78
+ if (definition == null) {
79
+ const isLegacyFatInterface = node.directives.some(
80
+ directive => directive.name === 'fixme_fat_interface',
81
+ );
82
+ if (!isLegacyFatInterface) {
83
+ throw createUserError(
84
+ `Unknown field '${node.name}' on type ` +
85
+ `'${schema.getTypeString(parentType)}'.`,
86
+ [node.loc],
87
+ );
88
+ }
89
+ } else {
90
+ validateRequiredArgumentsOnNode(
91
+ schema,
92
+ node,
93
+ schema.getFieldConfig(definition).args,
94
+ rootNode,
95
+ );
96
+ }
97
+ this.traverse(node, {
98
+ rootNode,
99
+ parentType: node.type,
100
+ });
101
+ }
102
+
103
+ function validateRequiredArgumentsOnNode(
104
+ schema: Schema,
105
+ node: Directive | Field,
106
+ definitionArgs: $ReadOnlyArray<Argument>,
107
+ rootNode: Fragment | Root | SplitOperation,
108
+ ): void {
109
+ const nodeArgsSet = new Set(node.args.map(arg => arg.name));
110
+ for (const arg of definitionArgs) {
111
+ if (
112
+ arg.defaultValue == null &&
113
+ schema.isNonNull(arg.type) &&
114
+ !nodeArgsSet.has(arg.name)
115
+ ) {
116
+ throw createUserError(
117
+ `Required argument '${arg.name}: ${schema.getTypeString(arg.type)}' ` +
118
+ `is missing on '${node.name}' in '${rootNode.name}'.`,
119
+ [node.loc, rootNode.loc],
120
+ );
121
+ }
122
+ }
123
+ }
124
+
125
+ module.exports = {
126
+ transform: validateRequiredArguments,
127
+ };
@@ -0,0 +1,112 @@
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 IRValidator = require('../core/IRValidator');
16
+
17
+ const {createUserError} = require('../core/CompilerError');
18
+
19
+ import type CompilerContext from '../core/CompilerContext';
20
+ import type {
21
+ ClientExtension,
22
+ Defer,
23
+ LinkedField,
24
+ Selection,
25
+ Stream,
26
+ } from '../core/IR';
27
+
28
+ type State = {rootClientSelection: ?Selection, ...};
29
+
30
+ const NODEKIND_DIRECTIVE_MAP = {
31
+ Defer: 'defer',
32
+ Stream: 'stream',
33
+ };
34
+
35
+ /*
36
+ * Validate that server-only directives are not used inside client fields
37
+ */
38
+ function validateServerOnlyDirectives(
39
+ context: CompilerContext,
40
+ ): CompilerContext {
41
+ IRValidator.validate(
42
+ context,
43
+ {
44
+ ClientExtension: visitClientExtension,
45
+ Defer: visitTransformedDirective,
46
+ Stream: visitTransformedDirective,
47
+ LinkedField: visitLinkedField,
48
+ ScalarField: stopVisit,
49
+ },
50
+ () => ({
51
+ rootClientSelection: null,
52
+ }),
53
+ );
54
+ return context;
55
+ }
56
+
57
+ // If an empty visitor is defined, we no longer automatically visit child nodes
58
+ // such as arguments.
59
+ function stopVisit() {}
60
+
61
+ // Only visits selections as an optimization to not look at arguments
62
+ function visitLinkedField(node: LinkedField, state: State): void {
63
+ for (const selection of node.selections) {
64
+ this.visit(selection, state);
65
+ }
66
+ }
67
+
68
+ function visitClientExtension(node: ClientExtension, state: State): void {
69
+ for (const selection of node.selections) {
70
+ this.visit(selection, {
71
+ rootClientSelection: selection,
72
+ });
73
+ }
74
+ }
75
+
76
+ function visitTransformedDirective(node: Defer | Stream, state: State): void {
77
+ if (state.rootClientSelection) {
78
+ throwError(
79
+ `@${NODEKIND_DIRECTIVE_MAP[node.kind]}`,
80
+ node.loc,
81
+ state.rootClientSelection.loc,
82
+ );
83
+ }
84
+ // directive used only on client fields
85
+ if (node.selections.every(sel => sel.kind === 'ClientExtension')) {
86
+ const clientExtension = node.selections[0];
87
+ throwError(
88
+ `@${NODEKIND_DIRECTIVE_MAP[node.kind]}`,
89
+ node.loc,
90
+ clientExtension && clientExtension.kind === 'ClientExtension'
91
+ ? clientExtension.selections[0]?.loc
92
+ : null,
93
+ );
94
+ }
95
+ this.traverse(node, state);
96
+ }
97
+
98
+ function throwError(directiveName, directiveLoc, clientExtensionLoc) {
99
+ throw createUserError(
100
+ `Unexpected directive: ${directiveName}. ` +
101
+ 'This directive can only be used on fields/fragments that are ' +
102
+ 'fetched from the server schema, but it is used ' +
103
+ 'inside a client-only selection.',
104
+ clientExtensionLoc == null || directiveLoc === clientExtensionLoc
105
+ ? [directiveLoc]
106
+ : [directiveLoc, clientExtensionLoc],
107
+ );
108
+ }
109
+
110
+ module.exports = {
111
+ transform: validateServerOnlyDirectives,
112
+ };
@@ -0,0 +1,89 @@
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 inferRootArgumentDefinitions = require('../core/inferRootArgumentDefinitions');
16
+
17
+ const {
18
+ createUserError,
19
+ eachWithCombinedError,
20
+ } = require('../core/CompilerError');
21
+
22
+ import type CompilerContext from '../core/CompilerContext';
23
+ import type {ArgumentDefinition} from '../core/IR';
24
+
25
+ const SCHEMA_EXTENSION =
26
+ 'directive @DEPRECATED__relay_ignore_unused_variables_error on QUERY | MUTATION | SUBSCRIPTION';
27
+
28
+ /**
29
+ * Validates that there are no unused variables in the operation.
30
+ * former `graphql-js`` NoUnusedVariablesRule
31
+ */
32
+ function validateUnusedVariablesTransform(
33
+ context: CompilerContext,
34
+ ): CompilerContext {
35
+ const contextWithUsedArguments = inferRootArgumentDefinitions(context);
36
+ eachWithCombinedError(context.documents(), node => {
37
+ if (node.kind !== 'Root') {
38
+ return;
39
+ }
40
+ const rootArgumentLocations = new Map(
41
+ node.argumentDefinitions.map(arg => [arg.name, arg.loc]),
42
+ );
43
+ const nodeWithUsedArguments = contextWithUsedArguments.getRoot(node.name);
44
+ const usedArguments = argumentDefinitionsToMap(
45
+ nodeWithUsedArguments.argumentDefinitions,
46
+ );
47
+ for (const usedArgumentName of usedArguments.keys()) {
48
+ rootArgumentLocations.delete(usedArgumentName);
49
+ }
50
+
51
+ const ignoreErrorDirective = node.directives.find(
52
+ ({name}) => name === 'DEPRECATED__relay_ignore_unused_variables_error',
53
+ );
54
+ if (rootArgumentLocations.size > 0 && !ignoreErrorDirective) {
55
+ const isPlural = rootArgumentLocations.size > 1;
56
+ throw createUserError(
57
+ `Variable${isPlural ? 's' : ''} '$${Array.from(
58
+ rootArgumentLocations.keys(),
59
+ ).join("', '$")}' ${isPlural ? 'are' : 'is'} never used in operation '${
60
+ node.name
61
+ }'.`,
62
+ Array.from(rootArgumentLocations.values()),
63
+ );
64
+ }
65
+ if (rootArgumentLocations.size === 0 && ignoreErrorDirective) {
66
+ throw createUserError(
67
+ "Invalid usage of '@DEPRECATED__relay_ignore_unused_variables_error.'" +
68
+ `No unused variables found in the query '${node.name}'`,
69
+ [ignoreErrorDirective.loc],
70
+ );
71
+ }
72
+ });
73
+ return context;
74
+ }
75
+
76
+ function argumentDefinitionsToMap<T: ArgumentDefinition>(
77
+ argDefs: $ReadOnlyArray<T>,
78
+ ): Map<string, T> {
79
+ const map = new Map();
80
+ for (const argDef of argDefs) {
81
+ map.set(argDef.name, argDef);
82
+ }
83
+ return map;
84
+ }
85
+
86
+ module.exports = {
87
+ transform: validateUnusedVariablesTransform,
88
+ SCHEMA_EXTENSION,
89
+ };