relay-compiler 11.0.2 → 13.0.0-rc.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (261) hide show
  1. package/README.md +108 -0
  2. package/cli.js +23 -0
  3. package/index.js +22 -4
  4. package/linux-x64/relay +0 -0
  5. package/macos-arm64/relay +0 -0
  6. package/macos-x64/relay +0 -0
  7. package/package.json +2 -26
  8. package/win-x64/relay.exe +0 -0
  9. package/bin/RelayCompilerBin.js.flow +0 -169
  10. package/bin/RelayCompilerMain.js.flow +0 -515
  11. package/bin/__fixtures__/plugin-module.js.flow +0 -17
  12. package/bin/relay-compiler +0 -19008
  13. package/codegen/CodegenDirectory.js.flow +0 -375
  14. package/codegen/CodegenRunner.js.flow +0 -432
  15. package/codegen/CodegenTypes.js.flow +0 -28
  16. package/codegen/CodegenWatcher.js.flow +0 -254
  17. package/codegen/NormalizationCodeGenerator.js.flow +0 -571
  18. package/codegen/ReaderCodeGenerator.js.flow +0 -512
  19. package/codegen/RelayCodeGenerator.js.flow +0 -85
  20. package/codegen/RelayFileWriter.js.flow +0 -367
  21. package/codegen/SourceControl.js.flow +0 -58
  22. package/codegen/compileRelayArtifacts.js.flow +0 -182
  23. package/codegen/createPrintRequireModuleDependency.js.flow +0 -21
  24. package/codegen/sortObjectByKey.js.flow +0 -25
  25. package/codegen/writeRelayGeneratedFile.js.flow +0 -239
  26. package/core/ASTCache.js.flow +0 -74
  27. package/core/ASTConvert.js.flow +0 -233
  28. package/core/CompilerContext.js.flow +0 -191
  29. package/core/CompilerError.js.flow +0 -255
  30. package/core/DotGraphQLParser.js.flow +0 -39
  31. package/core/GraphQLCompilerProfiler.js.flow +0 -341
  32. package/core/GraphQLDerivedFromMetadata.js.flow +0 -36
  33. package/core/GraphQLWatchmanClient.js.flow +0 -111
  34. package/core/IR.js.flow +0 -326
  35. package/core/IRPrinter.js.flow +0 -478
  36. package/core/IRTransformer.js.flow +0 -377
  37. package/core/IRValidator.js.flow +0 -260
  38. package/core/IRVisitor.js.flow +0 -150
  39. package/core/JSModuleParser.js.flow +0 -24
  40. package/core/RelayCompilerScope.js.flow +0 -199
  41. package/core/RelayFindGraphQLTags.js.flow +0 -119
  42. package/core/RelayGraphQLEnumsGenerator.js.flow +0 -55
  43. package/core/RelayIRTransforms.js.flow +0 -138
  44. package/core/RelayParser.js.flow +0 -1734
  45. package/core/RelaySourceModuleParser.js.flow +0 -135
  46. package/core/Schema.js.flow +0 -2037
  47. package/core/SchemaUtils.js.flow +0 -120
  48. package/core/filterContextForNode.js.flow +0 -50
  49. package/core/getFieldDefinition.js.flow +0 -156
  50. package/core/getIdentifierForArgumentValue.js.flow +0 -49
  51. package/core/getIdentifierForSelection.js.flow +0 -69
  52. package/core/getLiteralArgumentValues.js.flow +0 -32
  53. package/core/getNormalizationOperationName.js.flow +0 -19
  54. package/core/inferRootArgumentDefinitions.js.flow +0 -323
  55. package/index.js.flow +0 -200
  56. package/language/RelayLanguagePluginInterface.js.flow +0 -283
  57. package/language/javascript/FindGraphQLTags.js.flow +0 -137
  58. package/language/javascript/RelayFlowBabelFactories.js.flow +0 -176
  59. package/language/javascript/RelayFlowGenerator.js.flow +0 -1100
  60. package/language/javascript/RelayFlowTypeTransformers.js.flow +0 -184
  61. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +0 -34
  62. package/language/javascript/formatGeneratedModule.js.flow +0 -65
  63. package/lib/bin/RelayCompilerBin.js +0 -143
  64. package/lib/bin/RelayCompilerMain.js +0 -486
  65. package/lib/bin/__fixtures__/plugin-module.js +0 -16
  66. package/lib/codegen/CodegenDirectory.js +0 -336
  67. package/lib/codegen/CodegenRunner.js +0 -433
  68. package/lib/codegen/CodegenTypes.js +0 -11
  69. package/lib/codegen/CodegenWatcher.js +0 -271
  70. package/lib/codegen/NormalizationCodeGenerator.js +0 -481
  71. package/lib/codegen/ReaderCodeGenerator.js +0 -472
  72. package/lib/codegen/RelayCodeGenerator.js +0 -68
  73. package/lib/codegen/RelayFileWriter.js +0 -270
  74. package/lib/codegen/SourceControl.js +0 -60
  75. package/lib/codegen/compileRelayArtifacts.js +0 -157
  76. package/lib/codegen/createPrintRequireModuleDependency.js +0 -19
  77. package/lib/codegen/sortObjectByKey.js +0 -41
  78. package/lib/codegen/writeRelayGeneratedFile.js +0 -208
  79. package/lib/core/ASTCache.js +0 -70
  80. package/lib/core/ASTConvert.js +0 -198
  81. package/lib/core/CompilerContext.js +0 -165
  82. package/lib/core/CompilerError.js +0 -251
  83. package/lib/core/DotGraphQLParser.js +0 -40
  84. package/lib/core/GraphQLCompilerProfiler.js +0 -299
  85. package/lib/core/GraphQLDerivedFromMetadata.js +0 -31
  86. package/lib/core/GraphQLWatchmanClient.js +0 -150
  87. package/lib/core/IR.js +0 -11
  88. package/lib/core/IRPrinter.js +0 -389
  89. package/lib/core/IRTransformer.js +0 -345
  90. package/lib/core/IRValidator.js +0 -226
  91. package/lib/core/IRVisitor.js +0 -45
  92. package/lib/core/JSModuleParser.js +0 -18
  93. package/lib/core/RelayCompilerScope.js +0 -149
  94. package/lib/core/RelayFindGraphQLTags.js +0 -79
  95. package/lib/core/RelayGraphQLEnumsGenerator.js +0 -50
  96. package/lib/core/RelayIRTransforms.js +0 -109
  97. package/lib/core/RelayParser.js +0 -1382
  98. package/lib/core/RelaySourceModuleParser.js +0 -104
  99. package/lib/core/Schema.js +0 -1877
  100. package/lib/core/SchemaUtils.js +0 -98
  101. package/lib/core/filterContextForNode.js +0 -49
  102. package/lib/core/getFieldDefinition.js +0 -145
  103. package/lib/core/getIdentifierForArgumentValue.js +0 -53
  104. package/lib/core/getIdentifierForSelection.js +0 -48
  105. package/lib/core/getLiteralArgumentValues.js +0 -26
  106. package/lib/core/getNormalizationOperationName.js +0 -17
  107. package/lib/core/inferRootArgumentDefinitions.js +0 -351
  108. package/lib/index.js +0 -178
  109. package/lib/language/RelayLanguagePluginInterface.js +0 -14
  110. package/lib/language/javascript/FindGraphQLTags.js +0 -126
  111. package/lib/language/javascript/RelayFlowBabelFactories.js +0 -160
  112. package/lib/language/javascript/RelayFlowGenerator.js +0 -857
  113. package/lib/language/javascript/RelayFlowTypeTransformers.js +0 -119
  114. package/lib/language/javascript/RelayLanguagePluginJavaScript.js +0 -30
  115. package/lib/language/javascript/formatGeneratedModule.js +0 -36
  116. package/lib/reporters/ConsoleReporter.js +0 -61
  117. package/lib/reporters/MultiReporter.js +0 -45
  118. package/lib/reporters/Reporter.js +0 -11
  119. package/lib/runner/Artifacts.js +0 -323
  120. package/lib/runner/BufferedFilesystem.js +0 -261
  121. package/lib/runner/GraphQLASTNodeGroup.js +0 -256
  122. package/lib/runner/GraphQLASTUtils.js +0 -23
  123. package/lib/runner/GraphQLNodeMap.js +0 -81
  124. package/lib/runner/Sources.js +0 -271
  125. package/lib/runner/StrictMap.js +0 -134
  126. package/lib/runner/compileArtifacts.js +0 -39
  127. package/lib/runner/extractAST.js +0 -77
  128. package/lib/runner/getChangedNodeNames.js +0 -82
  129. package/lib/runner/getSchemaInstance.js +0 -30
  130. package/lib/runner/types.js +0 -12
  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 -41
  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 -44
  154. package/lib/transforms/SkipRedundantNodesTransform.js +0 -254
  155. package/lib/transforms/SkipSplitOperationTransform.js +0 -32
  156. package/lib/transforms/SkipUnreachableNodeTransform.js +0 -158
  157. package/lib/transforms/SkipUnusedVariablesTransform.js +0 -74
  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 -91
  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 -98
  174. package/lib/util/Rollout.js +0 -40
  175. package/lib/util/TimeReporter.js +0 -83
  176. package/lib/util/areEqualArgValues.js +0 -134
  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 -66
  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 -219
  194. package/runner/BufferedFilesystem.js.flow +0 -194
  195. package/runner/GraphQLASTNodeGroup.js.flow +0 -176
  196. package/runner/GraphQLASTUtils.js.flow +0 -26
  197. package/runner/GraphQLNodeMap.js.flow +0 -55
  198. package/runner/Sources.js.flow +0 -228
  199. package/runner/StrictMap.js.flow +0 -96
  200. package/runner/compileArtifacts.js.flow +0 -76
  201. package/runner/extractAST.js.flow +0 -100
  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/transforms/ApplyFragmentArgumentTransform.js.flow +0 -526
  206. package/transforms/ClientExtensionsTransform.js.flow +0 -225
  207. package/transforms/ConnectionTransform.js.flow +0 -855
  208. package/transforms/DeclarativeConnectionMutationTransform.js.flow +0 -247
  209. package/transforms/DeferStreamTransform.js.flow +0 -265
  210. package/transforms/DisallowIdAsAlias.js.flow +0 -47
  211. package/transforms/DisallowTypenameOnRoot.js.flow +0 -45
  212. package/transforms/FieldHandleTransform.js.flow +0 -79
  213. package/transforms/FilterCompilerDirectivesTransform.js.flow +0 -33
  214. package/transforms/FilterDirectivesTransform.js.flow +0 -45
  215. package/transforms/FlattenTransform.js.flow +0 -460
  216. package/transforms/GenerateIDFieldTransform.js.flow +0 -152
  217. package/transforms/GenerateTypeNameTransform.js.flow +0 -161
  218. package/transforms/InlineDataFragmentTransform.js.flow +0 -125
  219. package/transforms/InlineFragmentsTransform.js.flow +0 -71
  220. package/transforms/MaskTransform.js.flow +0 -126
  221. package/transforms/MatchTransform.js.flow +0 -589
  222. package/transforms/ReactFlightComponentTransform.js.flow +0 -195
  223. package/transforms/RefetchableFragmentTransform.js.flow +0 -272
  224. package/transforms/RelayDirectiveTransform.js.flow +0 -97
  225. package/transforms/RequiredFieldTransform.js.flow +0 -415
  226. package/transforms/SkipClientExtensionsTransform.js.flow +0 -54
  227. package/transforms/SkipHandleFieldTransform.js.flow +0 -44
  228. package/transforms/SkipRedundantNodesTransform.js.flow +0 -258
  229. package/transforms/SkipSplitOperationTransform.js.flow +0 -37
  230. package/transforms/SkipUnreachableNodeTransform.js.flow +0 -149
  231. package/transforms/SkipUnusedVariablesTransform.js.flow +0 -59
  232. package/transforms/SplitModuleImportTransform.js.flow +0 -98
  233. package/transforms/TestOperationTransform.js.flow +0 -142
  234. package/transforms/TransformUtils.js.flow +0 -26
  235. package/transforms/ValidateGlobalVariablesTransform.js.flow +0 -81
  236. package/transforms/ValidateRequiredArgumentsTransform.js.flow +0 -127
  237. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +0 -112
  238. package/transforms/ValidateUnusedVariablesTransform.js.flow +0 -89
  239. package/transforms/query-generators/FetchableQueryGenerator.js.flow +0 -189
  240. package/transforms/query-generators/NodeQueryGenerator.js.flow +0 -219
  241. package/transforms/query-generators/QueryQueryGenerator.js.flow +0 -57
  242. package/transforms/query-generators/ViewerQueryGenerator.js.flow +0 -97
  243. package/transforms/query-generators/index.js.flow +0 -90
  244. package/transforms/query-generators/utils.js.flow +0 -76
  245. package/util/CodeMarker.js.flow +0 -79
  246. package/util/DefaultHandleKey.js.flow +0 -17
  247. package/util/RelayCompilerCache.js.flow +0 -88
  248. package/util/Rollout.js.flow +0 -39
  249. package/util/TimeReporter.js.flow +0 -79
  250. package/util/areEqualArgValues.js.flow +0 -125
  251. package/util/argumentContainsVariables.js.flow +0 -38
  252. package/util/dedupeJSONStringify.js.flow +0 -152
  253. package/util/generateAbstractTypeRefinementKey.js.flow +0 -29
  254. package/util/getDefinitionNodeHash.js.flow +0 -25
  255. package/util/getModuleName.js.flow +0 -39
  256. package/util/joinArgumentDefinitions.js.flow +0 -105
  257. package/util/md5.js.flow +0 -22
  258. package/util/murmurHash.js.flow +0 -94
  259. package/util/nullthrowsOSS.js.flow +0 -25
  260. package/util/orList.js.flow +0 -37
  261. package/util/partitionArray.js.flow +0 -37
@@ -1,415 +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
- 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
- };
@@ -1,54 +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
- const context: CompilerContext = this.getContext();
32
- if (context.getSchema().isServerType(node.type)) {
33
- return this.traverse(node);
34
- }
35
- return null;
36
- }
37
-
38
- function vistFragmentSpread(node: FragmentSpread): ?FragmentSpread {
39
- const context: CompilerContext = this.getContext();
40
- const fragment = context.getFragment(node.name, node.loc);
41
- const isServer = context.getSchema().isServerType(fragment.type);
42
- return isServer ? node : null;
43
- }
44
-
45
- function visitClientExtension(
46
- node: ClientExtension,
47
- state: void,
48
- ): ?ClientExtension {
49
- return null;
50
- }
51
-
52
- module.exports = {
53
- transform: skipClientExtensionTransform,
54
- };
@@ -1,44 +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
- const transformedNode = this.traverse(field);
33
- if (transformedNode.handles) {
34
- return {
35
- ...transformedNode,
36
- handles: null,
37
- };
38
- }
39
- return transformedNode;
40
- }
41
-
42
- module.exports = {
43
- transform: skipHandleFieldTransform,
44
- };