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,198 +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
- * @emails oncall+relay
10
- */
11
-
12
- 'use strict';
13
-
14
- const IRTransformer = require('../core/IRTransformer');
15
-
16
- const {createUserError, createCompilerError} = require('../core/CompilerError');
17
- const {RelayFeatureFlags} = require('relay-runtime');
18
-
19
- import type CompilerContext from '../core/CompilerContext';
20
- import type {ScalarField} from '../core/IR';
21
- import type {TypeID, InputTypeID, ScalarFieldTypeID} from '../core/Schema';
22
-
23
- const FLIGHT_FIELD_COMPONENT_ARGUMENT_TYPE = 'String';
24
- const FLIGHT_FIELD_COMPONENT_ARGUMENT_NAME = 'component';
25
- const FLIGHT_FIELD_PROPS_ARGUMENT_NAME = 'props';
26
- const FLIGHT_FIELD_PROPS_TYPE = 'ReactFlightProps';
27
- const FLIGHT_FIELD_RETURN_TYPE = 'ReactFlightComponent';
28
-
29
- type State = {
30
- parentType: TypeID,
31
- types: {
32
- propsType: InputTypeID,
33
- componentType: ScalarFieldTypeID,
34
- },
35
- };
36
-
37
- /**
38
- * Experimental transform for React Flight.
39
- */
40
- function reactFlightComponentTransform(
41
- context: CompilerContext,
42
- ): CompilerContext {
43
- const schema = context.getSchema();
44
- let propsType = schema.getTypeFromString(FLIGHT_FIELD_PROPS_TYPE);
45
- propsType = propsType ? schema.asInputType(propsType) : null;
46
- let componentType = schema.getTypeFromString(FLIGHT_FIELD_RETURN_TYPE);
47
- componentType = componentType
48
- ? schema.asScalarFieldType(componentType)
49
- : null;
50
- if (
51
- !RelayFeatureFlags.ENABLE_REACT_FLIGHT_COMPONENT_FIELD ||
52
- propsType == null ||
53
- componentType == null
54
- ) {
55
- return context;
56
- }
57
- const types = {propsType, componentType};
58
- return IRTransformer.transform(
59
- context,
60
- {
61
- ScalarField: visitScalarField,
62
- LinkedField: visitLinkedField,
63
- InlineFragment: visitInlineFragment,
64
- },
65
- node => ({
66
- parentType: node.type,
67
- types,
68
- }),
69
- );
70
- }
71
-
72
- function visitInlineFragment(fragment, state) {
73
- // $FlowFixMe[incompatible-use]
74
- return this.traverse(fragment, {
75
- parentType: fragment.typeCondition ?? state.parentType,
76
- types: state.types,
77
- });
78
- }
79
-
80
- function visitLinkedField(field, state) {
81
- // $FlowFixMe[incompatible-use]
82
- return this.traverse(field, {parentType: field.type, types: state.types});
83
- }
84
-
85
- function visitScalarField(field: ScalarField, state: State): ScalarField {
86
- // use the return type to quickly determine if this is a flight field
87
- // $FlowFixMe[incompatible-use]
88
- const schema = this.getContext().getSchema();
89
- if (schema.getRawType(field.type) !== state.types.componentType) {
90
- return field;
91
- }
92
-
93
- // get the name of the component that provides this field
94
- const clientField = schema.getFieldByName(state.parentType, field.name);
95
- if (clientField == null) {
96
- throw createCompilerError(
97
- `Definition not found for field '${schema.getTypeString(
98
- state.parentType,
99
- )}.${field.name}'`,
100
- [field.loc],
101
- );
102
- }
103
- const componentDirective = clientField.directives.find(
104
- directive => directive.name === 'react_flight_component',
105
- );
106
- const componentNameArg = componentDirective?.args.find(
107
- arg => arg.name === 'name',
108
- );
109
- if (
110
- componentNameArg == null ||
111
- componentNameArg.value.kind !== 'StringValue' ||
112
- typeof componentNameArg.value.value !== 'string'
113
- ) {
114
- throw createUserError(
115
- 'Invalid Flight field, expected the schema extension to specify ' +
116
- "the component's module name with the '@react_flight_component' directive",
117
- [field.loc],
118
- );
119
- }
120
- const componentName = componentNameArg.value.value;
121
-
122
- // validate that the parent type has a `flight(component, props)` field
123
- const flightField = schema.getFieldByName(state.parentType, 'flight');
124
- if (flightField == null) {
125
- throw createUserError(
126
- `Invalid Flight field, expected the parent type '${schema.getTypeString(
127
- state.parentType,
128
- )}' ` +
129
- "to define a 'flight(component: String, props: ReactFlightProps): ReactFlightComponent' field",
130
- [field.loc],
131
- );
132
- }
133
- const componentArg = flightField.args.get(
134
- FLIGHT_FIELD_COMPONENT_ARGUMENT_NAME,
135
- );
136
- const propsArg = flightField.args.get(FLIGHT_FIELD_PROPS_ARGUMENT_NAME);
137
- if (
138
- componentArg == null ||
139
- propsArg == null ||
140
- schema.getRawType(componentArg.type) !==
141
- schema.getTypeFromString(FLIGHT_FIELD_COMPONENT_ARGUMENT_TYPE) ||
142
- schema.getRawType(propsArg.type) !== state.types.propsType ||
143
- schema.getRawType(flightField.type) !== state.types.componentType
144
- ) {
145
- throw createUserError(
146
- `Invalid Flight field, expected the parent type '${schema.getTypeString(
147
- state.parentType,
148
- )}' ` +
149
- "to define a 'flight(component: String, props: ReactFlightProps): ReactFlightComponent' field",
150
- [field.loc],
151
- );
152
- }
153
-
154
- return {
155
- ...field,
156
- name: 'flight',
157
- args: [
158
- {
159
- kind: 'Argument',
160
- loc: field.loc,
161
- name: FLIGHT_FIELD_COMPONENT_ARGUMENT_NAME,
162
- type: schema.getTypeFromString(FLIGHT_FIELD_COMPONENT_ARGUMENT_TYPE),
163
- value: {
164
- kind: 'Literal',
165
- value: componentName,
166
- loc: field.loc,
167
- },
168
- },
169
- {
170
- kind: 'Argument',
171
- loc: field.loc,
172
- name: FLIGHT_FIELD_PROPS_ARGUMENT_NAME,
173
- type: state.types.propsType,
174
- value: {
175
- kind: 'ObjectValue',
176
- fields: field.args.map(arg => {
177
- return {
178
- kind: 'ObjectFieldValue',
179
- loc: arg.loc,
180
- name: arg.name,
181
- value: arg.value,
182
- };
183
- }),
184
- loc: field.loc,
185
- },
186
- },
187
- ],
188
- metadata: {
189
- ...(field.metadata || {}),
190
- flight: true,
191
- },
192
- type: state.types.componentType,
193
- };
194
- }
195
-
196
- module.exports = {
197
- transform: reactFlightComponentTransform,
198
- };
@@ -1,272 +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 IRVisitor = require('../core/IRVisitor');
16
-
17
- const getLiteralArgumentValues = require('../core/getLiteralArgumentValues');
18
- const inferRootArgumentDefinitions = require('../core/inferRootArgumentDefinitions');
19
-
20
- const {
21
- createUserError,
22
- eachWithCombinedError,
23
- } = require('../core/CompilerError');
24
- const {buildRefetchOperation} = require('./query-generators');
25
-
26
- import type CompilerContext from '../core/CompilerContext';
27
- import type {Argument, Field, Fragment} from '../core/IR';
28
- import type {Schema} from '../core/Schema';
29
- import type {ReaderPaginationMetadata} from 'relay-runtime';
30
-
31
- const SCHEMA_EXTENSION = `
32
- directive @refetchable(
33
- queryName: String!
34
- ) on FRAGMENT_DEFINITION
35
- `;
36
-
37
- /**
38
- * This transform synthesizes "refetch" queries for fragments that
39
- * are trivially refetchable. This is comprised of three main stages:
40
- *
41
- * 1. Validating that fragments marked with @refetchable qualify for
42
- * refetch query generation; mainly this means that the fragment
43
- * type is able to be refetched in some canonical way.
44
- * 2. Determining the variable definitions to use for each generated
45
- * query. GraphQL does not have a notion of fragment-local variables
46
- * at all, and although Relay adds this concept developers are still
47
- * allowed to reference global variables. This necessitates a
48
- * visiting all reachable fragments for each @refetchable fragment,
49
- * and finding the union of all global variables expceted to be defined.
50
- * 3. Building the refetch queries, a straightforward copying transform from
51
- * Fragment to Root IR nodes.
52
- */
53
- function refetchableFragmentTransform(
54
- context: CompilerContext,
55
- ): CompilerContext {
56
- const schema = context.getSchema();
57
-
58
- const refetchOperations = buildRefetchMap(context);
59
- let nextContext = context;
60
- eachWithCombinedError(refetchOperations, ([refetchName, fragment]) => {
61
- const {
62
- identifierField,
63
- path,
64
- node,
65
- transformedFragment,
66
- } = buildRefetchOperation(schema, fragment, refetchName);
67
- const connectionMetadata = extractConnectionMetadata(
68
- context.getSchema(),
69
- transformedFragment,
70
- );
71
- nextContext = nextContext.replace({
72
- ...transformedFragment,
73
- metadata: {
74
- ...(transformedFragment.metadata || {}),
75
- refetch: {
76
- connection: connectionMetadata ?? null,
77
- operation: refetchName,
78
- fragmentPathInResult: path,
79
- identifierField,
80
- },
81
- },
82
- });
83
- nextContext = nextContext.add({
84
- ...node,
85
- metadata: {
86
- ...(node.metadata || {}),
87
- derivedFrom: transformedFragment.name,
88
- isRefetchableQuery: true,
89
- },
90
- });
91
- });
92
- return nextContext;
93
- }
94
-
95
- /**
96
- * Walk the documents of a compiler context and create a mapping of
97
- * refetch operation names to the source fragment from which the refetch
98
- * operation should be derived.
99
- */
100
- function buildRefetchMap(context: CompilerContext): Map<string, Fragment> {
101
- const refetchOperations = new Map();
102
- eachWithCombinedError(context.documents(), node => {
103
- if (node.kind !== 'Fragment') {
104
- return;
105
- }
106
- const refetchName = getRefetchQueryName(node);
107
- if (refetchName === null) {
108
- return;
109
- }
110
- const previousOperation = refetchOperations.get(refetchName);
111
- if (previousOperation != null) {
112
- throw createUserError(
113
- `Duplicate definition for @refetchable operation '${refetchName}' from fragments '${node.name}' and '${previousOperation.name}'`,
114
- [node.loc, previousOperation.loc],
115
- );
116
- }
117
- refetchOperations.set(refetchName, node);
118
- });
119
- const transformed = inferRootArgumentDefinitions(context);
120
- return new Map(
121
- Array.from(refetchOperations.entries(), ([name, fragment]) => {
122
- return [name, transformed.getFragment(fragment.name)];
123
- }),
124
- );
125
- }
126
-
127
- /**
128
- * Validate that any @connection usage is valid for refetching:
129
- * - Variables are used for both the "count" and "cursor" arguments
130
- * (after/first or before/last)
131
- * - Exactly one connection
132
- * - Has a stable path to the connection data
133
- *
134
- * Returns connection metadata to add to the transformed fragment or undefined
135
- * if there is no connection.
136
- */
137
- function extractConnectionMetadata(
138
- schema: Schema,
139
- fragment: Fragment,
140
- ): ReaderPaginationMetadata | void {
141
- const fields = [];
142
- let connectionField = null;
143
- let path = null;
144
- IRVisitor.visit(fragment, {
145
- LinkedField: {
146
- enter(field) {
147
- fields.push(field);
148
- if (
149
- field.connection === true ||
150
- (field.handles &&
151
- field.handles.some(handle => handle.name === 'connection'))
152
- ) {
153
- // Disallow multiple @connections
154
- if (connectionField != null) {
155
- throw createUserError(
156
- `Invalid use of @refetchable with @connection in fragment '${fragment.name}', at most once @connection can appear in a refetchable fragment.`,
157
- [field.loc],
158
- );
159
- }
160
- // Disallow connections within plurals
161
- const pluralOnPath = fields.find(pathField =>
162
- schema.isList(schema.getNullableType(pathField.type)),
163
- );
164
- if (pluralOnPath) {
165
- throw createUserError(
166
- `Invalid use of @refetchable with @connection in fragment '${fragment.name}', refetchable connections cannot appear inside plural fields.`,
167
- [field.loc, pluralOnPath.loc],
168
- );
169
- }
170
- connectionField = field;
171
- path = fields.map(pathField => pathField.alias);
172
- }
173
- },
174
- leave() {
175
- fields.pop();
176
- },
177
- },
178
- });
179
- if (connectionField == null || path == null) {
180
- return;
181
- }
182
- // Validate arguments: if either of before/last appear they must both appear
183
- // and use variables (not scalar values)
184
- let backward = null;
185
- const before = findArgument(connectionField, 'before');
186
- const last = findArgument(connectionField, 'last');
187
- if (before || last) {
188
- if (
189
- !before ||
190
- !last ||
191
- before.value.kind !== 'Variable' ||
192
- last.value.kind !== 'Variable'
193
- ) {
194
- throw createUserError(
195
- `Invalid use of @refetchable with @connection in fragment '${fragment.name}', refetchable connections must use variables for the before and last arguments.`,
196
- [
197
- connectionField.loc,
198
- before && before.value.kind !== 'Variable' ? before.value.loc : null,
199
- last && last.value.kind !== 'Variable' ? last.value.loc : null,
200
- ].filter(Boolean),
201
- );
202
- }
203
- backward = {
204
- count: last.value.variableName,
205
- cursor: before.value.variableName,
206
- };
207
- }
208
- // Validate arguments: if either of after/first appear they must both appear
209
- // and use variables (not scalar values)
210
- let forward = null;
211
- const after = findArgument(connectionField, 'after');
212
- const first = findArgument(connectionField, 'first');
213
- if (after || first) {
214
- if (
215
- !after ||
216
- !first ||
217
- after.value.kind !== 'Variable' ||
218
- first.value.kind !== 'Variable'
219
- ) {
220
- throw createUserError(
221
- `Invalid use of @refetchable with @connection in fragment '${fragment.name}', refetchable connections must use variables for the after and first arguments.`,
222
- [
223
- connectionField.loc,
224
- after && after.value.kind !== 'Variable' ? after.value.loc : null,
225
- first && first.value.kind !== 'Variable' ? first.value.loc : null,
226
- ].filter(Boolean),
227
- );
228
- }
229
- forward = {
230
- count: first.value.variableName,
231
- cursor: after.value.variableName,
232
- };
233
- }
234
- return {forward, backward, path};
235
- }
236
-
237
- function getRefetchQueryName(fragment: Fragment): string | null {
238
- const refetchableDirective = fragment.directives.find(
239
- directive => directive.name === 'refetchable',
240
- );
241
- if (refetchableDirective == null) {
242
- return null;
243
- }
244
- const refetchArguments = getLiteralArgumentValues(refetchableDirective.args);
245
- const queryName = refetchArguments.queryName;
246
- if (queryName == null) {
247
- throw createUserError(
248
- "Expected the 'queryName' argument of @refetchable to be provided",
249
- [refetchableDirective.loc],
250
- );
251
- } else if (typeof queryName !== 'string') {
252
- const queryNameArg = refetchableDirective.args.find(
253
- arg => arg.name === 'queryName',
254
- );
255
- throw createUserError(
256
- `Expected the 'queryName' argument of @refetchable to be a string, got '${String(
257
- queryName,
258
- )}'.`,
259
- [queryNameArg?.loc ?? refetchableDirective.loc],
260
- );
261
- }
262
- return queryName;
263
- }
264
-
265
- function findArgument(field: Field, argumentName: string): Argument | null {
266
- return field.args.find(arg => arg.name === argumentName) ?? null;
267
- }
268
-
269
- module.exports = {
270
- SCHEMA_EXTENSION,
271
- transform: refetchableFragmentTransform,
272
- };
@@ -1,99 +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 getLiteralArgumentValues = require('../core/getLiteralArgumentValues');
18
- const invariant = require('invariant');
19
-
20
- import type CompilerContext from '../core/CompilerContext';
21
- import type {Fragment, FragmentSpread} from '../core/IR';
22
-
23
- const RELAY = 'relay';
24
- const SCHEMA_EXTENSION = `
25
- directive @relay(
26
- # Marks a fragment as being backed by a GraphQLList.
27
- plural: Boolean,
28
-
29
- # Marks a fragment spread which should be unmasked if provided false
30
- mask: Boolean = true,
31
- ) on FRAGMENT_DEFINITION | FRAGMENT_SPREAD
32
- `;
33
-
34
- /**
35
- * A transform that extracts `@relay(plural: Boolean)` directives and converts
36
- * them to metadata that can be accessed at runtime.
37
- */
38
- function relayDirectiveTransform(context: CompilerContext): CompilerContext {
39
- return IRTransformer.transform(context, {
40
- Fragment: visitRelayMetadata(fragmentMetadata),
41
- FragmentSpread: visitRelayMetadata(fragmentSpreadMetadata),
42
- });
43
- }
44
-
45
- type MixedObj = {[key: string]: mixed, ...};
46
- function visitRelayMetadata<T: Fragment | FragmentSpread>(
47
- metadataFn: MixedObj => MixedObj,
48
- ): T => T {
49
- return function(node) {
50
- const relayDirective = node.directives.find(({name}) => name === RELAY);
51
- if (!relayDirective) {
52
- // $FlowFixMe[incompatible-use]
53
- return this.traverse(node);
54
- }
55
- const argValues = getLiteralArgumentValues(relayDirective.args);
56
- const metadata = metadataFn(argValues);
57
- // $FlowFixMe[incompatible-use]
58
- return this.traverse({
59
- ...node,
60
- directives: node.directives.filter(
61
- directive => directive !== relayDirective,
62
- ),
63
- // $FlowFixMe[cannot-spread-indexer]
64
- metadata: {
65
- ...(node.metadata || {}),
66
- ...metadata,
67
- },
68
- });
69
- };
70
- }
71
-
72
- function fragmentMetadata({mask, plural}): MixedObj {
73
- invariant(
74
- plural === undefined || typeof plural === 'boolean',
75
- 'RelayDirectiveTransform: Expected the "plural" argument to @relay ' +
76
- 'to be a boolean literal if specified.',
77
- );
78
- invariant(
79
- mask === undefined || typeof mask === 'boolean',
80
- 'RelayDirectiveTransform: Expected the "mask" argument to @relay ' +
81
- 'to be a boolean literal if specified.',
82
- );
83
- return {mask, plural};
84
- }
85
-
86
- function fragmentSpreadMetadata({mask}): MixedObj {
87
- invariant(
88
- mask === undefined || typeof mask === 'boolean',
89
- 'RelayDirectiveTransform: Expected the "mask" argument to @relay ' +
90
- 'to be a boolean literal if specified.',
91
- );
92
- return {mask};
93
- }
94
-
95
- module.exports = {
96
- RELAY,
97
- SCHEMA_EXTENSION,
98
- transform: relayDirectiveTransform,
99
- };