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,419 +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
-
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
- // $FlowFixMe[incompatible-use]
99
- return addChildrenCanBubbleMetadata(this.traverse(fragment, state), state);
100
- }
101
-
102
- function visitRoot(root: Root, state: State) {
103
- // $FlowFixMe[incompatible-use]
104
- return addChildrenCanBubbleMetadata(this.traverse(root, state), state);
105
- }
106
-
107
- function visitInlineFragment(fragment: InlineFragment, state: State) {
108
- // Ideally we could allow @required when the direct parent inline fragment was
109
- // on a concrete type, but we would need to solve this bug in our Flow type
110
- // generation first: T65695438
111
- const parentAbstractInlineFragment =
112
- state.parentAbstractInlineFragment ??
113
- getAbstractInlineFragment(fragment, state.schema);
114
-
115
- // $FlowFixMe[incompatible-use]
116
- return this.traverse(fragment, {...state, parentAbstractInlineFragment});
117
- }
118
-
119
- function getAbstractInlineFragment(
120
- fragment: InlineFragment,
121
- schema: Schema,
122
- ): ?InlineFragment {
123
- const {typeCondition} = fragment;
124
- if (schema.isAbstractType(typeCondition)) {
125
- return fragment;
126
- }
127
- return null;
128
- }
129
-
130
- // Convert action to a number so that we can numerically compare their severity.
131
- function getActionSeverity(action: RequiredFieldAction): number {
132
- switch (action) {
133
- case 'NONE':
134
- return 0;
135
- case 'LOG':
136
- return 1;
137
- case 'THROW':
138
- return 2;
139
- default:
140
- (action: empty);
141
- throw createCompilerError(`Unhandled action type ${action}`);
142
- }
143
- }
144
-
145
- function visitLinkedField(field: LinkedField, state: State): LinkedField {
146
- const path = [...state.path, field.alias];
147
- const newState = {
148
- ...state,
149
- currentNodeRequiredChildren: new Map(),
150
- path,
151
- parentAbstractInlineFragment: null,
152
- };
153
-
154
- // $FlowFixMe[incompatible-use]
155
- let newField = this.traverse(field, newState);
156
-
157
- const pathName = path.join('.');
158
- assertCompatibleRequiredChildren(field, pathName, newState);
159
- newField = applyDirectives(newField, pathName, state.documentName);
160
- assertCompatibleNullability(newField, pathName, newState.pathRequiredMap);
161
-
162
- const directiveMetadata = getRequiredDirectiveMetadata(newField);
163
- if (directiveMetadata != null) {
164
- assertParentIsNotInvalidInlineFragmet(
165
- state.schema,
166
- directiveMetadata,
167
- state.parentAbstractInlineFragment,
168
- );
169
- state.currentNodeRequiredChildren.set(field.alias, newField);
170
-
171
- const severity = getActionSeverity(directiveMetadata.action);
172
-
173
- // Assert that all @required children have at least this severity.
174
- newState.currentNodeRequiredChildren.forEach(childField => {
175
- const childMetadata = getRequiredDirectiveMetadata(childField);
176
- if (childMetadata == null) {
177
- return;
178
- }
179
- if (getActionSeverity(childMetadata.action) < severity) {
180
- throw createUserError(
181
- `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}\`.`,
182
- [childMetadata.actionLoc, directiveMetadata.actionLoc],
183
- );
184
- }
185
- });
186
- }
187
-
188
- state.requiredChildrenMap.set(pathName, newState.currentNodeRequiredChildren);
189
- return addChildrenCanBubbleMetadata(newField, newState);
190
- }
191
-
192
- function vistitScalarField(field: ScalarField, state: State): ScalarField {
193
- const pathName = [...state.path, field.alias].join('.');
194
- const newField = applyDirectives(field, pathName, state.documentName);
195
- const directiveMetadata = getRequiredDirectiveMetadata(newField);
196
- if (directiveMetadata != null) {
197
- assertParentIsNotInvalidInlineFragmet(
198
- state.schema,
199
- directiveMetadata,
200
- state.parentAbstractInlineFragment,
201
- );
202
- state.currentNodeRequiredChildren.set(field.alias, newField);
203
- }
204
- assertCompatibleNullability(newField, pathName, state.pathRequiredMap);
205
- return newField;
206
- }
207
-
208
- function addChildrenCanBubbleMetadata<T: {|+metadata: Metadata|}>(
209
- node: T,
210
- state: State,
211
- ): T {
212
- for (const child of state.currentNodeRequiredChildren.values()) {
213
- const requiredMetadata = getRequiredDirectiveMetadata(child);
214
- if (requiredMetadata != null && requiredMetadata.action !== 'THROW') {
215
- const metadata = {...node.metadata, childrenCanBubbleNull: true};
216
- return {...node, metadata};
217
- }
218
- }
219
-
220
- return node;
221
- }
222
-
223
- function assertParentIsNotInvalidInlineFragmet(
224
- schema: Schema,
225
- directiveMetadata: RequiredDirectiveMetadata,
226
- parentAbstractInlineFragment: ?InlineFragment,
227
- ) {
228
- if (parentAbstractInlineFragment == null) {
229
- return;
230
- }
231
- const {typeCondition} = parentAbstractInlineFragment;
232
- if (schema.isUnion(typeCondition)) {
233
- throw createUserError(
234
- 'The @required directive [1] may not be used anywhere within an inline fragment on a union type [2].',
235
- [directiveMetadata.directiveLoc, parentAbstractInlineFragment.loc],
236
- );
237
- } else if (schema.isInterface(typeCondition)) {
238
- throw createUserError(
239
- 'The @required directive [1] may not be used anywhere within an inline fragment on an interface type [2].',
240
- [directiveMetadata.directiveLoc, parentAbstractInlineFragment.loc],
241
- );
242
- } else {
243
- throw createCompilerError('Unexpected abstract inline fragment type.', [
244
- parentAbstractInlineFragment.loc,
245
- ]);
246
- }
247
- }
248
-
249
- // Check that this field's nullability matches all other instances.
250
- function assertCompatibleNullability(
251
- field: Field,
252
- pathName: string,
253
- pathRequiredMap: PathRequiredMap,
254
- ): void {
255
- const existingField = pathRequiredMap.get(pathName);
256
- if (existingField == null) {
257
- pathRequiredMap.set(pathName, field);
258
- return;
259
- }
260
-
261
- const requiredMetadata = getRequiredDirectiveMetadata(field);
262
- const existingRequiredMetadata = getRequiredDirectiveMetadata(existingField);
263
-
264
- if (requiredMetadata?.action === existingRequiredMetadata?.action) {
265
- return;
266
- }
267
-
268
- if (requiredMetadata == null) {
269
- throw createUserError(
270
- `The field "${field.alias}" is @required in [1] but not in [2].`,
271
- [existingField.loc, field.loc],
272
- );
273
- }
274
- if (existingRequiredMetadata == null) {
275
- throw createUserError(
276
- `The field "${field.alias}" is @required in [1] but not in [2].`,
277
- [field.loc, existingField.loc],
278
- );
279
- }
280
- throw createUserError(
281
- `The field "${field.alias}" has a different @required action in [1] than in [2].`,
282
- [requiredMetadata.actionLoc, existingRequiredMetadata.actionLoc],
283
- );
284
- }
285
-
286
- // Metadata is untyped, so we use this utility function to do the type coersion.
287
- function getRequiredDirectiveMetadata(
288
- field: Field,
289
- ): ?RequiredDirectiveMetadata {
290
- return (field.metadata?.required: $FlowFixMe);
291
- }
292
-
293
- // Check that this field has the same required children as all other instances.
294
- function assertCompatibleRequiredChildren(
295
- field: LinkedField,
296
- fieldPath: string,
297
- {currentNodeRequiredChildren, pathRequiredMap, requiredChildrenMap}: State,
298
- ) {
299
- const previouslyRequiredChildren = requiredChildrenMap.get(fieldPath);
300
-
301
- if (previouslyRequiredChildren == null) {
302
- return;
303
- }
304
-
305
- // Check if this field has a required child field which was previously omitted.
306
- for (const [path, childField] of currentNodeRequiredChildren) {
307
- if (!previouslyRequiredChildren.has(path)) {
308
- const otherParent = pathRequiredMap.get(fieldPath);
309
- if (otherParent == null) {
310
- throw createCompilerError(
311
- `Could not find other parent node at path "${fieldPath}".`,
312
- [childField.loc],
313
- );
314
- }
315
- throw createMissingRequiredFieldError(childField, otherParent);
316
- }
317
- }
318
-
319
- // Check if a previous reference to this field had a required child field which we are missing.
320
- for (const [path, childField] of previouslyRequiredChildren) {
321
- if (!currentNodeRequiredChildren.has(path)) {
322
- throw createMissingRequiredFieldError(childField, field);
323
- }
324
- }
325
- }
326
-
327
- function createMissingRequiredFieldError(
328
- requiredChild: Field,
329
- missingParent: Field,
330
- ) {
331
- const {alias} = requiredChild;
332
- return createUserError(
333
- `The field "${alias}" is marked as @required in [1] but is missing in [2].`,
334
- [requiredChild.loc, missingParent.loc],
335
- );
336
- }
337
-
338
- // TODO T74397896: Remove prefix gating once @required is rolled out more broadly.
339
- function featureIsEnabled(documentName: string): boolean {
340
- const featureFlag = RelayFeatureFlags.ENABLE_REQUIRED_DIRECTIVES;
341
- if (typeof featureFlag === 'boolean') {
342
- return featureFlag;
343
- } else if (featureFlag === 'LIMITED') {
344
- return documentName.startsWith('RelayRequiredTest');
345
- } else if (typeof featureFlag === 'string') {
346
- return featureFlag
347
- .split('|')
348
- .some(prefix => documentName.startsWith(prefix));
349
- }
350
- return false;
351
- }
352
-
353
- // Strip and validate @required directives, and convert them to metadata.
354
- function applyDirectives<T: ScalarField | LinkedField>(
355
- field: T,
356
- pathName: string,
357
- documentName: string,
358
- ): T {
359
- const [requiredDirectives, otherDirectives] = partitionArray(
360
- field.directives,
361
- directive => directive.name === 'required',
362
- );
363
-
364
- if (requiredDirectives.length === 0) {
365
- return field;
366
- }
367
-
368
- if (!featureIsEnabled(documentName)) {
369
- throw new createUserError(
370
- // Purposefully don't include details in this error message, since we
371
- // don't want folks adopting this feature until it's been tested more.
372
- 'The @required directive is experimental and not yet supported for use in product code',
373
- requiredDirectives.map(x => x.loc),
374
- );
375
- }
376
-
377
- if (requiredDirectives.length > 1) {
378
- throw new createUserError(
379
- 'Did not expect multiple @required directives.',
380
- requiredDirectives.map(x => x.loc),
381
- );
382
- }
383
-
384
- const requiredDirective = requiredDirectives[0];
385
- const arg = requiredDirective.args[0];
386
- // I would expect this check to be handled by the schema validation, but...
387
- if (arg == null) {
388
- throw createUserError(
389
- 'The @required directive requires an `action` argument.',
390
- [requiredDirective.loc],
391
- );
392
- }
393
- if (arg.value.kind !== 'Literal') {
394
- throw createUserError(
395
- 'Expected @required `action` argument to be a literal.',
396
- [arg.value.loc],
397
- );
398
- }
399
-
400
- return {
401
- ...field,
402
- directives: otherDirectives,
403
- metadata: {
404
- ...field.metadata,
405
- required: {
406
- action: arg.value.value,
407
- actionLoc: arg.loc,
408
- directiveLoc: requiredDirective.loc,
409
- path: pathName,
410
- },
411
- },
412
- };
413
- }
414
-
415
- // Transform @required directive to metadata
416
- module.exports = {
417
- SCHEMA_EXTENSION,
418
- transform: requiredFieldTransform,
419
- };
@@ -1,57 +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
-
17
- import type CompilerContext from '../core/CompilerContext';
18
- import type {ClientExtension, Fragment, FragmentSpread} from '../core/IR';
19
-
20
- function skipClientExtensionTransform(
21
- context: CompilerContext,
22
- ): CompilerContext {
23
- return IRTransformer.transform(context, {
24
- Fragment: visitFragment,
25
- FragmentSpread: vistFragmentSpread,
26
- ClientExtension: visitClientExtension,
27
- });
28
- }
29
-
30
- function visitFragment(node: Fragment): ?Fragment {
31
- // $FlowFixMe[incompatible-use]
32
- const context: CompilerContext = this.getContext();
33
- if (context.getSchema().isServerType(node.type)) {
34
- // $FlowFixMe[incompatible-use]
35
- return this.traverse(node);
36
- }
37
- return null;
38
- }
39
-
40
- function vistFragmentSpread(node: FragmentSpread): ?FragmentSpread {
41
- // $FlowFixMe[incompatible-use]
42
- const context: CompilerContext = this.getContext();
43
- const fragment = context.getFragment(node.name, node.loc);
44
- const isServer = context.getSchema().isServerType(fragment.type);
45
- return isServer ? node : null;
46
- }
47
-
48
- function visitClientExtension(
49
- node: ClientExtension,
50
- state: void,
51
- ): ?ClientExtension {
52
- return null;
53
- }
54
-
55
- module.exports = {
56
- transform: skipClientExtensionTransform,
57
- };
@@ -1,45 +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
-
17
- import type CompilerContext from '../core/CompilerContext';
18
- import type {Field} from '../core/IR';
19
-
20
- /**
21
- * A transform that removes field `handles`. Intended for use when e.g.
22
- * printing queries to send to a GraphQL server.
23
- */
24
- function skipHandleFieldTransform(context: CompilerContext): CompilerContext {
25
- return IRTransformer.transform(context, {
26
- LinkedField: visitField,
27
- ScalarField: visitField,
28
- });
29
- }
30
-
31
- function visitField<F: Field>(field: F): ?F {
32
- // $FlowFixMe[incompatible-use]
33
- const transformedNode = this.traverse(field);
34
- if (transformedNode.handles) {
35
- return {
36
- ...transformedNode,
37
- handles: null,
38
- };
39
- }
40
- return transformedNode;
41
- }
42
-
43
- module.exports = {
44
- transform: skipHandleFieldTransform,
45
- };