@ardatan/relay-compiler 12.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/LICENSE +21 -0
  2. package/bin/RelayCompilerBin.js.flow +169 -0
  3. package/bin/RelayCompilerMain.js.flow +515 -0
  4. package/bin/__fixtures__/plugin-module.js.flow +17 -0
  5. package/bin/relay-compiler +19066 -0
  6. package/codegen/CodegenDirectory.js.flow +375 -0
  7. package/codegen/CodegenRunner.js.flow +432 -0
  8. package/codegen/CodegenTypes.js.flow +28 -0
  9. package/codegen/CodegenWatcher.js.flow +254 -0
  10. package/codegen/NormalizationCodeGenerator.js.flow +566 -0
  11. package/codegen/ReaderCodeGenerator.js.flow +512 -0
  12. package/codegen/RelayCodeGenerator.js.flow +85 -0
  13. package/codegen/RelayFileWriter.js.flow +367 -0
  14. package/codegen/SourceControl.js.flow +58 -0
  15. package/codegen/compileRelayArtifacts.js.flow +182 -0
  16. package/codegen/createPrintRequireModuleDependency.js.flow +19 -0
  17. package/codegen/sortObjectByKey.js.flow +25 -0
  18. package/codegen/writeRelayGeneratedFile.js.flow +239 -0
  19. package/core/ASTCache.js.flow +74 -0
  20. package/core/ASTConvert.js.flow +233 -0
  21. package/core/CompilerContext.js.flow +191 -0
  22. package/core/CompilerError.js.flow +255 -0
  23. package/core/DotGraphQLParser.js.flow +39 -0
  24. package/core/GraphQLCompilerProfiler.js.flow +341 -0
  25. package/core/GraphQLDerivedFromMetadata.js.flow +36 -0
  26. package/core/GraphQLWatchmanClient.js.flow +111 -0
  27. package/core/IR.js.flow +326 -0
  28. package/core/IRPrinter.js.flow +478 -0
  29. package/core/IRTransformer.js.flow +377 -0
  30. package/core/IRValidator.js.flow +260 -0
  31. package/core/IRVisitor.js.flow +150 -0
  32. package/core/JSModuleParser.js.flow +24 -0
  33. package/core/RelayCompilerScope.js.flow +199 -0
  34. package/core/RelayFindGraphQLTags.js.flow +119 -0
  35. package/core/RelayGraphQLEnumsGenerator.js.flow +55 -0
  36. package/core/RelayIRTransforms.js.flow +138 -0
  37. package/core/RelayParser.js.flow +1734 -0
  38. package/core/RelaySourceModuleParser.js.flow +135 -0
  39. package/core/Schema.js.flow +2037 -0
  40. package/core/SchemaUtils.js.flow +120 -0
  41. package/core/filterContextForNode.js.flow +50 -0
  42. package/core/getFieldDefinition.js.flow +156 -0
  43. package/core/getIdentifierForArgumentValue.js.flow +49 -0
  44. package/core/getIdentifierForSelection.js.flow +69 -0
  45. package/core/getLiteralArgumentValues.js.flow +32 -0
  46. package/core/getNormalizationOperationName.js.flow +19 -0
  47. package/core/inferRootArgumentDefinitions.js.flow +323 -0
  48. package/index.js +10 -0
  49. package/index.js.flow +200 -0
  50. package/language/RelayLanguagePluginInterface.js.flow +283 -0
  51. package/language/javascript/FindGraphQLTags.js.flow +137 -0
  52. package/language/javascript/RelayFlowBabelFactories.js.flow +176 -0
  53. package/language/javascript/RelayFlowGenerator.js.flow +1100 -0
  54. package/language/javascript/RelayFlowTypeTransformers.js.flow +184 -0
  55. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +34 -0
  56. package/language/javascript/formatGeneratedModule.js.flow +65 -0
  57. package/lib/bin/RelayCompilerBin.js +143 -0
  58. package/lib/bin/RelayCompilerMain.js +486 -0
  59. package/lib/bin/__fixtures__/plugin-module.js +16 -0
  60. package/lib/codegen/CodegenDirectory.js +336 -0
  61. package/lib/codegen/CodegenRunner.js +433 -0
  62. package/lib/codegen/CodegenTypes.js +11 -0
  63. package/lib/codegen/CodegenWatcher.js +271 -0
  64. package/lib/codegen/NormalizationCodeGenerator.js +480 -0
  65. package/lib/codegen/ReaderCodeGenerator.js +472 -0
  66. package/lib/codegen/RelayCodeGenerator.js +68 -0
  67. package/lib/codegen/RelayFileWriter.js +270 -0
  68. package/lib/codegen/SourceControl.js +60 -0
  69. package/lib/codegen/compileRelayArtifacts.js +157 -0
  70. package/lib/codegen/createPrintRequireModuleDependency.js +19 -0
  71. package/lib/codegen/sortObjectByKey.js +41 -0
  72. package/lib/codegen/writeRelayGeneratedFile.js +208 -0
  73. package/lib/core/ASTCache.js +70 -0
  74. package/lib/core/ASTConvert.js +198 -0
  75. package/lib/core/CompilerContext.js +165 -0
  76. package/lib/core/CompilerError.js +251 -0
  77. package/lib/core/DotGraphQLParser.js +40 -0
  78. package/lib/core/GraphQLCompilerProfiler.js +299 -0
  79. package/lib/core/GraphQLDerivedFromMetadata.js +31 -0
  80. package/lib/core/GraphQLWatchmanClient.js +150 -0
  81. package/lib/core/IR.js +11 -0
  82. package/lib/core/IRPrinter.js +389 -0
  83. package/lib/core/IRTransformer.js +345 -0
  84. package/lib/core/IRValidator.js +226 -0
  85. package/lib/core/IRVisitor.js +45 -0
  86. package/lib/core/JSModuleParser.js +18 -0
  87. package/lib/core/RelayCompilerScope.js +149 -0
  88. package/lib/core/RelayFindGraphQLTags.js +79 -0
  89. package/lib/core/RelayGraphQLEnumsGenerator.js +50 -0
  90. package/lib/core/RelayIRTransforms.js +109 -0
  91. package/lib/core/RelayParser.js +1382 -0
  92. package/lib/core/RelaySourceModuleParser.js +104 -0
  93. package/lib/core/Schema.js +1877 -0
  94. package/lib/core/SchemaUtils.js +98 -0
  95. package/lib/core/filterContextForNode.js +49 -0
  96. package/lib/core/getFieldDefinition.js +145 -0
  97. package/lib/core/getIdentifierForArgumentValue.js +53 -0
  98. package/lib/core/getIdentifierForSelection.js +48 -0
  99. package/lib/core/getLiteralArgumentValues.js +26 -0
  100. package/lib/core/getNormalizationOperationName.js +17 -0
  101. package/lib/core/inferRootArgumentDefinitions.js +351 -0
  102. package/lib/index.js +178 -0
  103. package/lib/language/RelayLanguagePluginInterface.js +14 -0
  104. package/lib/language/javascript/FindGraphQLTags.js +126 -0
  105. package/lib/language/javascript/RelayFlowBabelFactories.js +160 -0
  106. package/lib/language/javascript/RelayFlowGenerator.js +857 -0
  107. package/lib/language/javascript/RelayFlowTypeTransformers.js +119 -0
  108. package/lib/language/javascript/RelayLanguagePluginJavaScript.js +30 -0
  109. package/lib/language/javascript/formatGeneratedModule.js +36 -0
  110. package/lib/reporters/ConsoleReporter.js +61 -0
  111. package/lib/reporters/MultiReporter.js +45 -0
  112. package/lib/reporters/Reporter.js +11 -0
  113. package/lib/runner/Artifacts.js +323 -0
  114. package/lib/runner/BufferedFilesystem.js +261 -0
  115. package/lib/runner/GraphQLASTNodeGroup.js +256 -0
  116. package/lib/runner/GraphQLASTUtils.js +23 -0
  117. package/lib/runner/GraphQLNodeMap.js +81 -0
  118. package/lib/runner/Sources.js +271 -0
  119. package/lib/runner/StrictMap.js +134 -0
  120. package/lib/runner/compileArtifacts.js +39 -0
  121. package/lib/runner/extractAST.js +77 -0
  122. package/lib/runner/getChangedNodeNames.js +82 -0
  123. package/lib/runner/getSchemaInstance.js +30 -0
  124. package/lib/runner/types.js +12 -0
  125. package/lib/transforms/ApplyFragmentArgumentTransform.js +393 -0
  126. package/lib/transforms/ClientExtensionsTransform.js +222 -0
  127. package/lib/transforms/ConnectionTransform.js +643 -0
  128. package/lib/transforms/DeclarativeConnectionMutationTransform.js +221 -0
  129. package/lib/transforms/DeferStreamTransform.js +247 -0
  130. package/lib/transforms/DisallowIdAsAlias.js +41 -0
  131. package/lib/transforms/DisallowTypenameOnRoot.js +53 -0
  132. package/lib/transforms/FieldHandleTransform.js +81 -0
  133. package/lib/transforms/FilterCompilerDirectivesTransform.js +29 -0
  134. package/lib/transforms/FilterDirectivesTransform.js +41 -0
  135. package/lib/transforms/FlattenTransform.js +308 -0
  136. package/lib/transforms/GenerateIDFieldTransform.js +137 -0
  137. package/lib/transforms/GenerateTypeNameTransform.js +155 -0
  138. package/lib/transforms/InlineDataFragmentTransform.js +104 -0
  139. package/lib/transforms/InlineFragmentsTransform.js +63 -0
  140. package/lib/transforms/MaskTransform.js +121 -0
  141. package/lib/transforms/MatchTransform.js +438 -0
  142. package/lib/transforms/ReactFlightComponentTransform.js +161 -0
  143. package/lib/transforms/RefetchableFragmentTransform.js +249 -0
  144. package/lib/transforms/RelayDirectiveTransform.js +85 -0
  145. package/lib/transforms/RequiredFieldTransform.js +373 -0
  146. package/lib/transforms/SkipClientExtensionsTransform.js +49 -0
  147. package/lib/transforms/SkipHandleFieldTransform.js +45 -0
  148. package/lib/transforms/SkipRedundantNodesTransform.js +255 -0
  149. package/lib/transforms/SkipSplitOperationTransform.js +32 -0
  150. package/lib/transforms/SkipUnreachableNodeTransform.js +158 -0
  151. package/lib/transforms/SkipUnusedVariablesTransform.js +74 -0
  152. package/lib/transforms/SplitModuleImportTransform.js +85 -0
  153. package/lib/transforms/TestOperationTransform.js +145 -0
  154. package/lib/transforms/TransformUtils.js +21 -0
  155. package/lib/transforms/ValidateGlobalVariablesTransform.js +91 -0
  156. package/lib/transforms/ValidateRequiredArgumentsTransform.js +118 -0
  157. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +111 -0
  158. package/lib/transforms/ValidateUnusedVariablesTransform.js +96 -0
  159. package/lib/transforms/query-generators/FetchableQueryGenerator.js +157 -0
  160. package/lib/transforms/query-generators/NodeQueryGenerator.js +166 -0
  161. package/lib/transforms/query-generators/QueryQueryGenerator.js +48 -0
  162. package/lib/transforms/query-generators/ViewerQueryGenerator.js +77 -0
  163. package/lib/transforms/query-generators/index.js +60 -0
  164. package/lib/transforms/query-generators/utils.js +92 -0
  165. package/lib/util/CodeMarker.js +80 -0
  166. package/lib/util/DefaultHandleKey.js +15 -0
  167. package/lib/util/RelayCompilerCache.js +98 -0
  168. package/lib/util/Rollout.js +40 -0
  169. package/lib/util/TimeReporter.js +83 -0
  170. package/lib/util/areEqualArgValues.js +135 -0
  171. package/lib/util/argumentContainsVariables.js +37 -0
  172. package/lib/util/dedupeJSONStringify.js +160 -0
  173. package/lib/util/generateAbstractTypeRefinementKey.js +24 -0
  174. package/lib/util/getDefinitionNodeHash.js +22 -0
  175. package/lib/util/getModuleName.js +32 -0
  176. package/lib/util/joinArgumentDefinitions.js +66 -0
  177. package/lib/util/md5.js +17 -0
  178. package/lib/util/murmurHash.js +86 -0
  179. package/lib/util/nullthrowsOSS.js +23 -0
  180. package/lib/util/orList.js +36 -0
  181. package/lib/util/partitionArray.js +35 -0
  182. package/package.json +42 -0
  183. package/relay-compiler.js +17 -0
  184. package/relay-compiler.min.js +22 -0
  185. package/reporters/ConsoleReporter.js.flow +81 -0
  186. package/reporters/MultiReporter.js.flow +43 -0
  187. package/reporters/Reporter.js.flow +19 -0
  188. package/runner/Artifacts.js.flow +219 -0
  189. package/runner/BufferedFilesystem.js.flow +194 -0
  190. package/runner/GraphQLASTNodeGroup.js.flow +176 -0
  191. package/runner/GraphQLASTUtils.js.flow +26 -0
  192. package/runner/GraphQLNodeMap.js.flow +55 -0
  193. package/runner/Sources.js.flow +228 -0
  194. package/runner/StrictMap.js.flow +96 -0
  195. package/runner/compileArtifacts.js.flow +76 -0
  196. package/runner/extractAST.js.flow +100 -0
  197. package/runner/getChangedNodeNames.js.flow +48 -0
  198. package/runner/getSchemaInstance.js.flow +36 -0
  199. package/runner/types.js.flow +37 -0
  200. package/transforms/ApplyFragmentArgumentTransform.js.flow +526 -0
  201. package/transforms/ClientExtensionsTransform.js.flow +226 -0
  202. package/transforms/ConnectionTransform.js.flow +859 -0
  203. package/transforms/DeclarativeConnectionMutationTransform.js.flow +250 -0
  204. package/transforms/DeferStreamTransform.js.flow +266 -0
  205. package/transforms/DisallowIdAsAlias.js.flow +48 -0
  206. package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
  207. package/transforms/FieldHandleTransform.js.flow +81 -0
  208. package/transforms/FilterCompilerDirectivesTransform.js.flow +33 -0
  209. package/transforms/FilterDirectivesTransform.js.flow +45 -0
  210. package/transforms/FlattenTransform.js.flow +462 -0
  211. package/transforms/GenerateIDFieldTransform.js.flow +154 -0
  212. package/transforms/GenerateTypeNameTransform.js.flow +167 -0
  213. package/transforms/InlineDataFragmentTransform.js.flow +129 -0
  214. package/transforms/InlineFragmentsTransform.js.flow +73 -0
  215. package/transforms/MaskTransform.js.flow +130 -0
  216. package/transforms/MatchTransform.js.flow +593 -0
  217. package/transforms/ReactFlightComponentTransform.js.flow +198 -0
  218. package/transforms/RefetchableFragmentTransform.js.flow +272 -0
  219. package/transforms/RelayDirectiveTransform.js.flow +99 -0
  220. package/transforms/RequiredFieldTransform.js.flow +419 -0
  221. package/transforms/SkipClientExtensionsTransform.js.flow +57 -0
  222. package/transforms/SkipHandleFieldTransform.js.flow +45 -0
  223. package/transforms/SkipRedundantNodesTransform.js.flow +259 -0
  224. package/transforms/SkipSplitOperationTransform.js.flow +37 -0
  225. package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
  226. package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
  227. package/transforms/SplitModuleImportTransform.js.flow +101 -0
  228. package/transforms/TestOperationTransform.js.flow +143 -0
  229. package/transforms/TransformUtils.js.flow +26 -0
  230. package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
  231. package/transforms/ValidateRequiredArgumentsTransform.js.flow +131 -0
  232. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +115 -0
  233. package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
  234. package/transforms/query-generators/FetchableQueryGenerator.js.flow +189 -0
  235. package/transforms/query-generators/NodeQueryGenerator.js.flow +219 -0
  236. package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
  237. package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
  238. package/transforms/query-generators/index.js.flow +90 -0
  239. package/transforms/query-generators/utils.js.flow +76 -0
  240. package/util/CodeMarker.js.flow +79 -0
  241. package/util/DefaultHandleKey.js.flow +17 -0
  242. package/util/RelayCompilerCache.js.flow +88 -0
  243. package/util/Rollout.js.flow +39 -0
  244. package/util/TimeReporter.js.flow +79 -0
  245. package/util/areEqualArgValues.js.flow +126 -0
  246. package/util/argumentContainsVariables.js.flow +38 -0
  247. package/util/dedupeJSONStringify.js.flow +152 -0
  248. package/util/generateAbstractTypeRefinementKey.js.flow +29 -0
  249. package/util/getDefinitionNodeHash.js.flow +25 -0
  250. package/util/getModuleName.js.flow +39 -0
  251. package/util/joinArgumentDefinitions.js.flow +105 -0
  252. package/util/md5.js.flow +22 -0
  253. package/util/murmurHash.js.flow +94 -0
  254. package/util/nullthrowsOSS.js.flow +25 -0
  255. package/util/orList.js.flow +37 -0
  256. package/util/partitionArray.js.flow +37 -0
@@ -0,0 +1,438 @@
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
+ *
8
+ * @format
9
+ */
10
+ // flowlint ambiguous-object-type:error
11
+ 'use strict';
12
+
13
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
14
+
15
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
16
+
17
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
18
+
19
+ var IRTransformer = require('../core/IRTransformer');
20
+
21
+ var getLiteralArgumentValues = require('../core/getLiteralArgumentValues');
22
+
23
+ var getNormalizationOperationName = require('../core/getNormalizationOperationName');
24
+
25
+ var _require = require('../core/CompilerError'),
26
+ createCompilerError = _require.createCompilerError,
27
+ createUserError = _require.createUserError;
28
+
29
+ var _require2 = require('relay-runtime'),
30
+ getModuleComponentKey = _require2.getModuleComponentKey,
31
+ getModuleOperationKey = _require2.getModuleOperationKey;
32
+
33
+ var SUPPORTED_ARGUMENT_NAME = 'supported';
34
+ var JS_FIELD_TYPE = 'JSDependency';
35
+ var JS_FIELD_MODULE_ARG = 'module';
36
+ var JS_FIELD_ID_ARG = 'id';
37
+ var JS_FIELD_NAME = 'js';
38
+ var SCHEMA_EXTENSION = "\n directive @match(key: String) on FIELD\n\n directive @module(\n name: String!\n ) on FRAGMENT_SPREAD\n";
39
+
40
+ /**
41
+ * This transform rewrites LinkedField nodes with @match and rewrites them
42
+ * into `LinkedField` nodes with a `supported` argument.
43
+ */
44
+ function matchTransform(context) {
45
+ return IRTransformer.transform(context, {
46
+ // TODO: type IRTransformer to allow changing result type
47
+ FragmentSpread: visitFragmentSpread,
48
+ LinkedField: visitLinkedField,
49
+ InlineFragment: visitInlineFragment,
50
+ ScalarField: visitScalarField
51
+ }, function (node) {
52
+ return {
53
+ documentName: node.name,
54
+ matchesForPath: new Map(),
55
+ moduleKey: null,
56
+ parentType: node.type,
57
+ path: []
58
+ };
59
+ });
60
+ }
61
+
62
+ function visitInlineFragment(node, state) {
63
+ // $FlowFixMe[incompatible-use]
64
+ return this.traverse(node, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, state), {}, {
65
+ parentType: node.typeCondition
66
+ }));
67
+ }
68
+
69
+ function visitScalarField(field) {
70
+ // $FlowFixMe[incompatible-use]
71
+ var context = this.getContext();
72
+ var schema = context.getSchema();
73
+
74
+ if (field.name === JS_FIELD_NAME) {
75
+ var jsModuleType = schema.getTypeFromString(JS_FIELD_TYPE);
76
+
77
+ if (jsModuleType == null || !schema.isServerType(jsModuleType)) {
78
+ throw new createUserError("'".concat(JS_FIELD_NAME, "' should be defined on the server schema."), [field.loc]);
79
+ }
80
+
81
+ if (schema.isScalar(jsModuleType) && schema.areEqualTypes(schema.getRawType(field.type), jsModuleType)) {
82
+ throw new createUserError("Direct use of the '".concat(JS_FIELD_NAME, "' field is not allowed, use ") + '@match/@module instead.', [field.loc]);
83
+ }
84
+ }
85
+
86
+ return field;
87
+ }
88
+
89
+ function visitLinkedField(node, state) {
90
+ // $FlowFixMe[incompatible-use]
91
+ var context = this.getContext();
92
+ var schema = context.getSchema();
93
+ var matchDirective = node.directives.find(function (directive) {
94
+ return directive.name === 'match';
95
+ });
96
+ var moduleKey = null;
97
+
98
+ if (matchDirective != null) {
99
+ var _getLiteralArgumentVa = getLiteralArgumentValues(matchDirective.args);
100
+
101
+ moduleKey = _getLiteralArgumentVa.key;
102
+
103
+ if (moduleKey != null && (typeof moduleKey !== 'string' || !moduleKey.startsWith(state.documentName))) {
104
+ var _matchDirective$args$;
105
+
106
+ throw createUserError("Expected the 'key' argument of @match to be a literal string starting " + "with the document name, e.g. '".concat(state.documentName, "_<localName>'."), [((_matchDirective$args$ = matchDirective.args.find(function (arg) {
107
+ return arg.name === 'key';
108
+ })) !== null && _matchDirective$args$ !== void 0 ? _matchDirective$args$ : matchDirective).loc]);
109
+ }
110
+ }
111
+
112
+ state.path.push(node); // $FlowFixMe[incompatible-use]
113
+
114
+ var transformedNode = this.traverse(node, (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, state), {}, {
115
+ moduleKey: moduleKey,
116
+ parentType: node.type
117
+ }));
118
+ state.path.pop();
119
+
120
+ if (matchDirective == null) {
121
+ return transformedNode;
122
+ }
123
+
124
+ var parentType = state.parentType;
125
+ var rawType = schema.getRawType(parentType);
126
+
127
+ if (!(schema.isInterface(rawType) || schema.isObject(rawType))) {
128
+ throw createUserError("@match used on incompatible field '".concat(transformedNode.name, "'.") + '@match may only be used with fields whose parent type is an ' + "interface or object, got invalid type '".concat(schema.getTypeString(parentType), "'."), [node.loc]);
129
+ }
130
+
131
+ var currentField = schema.getFieldConfig(schema.expectField(schema.assertCompositeType(rawType), transformedNode.name));
132
+ var supportedArgumentDefinition = currentField.args.find(function (_ref) {
133
+ var name = _ref.name;
134
+ return name === SUPPORTED_ARGUMENT_NAME;
135
+ });
136
+
137
+ if (supportedArgumentDefinition == null) {
138
+ if (moduleKey == null) {
139
+ throw createUserError('@match on a field without the `supported` argument is a no-op, please remove the `@match`.', [node.loc]);
140
+ }
141
+
142
+ return transformedNode;
143
+ }
144
+
145
+ var supportedArgType = schema.getNullableType(supportedArgumentDefinition.type);
146
+ var supportedArgOfType = supportedArgType != null && schema.isList(supportedArgType) ? schema.getListItemType(supportedArgType) : null;
147
+
148
+ if (supportedArgType == null || supportedArgOfType == null || !schema.isString(schema.getNullableType(supportedArgOfType))) {
149
+ throw createUserError("@match used on incompatible field '".concat(transformedNode.name, "'. ") + '@match may only be used with fields that accept a ' + "'supported: [String!]!' argument.", [node.loc]);
150
+ }
151
+
152
+ var rawFieldType = schema.getRawType(transformedNode.type);
153
+
154
+ if (!schema.isAbstractType(rawFieldType)) {
155
+ throw createUserError("@match used on incompatible field '".concat(transformedNode.name, "'.") + '@match may only be used with fields that return a union or interface.', [node.loc]);
156
+ }
157
+
158
+ var seenTypes = new Map();
159
+ var selections = [];
160
+ transformedNode.selections.forEach(function (matchSelection) {
161
+ if (matchSelection.kind === 'ScalarField' && matchSelection.name === '__typename') {
162
+ selections.push(matchSelection);
163
+ return;
164
+ }
165
+
166
+ var moduleImport = matchSelection.kind === 'InlineFragment' ? matchSelection.selections[0] : null;
167
+
168
+ if (matchSelection.kind !== 'InlineFragment' || moduleImport == null || moduleImport.kind !== 'ModuleImport') {
169
+ throw createUserError('Invalid @match selection: all selections should be ' + 'fragment spreads with @module.', [matchSelection.loc]);
170
+ }
171
+
172
+ var matchedType = matchSelection.typeCondition;
173
+ seenTypes.set(matchedType, matchSelection);
174
+ selections.push(matchSelection);
175
+ });
176
+
177
+ if (seenTypes.size === 0) {
178
+ throw createUserError('Invalid @match selection: expected at least one @module selection. ' + "Remove @match or add a '...Fragment @module()' selection.", [matchDirective.loc]);
179
+ }
180
+
181
+ var supportedArg = transformedNode.args.find(function (arg) {
182
+ return arg.name === SUPPORTED_ARGUMENT_NAME;
183
+ });
184
+
185
+ if (supportedArg != null) {
186
+ throw createUserError("Invalid @match selection: the '".concat(SUPPORTED_ARGUMENT_NAME, "' argument ") + 'is automatically added and cannot be supplied explicitly.', [supportedArg.loc]);
187
+ }
188
+
189
+ return {
190
+ kind: 'LinkedField',
191
+ alias: transformedNode.alias,
192
+ args: [].concat((0, _toConsumableArray2["default"])(transformedNode.args), [{
193
+ kind: 'Argument',
194
+ name: SUPPORTED_ARGUMENT_NAME,
195
+ type: supportedArgumentDefinition.type,
196
+ value: {
197
+ kind: 'Literal',
198
+ loc: node.loc,
199
+ value: Array.from(seenTypes.keys()).map(function (type) {
200
+ return schema.getTypeString(type);
201
+ })
202
+ },
203
+ loc: node.loc
204
+ }]),
205
+ connection: false,
206
+ directives: [],
207
+ handles: null,
208
+ loc: node.loc,
209
+ metadata: null,
210
+ name: transformedNode.name,
211
+ type: transformedNode.type,
212
+ selections: selections
213
+ };
214
+ } // Transform @module
215
+
216
+
217
+ function visitFragmentSpread(spread, _ref2) {
218
+ var _moduleDirective$args2, _moduleDirective$args3, _moduleDirective$args4, _moduleDirective$args5, _moduleDirective$args6, _moduleDirective$args7;
219
+
220
+ var documentName = _ref2.documentName,
221
+ path = _ref2.path,
222
+ matchesForPath = _ref2.matchesForPath,
223
+ moduleKeyFromParent = _ref2.moduleKey;
224
+ var transformedNode = this.traverse(spread);
225
+ var moduleDirective = transformedNode.directives.find(function (directive) {
226
+ return directive.name === 'module';
227
+ });
228
+
229
+ if (moduleDirective == null) {
230
+ return transformedNode;
231
+ }
232
+
233
+ if (spread.args.length !== 0) {
234
+ var _spread$args$;
235
+
236
+ throw createUserError('@module does not support @arguments.', [(_spread$args$ = spread.args[0]) === null || _spread$args$ === void 0 ? void 0 : _spread$args$.loc].filter(Boolean));
237
+ }
238
+
239
+ var context = this.getContext();
240
+ var schema = context.getSchema();
241
+ var jsModuleType = schema.asScalarFieldType(schema.getTypeFromString(JS_FIELD_TYPE));
242
+
243
+ if (jsModuleType == null || !schema.isServerType(jsModuleType)) {
244
+ throw new createUserError("'".concat(JS_FIELD_NAME, "' should be defined on the server schema."), [spread.loc]);
245
+ }
246
+
247
+ if (!schema.isScalar(jsModuleType)) {
248
+ throw createUserError('Using @module requires the schema to define a scalar ' + "'".concat(JS_FIELD_TYPE, "' type."));
249
+ }
250
+
251
+ var fragment = context.getFragment(spread.name, spread.loc);
252
+
253
+ if (!schema.isObject(fragment.type)) {
254
+ throw createUserError("@module used on invalid fragment spread '...".concat(spread.name, "'. @module ") + 'may only be used with fragments on a concrete (object) type, ' + "but the fragment has abstract type '".concat(schema.getTypeString(fragment.type), "'."), [spread.loc, fragment.loc]);
255
+ }
256
+
257
+ var field = schema.getFieldByName(fragment.type, JS_FIELD_NAME);
258
+
259
+ if (!field) {
260
+ throw createUserError("@module used on invalid fragment spread '...".concat(spread.name, "'. @module ") + "requires the fragment type '".concat(schema.getTypeString(fragment.type), "' to have a ") + "'".concat(JS_FIELD_NAME, "(").concat(JS_FIELD_MODULE_ARG, ": String! ") + "[".concat(JS_FIELD_ID_ARG, ": String]): ").concat(JS_FIELD_TYPE, "' field (your ") + "schema may choose to omit the 'id' argument but if present it " + "must accept a 'String').", [moduleDirective.loc]);
261
+ }
262
+
263
+ var jsField = schema.getFieldConfig(field);
264
+ var jsFieldModuleArg = jsField ? jsField.args.find(function (arg) {
265
+ return arg.name === JS_FIELD_MODULE_ARG;
266
+ }) : null;
267
+ var jsFieldIdArg = jsField ? jsField.args.find(function (arg) {
268
+ return arg.name === JS_FIELD_ID_ARG;
269
+ }) : null;
270
+
271
+ if (jsFieldModuleArg == null || !schema.isString(schema.getNullableType(jsFieldModuleArg.type)) || jsFieldIdArg != null && !schema.isString(jsFieldIdArg.type) || jsField.type !== jsModuleType) {
272
+ throw createUserError("@module used on invalid fragment spread '...".concat(spread.name, "'. @module ") + "requires the fragment type '".concat(schema.getTypeString(fragment.type), "' to have a ") + "'".concat(JS_FIELD_NAME, "(").concat(JS_FIELD_MODULE_ARG, ": String! ") + "[".concat(JS_FIELD_ID_ARG, ": String]): ").concat(JS_FIELD_TYPE, "' field (your ") + "schema may choose to omit the 'id' argument but if present it " + "must accept a 'String').", [moduleDirective.loc]);
273
+ }
274
+
275
+ if (spread.directives.length !== 1) {
276
+ throw createUserError("@module used on invalid fragment spread '...".concat(spread.name, "'. @module ") + 'may not have additional directives.', [spread.loc]);
277
+ }
278
+
279
+ var _getLiteralArgumentVa2 = getLiteralArgumentValues(moduleDirective.args),
280
+ moduleName = _getLiteralArgumentVa2.name;
281
+
282
+ if (typeof moduleName !== 'string') {
283
+ var _moduleDirective$args;
284
+
285
+ throw createUserError("Expected the 'name' argument of @module to be a literal string", [((_moduleDirective$args = moduleDirective.args.find(function (arg) {
286
+ return arg.name === 'name';
287
+ })) !== null && _moduleDirective$args !== void 0 ? _moduleDirective$args : spread).loc]);
288
+ }
289
+
290
+ var parentField = path[path.length - 1];
291
+ var moduleKey = moduleKeyFromParent !== null && moduleKeyFromParent !== void 0 ? moduleKeyFromParent : documentName;
292
+ var aliasPath = path.map(function (x) {
293
+ return x.alias;
294
+ }).join('.');
295
+ var moduleId = aliasPath === '' ? documentName : "".concat(documentName, ".").concat(aliasPath);
296
+ var typeName = schema.getTypeString(fragment.type);
297
+ var matches = matchesForPath.get(aliasPath);
298
+
299
+ if (matches == null) {
300
+ var _parentField$loc;
301
+
302
+ if (matchesForPath.size !== 0) {
303
+ var existingMatchWithKey = Array.from(matchesForPath.values()).find(function (entry) {
304
+ return entry.key === moduleKey;
305
+ });
306
+
307
+ if (existingMatchWithKey != null) {
308
+ if (parentField == null) {
309
+ throw createCompilerError('Cannot have @module selections at multiple paths unless the selections are within fields.', [spread.loc]);
310
+ }
311
+
312
+ throw createUserError('Invalid @module selection: documents with multiple fields ' + "containing 3D selections must specify a unique 'key' value " + "for each field: use '".concat(parentField.alias, " @match(key: \"").concat(documentName, "_<localName>\")'."), [parentField.loc]);
313
+ }
314
+ }
315
+
316
+ matches = {
317
+ key: moduleKey,
318
+ location: (_parentField$loc = parentField === null || parentField === void 0 ? void 0 : parentField.loc) !== null && _parentField$loc !== void 0 ? _parentField$loc : spread.loc,
319
+ types: new Map()
320
+ };
321
+ matchesForPath.set(aliasPath, matches);
322
+ }
323
+
324
+ if (moduleKey !== matches.key) {
325
+ var _parentField$loc2;
326
+
327
+ // The user can't override the key locally (per @module),
328
+ // so this is just an internal sanity check
329
+ throw createCompilerError('Invalid @module selection: expected all selections at path ' + "'".concat(aliasPath, " to have the same 'key', got '").concat(moduleKey, "' and '").concat(matches.key, "'."), [(_parentField$loc2 = parentField === null || parentField === void 0 ? void 0 : parentField.loc) !== null && _parentField$loc2 !== void 0 ? _parentField$loc2 : spread.loc]);
330
+ }
331
+
332
+ var previousMatchForType = matches.types.get(typeName);
333
+
334
+ if (previousMatchForType != null && (previousMatchForType.fragment !== spread.name || previousMatchForType.module !== moduleName)) {
335
+ throw createUserError('Invalid @module selection: concrete type ' + "'".concat(typeName, "' was matched multiple times at path ") + "'".concat(aliasPath, "' but with a different fragment or module name."), [spread.loc, previousMatchForType.location]);
336
+ }
337
+
338
+ matches.types.set(typeName, {
339
+ location: spread.loc,
340
+ fragment: spread.name,
341
+ module: moduleName
342
+ });
343
+ var normalizationName = getNormalizationOperationName(spread.name) + '.graphql';
344
+ var componentKey = getModuleComponentKey(moduleKey);
345
+ var componentField = {
346
+ alias: componentKey,
347
+ args: [{
348
+ kind: 'Argument',
349
+ name: JS_FIELD_MODULE_ARG,
350
+ type: jsFieldModuleArg.type,
351
+ value: {
352
+ kind: 'Literal',
353
+ loc: (_moduleDirective$args2 = (_moduleDirective$args3 = moduleDirective.args[0]) === null || _moduleDirective$args3 === void 0 ? void 0 : _moduleDirective$args3.loc) !== null && _moduleDirective$args2 !== void 0 ? _moduleDirective$args2 : moduleDirective.loc,
354
+ value: moduleName
355
+ },
356
+ loc: moduleDirective.loc
357
+ }, jsFieldIdArg != null ? {
358
+ kind: 'Argument',
359
+ name: JS_FIELD_ID_ARG,
360
+ type: jsFieldIdArg.type,
361
+ value: {
362
+ kind: 'Literal',
363
+ loc: (_moduleDirective$args4 = (_moduleDirective$args5 = moduleDirective.args[0]) === null || _moduleDirective$args5 === void 0 ? void 0 : _moduleDirective$args5.loc) !== null && _moduleDirective$args4 !== void 0 ? _moduleDirective$args4 : moduleDirective.loc,
364
+ value: moduleId
365
+ },
366
+ loc: moduleDirective.loc
367
+ } : null].filter(Boolean),
368
+ directives: [],
369
+ handles: null,
370
+ kind: 'ScalarField',
371
+ loc: moduleDirective.loc,
372
+ metadata: {
373
+ skipNormalizationNode: true
374
+ },
375
+ name: JS_FIELD_NAME,
376
+ type: jsModuleType
377
+ };
378
+ var operationKey = getModuleOperationKey(moduleKey);
379
+ var operationField = {
380
+ alias: operationKey,
381
+ args: [{
382
+ kind: 'Argument',
383
+ name: JS_FIELD_MODULE_ARG,
384
+ type: jsFieldModuleArg.type,
385
+ value: {
386
+ kind: 'Literal',
387
+ loc: moduleDirective.loc,
388
+ value: normalizationName
389
+ },
390
+ loc: moduleDirective.loc
391
+ }, jsFieldIdArg != null ? {
392
+ kind: 'Argument',
393
+ name: JS_FIELD_ID_ARG,
394
+ type: jsFieldIdArg.type,
395
+ value: {
396
+ kind: 'Literal',
397
+ loc: (_moduleDirective$args6 = (_moduleDirective$args7 = moduleDirective.args[0]) === null || _moduleDirective$args7 === void 0 ? void 0 : _moduleDirective$args7.loc) !== null && _moduleDirective$args6 !== void 0 ? _moduleDirective$args6 : moduleDirective.loc,
398
+ value: moduleId
399
+ },
400
+ loc: moduleDirective.loc
401
+ } : null].filter(Boolean),
402
+ directives: [],
403
+ handles: null,
404
+ kind: 'ScalarField',
405
+ loc: moduleDirective.loc,
406
+ metadata: {
407
+ skipNormalizationNode: true
408
+ },
409
+ name: JS_FIELD_NAME,
410
+ type: jsModuleType
411
+ };
412
+ return {
413
+ kind: 'InlineFragment',
414
+ directives: [],
415
+ loc: moduleDirective.loc,
416
+ metadata: null,
417
+ selections: [{
418
+ kind: 'ModuleImport',
419
+ loc: moduleDirective.loc,
420
+ key: moduleKey,
421
+ id: moduleId,
422
+ module: moduleName,
423
+ sourceDocument: documentName,
424
+ name: spread.name,
425
+ selections: [(0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, spread), {}, {
426
+ directives: spread.directives.filter(function (directive) {
427
+ return directive !== moduleDirective;
428
+ })
429
+ }), operationField, componentField]
430
+ }],
431
+ typeCondition: fragment.type
432
+ };
433
+ }
434
+
435
+ module.exports = {
436
+ SCHEMA_EXTENSION: SCHEMA_EXTENSION,
437
+ transform: matchTransform
438
+ };
@@ -0,0 +1,161 @@
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
+ *
8
+ * @format
9
+ * @emails oncall+relay
10
+ */
11
+ 'use strict';
12
+
13
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
14
+
15
+ var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
16
+
17
+ var IRTransformer = require('../core/IRTransformer');
18
+
19
+ var _require = require('../core/CompilerError'),
20
+ createUserError = _require.createUserError,
21
+ createCompilerError = _require.createCompilerError;
22
+
23
+ var _require2 = require('relay-runtime'),
24
+ RelayFeatureFlags = _require2.RelayFeatureFlags;
25
+
26
+ var FLIGHT_FIELD_COMPONENT_ARGUMENT_TYPE = 'String';
27
+ var FLIGHT_FIELD_COMPONENT_ARGUMENT_NAME = 'component';
28
+ var FLIGHT_FIELD_PROPS_ARGUMENT_NAME = 'props';
29
+ var FLIGHT_FIELD_PROPS_TYPE = 'ReactFlightProps';
30
+ var FLIGHT_FIELD_RETURN_TYPE = 'ReactFlightComponent';
31
+
32
+ /**
33
+ * Experimental transform for React Flight.
34
+ */
35
+ function reactFlightComponentTransform(context) {
36
+ var schema = context.getSchema();
37
+ var propsType = schema.getTypeFromString(FLIGHT_FIELD_PROPS_TYPE);
38
+ propsType = propsType ? schema.asInputType(propsType) : null;
39
+ var componentType = schema.getTypeFromString(FLIGHT_FIELD_RETURN_TYPE);
40
+ componentType = componentType ? schema.asScalarFieldType(componentType) : null;
41
+
42
+ if (!RelayFeatureFlags.ENABLE_REACT_FLIGHT_COMPONENT_FIELD || propsType == null || componentType == null) {
43
+ return context;
44
+ }
45
+
46
+ var types = {
47
+ propsType: propsType,
48
+ componentType: componentType
49
+ };
50
+ return IRTransformer.transform(context, {
51
+ ScalarField: visitScalarField,
52
+ LinkedField: visitLinkedField,
53
+ InlineFragment: visitInlineFragment
54
+ }, function (node) {
55
+ return {
56
+ parentType: node.type,
57
+ types: types
58
+ };
59
+ });
60
+ }
61
+
62
+ function visitInlineFragment(fragment, state) {
63
+ var _fragment$typeConditi;
64
+
65
+ // $FlowFixMe[incompatible-use]
66
+ return this.traverse(fragment, {
67
+ parentType: (_fragment$typeConditi = fragment.typeCondition) !== null && _fragment$typeConditi !== void 0 ? _fragment$typeConditi : state.parentType,
68
+ types: state.types
69
+ });
70
+ }
71
+
72
+ function visitLinkedField(field, state) {
73
+ // $FlowFixMe[incompatible-use]
74
+ return this.traverse(field, {
75
+ parentType: field.type,
76
+ types: state.types
77
+ });
78
+ }
79
+
80
+ function visitScalarField(field, state) {
81
+ // use the return type to quickly determine if this is a flight field
82
+ // $FlowFixMe[incompatible-use]
83
+ var schema = this.getContext().getSchema();
84
+
85
+ if (schema.getRawType(field.type) !== state.types.componentType) {
86
+ return field;
87
+ } // get the name of the component that provides this field
88
+
89
+
90
+ var clientField = schema.getFieldByName(state.parentType, field.name);
91
+
92
+ if (clientField == null) {
93
+ throw createCompilerError("Definition not found for field '".concat(schema.getTypeString(state.parentType), ".").concat(field.name, "'"), [field.loc]);
94
+ }
95
+
96
+ var componentDirective = clientField.directives.find(function (directive) {
97
+ return directive.name === 'react_flight_component';
98
+ });
99
+ var componentNameArg = componentDirective === null || componentDirective === void 0 ? void 0 : componentDirective.args.find(function (arg) {
100
+ return arg.name === 'name';
101
+ });
102
+
103
+ if (componentNameArg == null || componentNameArg.value.kind !== 'StringValue' || typeof componentNameArg.value.value !== 'string') {
104
+ throw createUserError('Invalid Flight field, expected the schema extension to specify ' + "the component's module name with the '@react_flight_component' directive", [field.loc]);
105
+ }
106
+
107
+ var componentName = componentNameArg.value.value; // validate that the parent type has a `flight(component, props)` field
108
+
109
+ var flightField = schema.getFieldByName(state.parentType, 'flight');
110
+
111
+ if (flightField == null) {
112
+ throw createUserError("Invalid Flight field, expected the parent type '".concat(schema.getTypeString(state.parentType), "' ") + "to define a 'flight(component: String, props: ReactFlightProps): ReactFlightComponent' field", [field.loc]);
113
+ }
114
+
115
+ var componentArg = flightField.args.get(FLIGHT_FIELD_COMPONENT_ARGUMENT_NAME);
116
+ var propsArg = flightField.args.get(FLIGHT_FIELD_PROPS_ARGUMENT_NAME);
117
+
118
+ if (componentArg == null || propsArg == null || schema.getRawType(componentArg.type) !== schema.getTypeFromString(FLIGHT_FIELD_COMPONENT_ARGUMENT_TYPE) || schema.getRawType(propsArg.type) !== state.types.propsType || schema.getRawType(flightField.type) !== state.types.componentType) {
119
+ throw createUserError("Invalid Flight field, expected the parent type '".concat(schema.getTypeString(state.parentType), "' ") + "to define a 'flight(component: String, props: ReactFlightProps): ReactFlightComponent' field", [field.loc]);
120
+ }
121
+
122
+ return (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, field), {}, {
123
+ name: 'flight',
124
+ args: [{
125
+ kind: 'Argument',
126
+ loc: field.loc,
127
+ name: FLIGHT_FIELD_COMPONENT_ARGUMENT_NAME,
128
+ type: schema.getTypeFromString(FLIGHT_FIELD_COMPONENT_ARGUMENT_TYPE),
129
+ value: {
130
+ kind: 'Literal',
131
+ value: componentName,
132
+ loc: field.loc
133
+ }
134
+ }, {
135
+ kind: 'Argument',
136
+ loc: field.loc,
137
+ name: FLIGHT_FIELD_PROPS_ARGUMENT_NAME,
138
+ type: state.types.propsType,
139
+ value: {
140
+ kind: 'ObjectValue',
141
+ fields: field.args.map(function (arg) {
142
+ return {
143
+ kind: 'ObjectFieldValue',
144
+ loc: arg.loc,
145
+ name: arg.name,
146
+ value: arg.value
147
+ };
148
+ }),
149
+ loc: field.loc
150
+ }
151
+ }],
152
+ metadata: (0, _objectSpread2["default"])((0, _objectSpread2["default"])({}, field.metadata || {}), {}, {
153
+ flight: true
154
+ }),
155
+ type: state.types.componentType
156
+ });
157
+ }
158
+
159
+ module.exports = {
160
+ transform: reactFlightComponentTransform
161
+ };