relay-compiler 9.0.0 → 9.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (197) hide show
  1. package/bin/RelayCompilerBin.js.flow +169 -0
  2. package/bin/RelayCompilerMain.js.flow +508 -0
  3. package/bin/__fixtures__/plugin-module.js.flow +17 -0
  4. package/bin/relay-compiler +2002 -1733
  5. package/codegen/CodegenDirectory.js.flow +375 -0
  6. package/codegen/CodegenRunner.js.flow +431 -0
  7. package/codegen/CodegenTypes.js.flow +28 -0
  8. package/codegen/CodegenWatcher.js.flow +254 -0
  9. package/codegen/NormalizationCodeGenerator.js.flow +499 -0
  10. package/codegen/ReaderCodeGenerator.js.flow +453 -0
  11. package/codegen/RelayCodeGenerator.js.flow +76 -0
  12. package/codegen/RelayFileWriter.js.flow +366 -0
  13. package/codegen/SourceControl.js.flow +58 -0
  14. package/codegen/compileRelayArtifacts.js.flow +182 -0
  15. package/codegen/createPrintRequireModuleDependency.js.flow +21 -0
  16. package/codegen/writeRelayGeneratedFile.js.flow +194 -0
  17. package/core/ASTCache.js.flow +73 -0
  18. package/core/ASTConvert.js.flow +233 -0
  19. package/core/CompilerContext.js.flow +190 -0
  20. package/core/CompilerError.js.flow +250 -0
  21. package/core/DotGraphQLParser.js.flow +39 -0
  22. package/core/GraphQLCompilerProfiler.js.flow +341 -0
  23. package/core/GraphQLDerivedFromMetadata.js.flow +48 -0
  24. package/core/GraphQLWatchmanClient.js.flow +111 -0
  25. package/core/IR.js.flow +329 -0
  26. package/core/IRPrinter.js.flow +488 -0
  27. package/core/IRTransformer.js.flow +377 -0
  28. package/core/IRValidator.js.flow +260 -0
  29. package/core/IRVisitor.js.flow +150 -0
  30. package/core/JSModuleParser.js.flow +24 -0
  31. package/core/RelayCompilerScope.js.flow +199 -0
  32. package/core/RelayFindGraphQLTags.js.flow +119 -0
  33. package/core/RelayGraphQLEnumsGenerator.js.flow +55 -0
  34. package/core/RelayIRTransforms.js.flow +130 -0
  35. package/core/RelayParser.js.flow +1759 -0
  36. package/core/RelaySourceModuleParser.js.flow +135 -0
  37. package/core/Schema.js.flow +1985 -0
  38. package/core/SchemaUtils.js.flow +109 -0
  39. package/core/filterContextForNode.js.flow +50 -0
  40. package/core/getFieldDefinition.js.flow +156 -0
  41. package/core/getIdentifierForArgumentValue.js.flow +49 -0
  42. package/core/getIdentifierForSelection.js.flow +69 -0
  43. package/core/getLiteralArgumentValues.js.flow +32 -0
  44. package/core/getNormalizationOperationName.js.flow +19 -0
  45. package/core/inferRootArgumentDefinitions.js.flow +323 -0
  46. package/index.js +1 -1
  47. package/index.js.flow +202 -0
  48. package/language/RelayLanguagePluginInterface.js.flow +283 -0
  49. package/language/javascript/FindGraphQLTags.js.flow +233 -0
  50. package/language/javascript/RelayFlowBabelFactories.js.flow +180 -0
  51. package/language/javascript/RelayFlowGenerator.js.flow +1040 -0
  52. package/language/javascript/RelayFlowTypeTransformers.js.flow +184 -0
  53. package/language/javascript/RelayLanguagePluginJavaScript.js.flow +34 -0
  54. package/language/javascript/formatGeneratedModule.js.flow +65 -0
  55. package/lib/bin/RelayCompilerBin.js +10 -0
  56. package/lib/bin/RelayCompilerMain.js +113 -119
  57. package/lib/codegen/CodegenDirectory.js +2 -6
  58. package/lib/codegen/CodegenRunner.js +34 -75
  59. package/lib/codegen/CodegenWatcher.js +13 -21
  60. package/lib/codegen/NormalizationCodeGenerator.js +43 -40
  61. package/lib/codegen/ReaderCodeGenerator.js +43 -35
  62. package/lib/codegen/RelayCodeGenerator.js +7 -5
  63. package/lib/codegen/RelayFileWriter.js +15 -36
  64. package/lib/codegen/compileRelayArtifacts.js +16 -30
  65. package/lib/codegen/writeRelayGeneratedFile.js +56 -98
  66. package/lib/core/ASTCache.js +1 -3
  67. package/lib/core/CompilerContext.js +1 -3
  68. package/lib/core/CompilerError.js +27 -54
  69. package/lib/core/GraphQLCompilerProfiler.js +8 -12
  70. package/lib/core/GraphQLWatchmanClient.js +4 -12
  71. package/lib/core/IRPrinter.js +5 -5
  72. package/lib/core/IRTransformer.js +8 -6
  73. package/lib/core/IRValidator.js +1 -3
  74. package/lib/core/RelayCompilerScope.js +4 -4
  75. package/lib/core/RelayGraphQLEnumsGenerator.js +12 -15
  76. package/lib/core/RelayIRTransforms.js +9 -5
  77. package/lib/core/RelayParser.js +44 -56
  78. package/lib/core/RelaySourceModuleParser.js +1 -3
  79. package/lib/core/Schema.js +78 -65
  80. package/lib/core/getFieldDefinition.js +12 -15
  81. package/lib/core/getIdentifierForSelection.js +1 -1
  82. package/lib/core/inferRootArgumentDefinitions.js +27 -36
  83. package/lib/language/javascript/RelayFlowGenerator.js +49 -78
  84. package/lib/language/javascript/RelayFlowTypeTransformers.js +1 -3
  85. package/lib/reporters/ConsoleReporter.js +1 -3
  86. package/lib/reporters/MultiReporter.js +1 -3
  87. package/lib/runner/Artifacts.js +69 -170
  88. package/lib/runner/BufferedFilesystem.js +32 -66
  89. package/lib/runner/GraphQLASTNodeGroup.js +54 -120
  90. package/lib/runner/GraphQLNodeMap.js +14 -19
  91. package/lib/runner/Sources.js +49 -78
  92. package/lib/runner/StrictMap.js +20 -36
  93. package/lib/runner/getChangedNodeNames.js +30 -62
  94. package/lib/transforms/ApplyFragmentArgumentTransform.js +33 -28
  95. package/lib/transforms/ClientExtensionsTransform.js +7 -12
  96. package/lib/transforms/ConnectionTransform.js +26 -24
  97. package/lib/transforms/DeferStreamTransform.js +20 -16
  98. package/lib/transforms/DisallowTypenameOnRoot.js +55 -0
  99. package/lib/transforms/FieldHandleTransform.js +6 -2
  100. package/lib/transforms/FlattenTransform.js +19 -14
  101. package/lib/transforms/GenerateIDFieldTransform.js +7 -3
  102. package/lib/transforms/GenerateTypeNameTransform.js +6 -2
  103. package/lib/transforms/InlineDataFragmentTransform.js +7 -3
  104. package/lib/transforms/MaskTransform.js +17 -17
  105. package/lib/transforms/MatchTransform.js +110 -32
  106. package/lib/transforms/RefetchableFragmentTransform.js +21 -17
  107. package/lib/transforms/RelayDirectiveTransform.js +11 -3
  108. package/lib/transforms/SkipClientExtensionsTransform.js +8 -0
  109. package/lib/transforms/SkipHandleFieldTransform.js +6 -2
  110. package/lib/transforms/SkipRedundantNodesTransform.js +6 -2
  111. package/lib/transforms/SkipSplitOperationTransform.js +32 -0
  112. package/lib/transforms/SkipUnreachableNodeTransform.js +9 -2
  113. package/lib/transforms/SkipUnusedVariablesTransform.js +18 -17
  114. package/lib/transforms/SplitModuleImportTransform.js +2 -2
  115. package/lib/transforms/TestOperationTransform.js +7 -3
  116. package/lib/transforms/ValidateGlobalVariablesTransform.js +18 -30
  117. package/lib/transforms/ValidateRequiredArgumentsTransform.js +12 -15
  118. package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +16 -30
  119. package/lib/transforms/ValidateUnusedVariablesTransform.js +18 -30
  120. package/lib/transforms/query-generators/FetchableQueryGenerator.js +161 -0
  121. package/lib/transforms/query-generators/NodeQueryGenerator.js +7 -2
  122. package/lib/transforms/query-generators/QueryQueryGenerator.js +1 -0
  123. package/lib/transforms/query-generators/ViewerQueryGenerator.js +1 -0
  124. package/lib/transforms/query-generators/index.js +23 -6
  125. package/lib/transforms/query-generators/utils.js +12 -15
  126. package/lib/util/RelayCompilerCache.js +1 -3
  127. package/lib/util/dedupeJSONStringify.js +15 -12
  128. package/lib/util/getModuleName.js +1 -1
  129. package/package.json +2 -2
  130. package/relay-compiler.js +4 -4
  131. package/relay-compiler.min.js +4 -4
  132. package/reporters/ConsoleReporter.js.flow +81 -0
  133. package/reporters/MultiReporter.js.flow +43 -0
  134. package/reporters/Reporter.js.flow +19 -0
  135. package/runner/Artifacts.js.flow +219 -0
  136. package/runner/BufferedFilesystem.js.flow +194 -0
  137. package/runner/GraphQLASTNodeGroup.js.flow +176 -0
  138. package/runner/GraphQLASTUtils.js.flow +26 -0
  139. package/runner/GraphQLNodeMap.js.flow +55 -0
  140. package/runner/Sources.js.flow +218 -0
  141. package/runner/StrictMap.js.flow +96 -0
  142. package/runner/compileArtifacts.js.flow +76 -0
  143. package/runner/extractAST.js.flow +100 -0
  144. package/runner/getChangedNodeNames.js.flow +48 -0
  145. package/runner/getSchemaInstance.js.flow +36 -0
  146. package/runner/types.js.flow +37 -0
  147. package/transforms/ApplyFragmentArgumentTransform.js.flow +474 -0
  148. package/transforms/ClientExtensionsTransform.js.flow +220 -0
  149. package/transforms/ConnectionTransform.js.flow +869 -0
  150. package/transforms/DeferStreamTransform.js.flow +258 -0
  151. package/transforms/DisallowIdAsAlias.js.flow +47 -0
  152. package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
  153. package/transforms/FieldHandleTransform.js.flow +80 -0
  154. package/transforms/FilterDirectivesTransform.js.flow +45 -0
  155. package/transforms/FlattenTransform.js.flow +456 -0
  156. package/transforms/GenerateIDFieldTransform.js.flow +134 -0
  157. package/transforms/GenerateTypeNameTransform.js.flow +81 -0
  158. package/transforms/InlineDataFragmentTransform.js.flow +124 -0
  159. package/transforms/InlineFragmentsTransform.js.flow +71 -0
  160. package/transforms/MaskTransform.js.flow +126 -0
  161. package/transforms/MatchTransform.js.flow +583 -0
  162. package/transforms/RefetchableFragmentTransform.js.flow +272 -0
  163. package/transforms/RelayDirectiveTransform.js.flow +99 -0
  164. package/transforms/SkipClientExtensionsTransform.js.flow +54 -0
  165. package/transforms/SkipHandleFieldTransform.js.flow +44 -0
  166. package/transforms/SkipRedundantNodesTransform.js.flow +253 -0
  167. package/transforms/SkipSplitOperationTransform.js.flow +37 -0
  168. package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
  169. package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
  170. package/transforms/SplitModuleImportTransform.js.flow +98 -0
  171. package/transforms/TestOperationTransform.js.flow +138 -0
  172. package/transforms/TransformUtils.js.flow +26 -0
  173. package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
  174. package/transforms/ValidateRequiredArgumentsTransform.js.flow +127 -0
  175. package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +112 -0
  176. package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
  177. package/transforms/query-generators/FetchableQueryGenerator.js.flow +190 -0
  178. package/transforms/query-generators/NodeQueryGenerator.js.flow +206 -0
  179. package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
  180. package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
  181. package/transforms/query-generators/index.js.flow +90 -0
  182. package/transforms/query-generators/utils.js.flow +72 -0
  183. package/util/CodeMarker.js.flow +79 -0
  184. package/util/DefaultHandleKey.js.flow +17 -0
  185. package/util/RelayCompilerCache.js.flow +88 -0
  186. package/util/Rollout.js.flow +39 -0
  187. package/util/TimeReporter.js.flow +79 -0
  188. package/util/areEqualOSS.js.flow +123 -0
  189. package/util/dedupeJSONStringify.js.flow +152 -0
  190. package/util/getDefinitionNodeHash.js.flow +25 -0
  191. package/util/getModuleName.js.flow +39 -0
  192. package/util/joinArgumentDefinitions.js.flow +99 -0
  193. package/util/md5.js.flow +22 -0
  194. package/util/murmurHash.js.flow +94 -0
  195. package/util/nullthrowsOSS.js.flow +25 -0
  196. package/util/orList.js.flow +37 -0
  197. package/util/partitionArray.js.flow +37 -0
@@ -0,0 +1,109 @@
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
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ import type {ScalarField} from '../core/IR';
16
+ import type {ScalarFieldTypeID} from './Schema';
17
+ import type {Schema, InputTypeID} from './Schema';
18
+ import type {ASTNode} from 'graphql';
19
+
20
+ const ID = 'id';
21
+
22
+ /**
23
+ * @public
24
+ *
25
+ * Determine if an AST node contains a fragment/operation definition.
26
+ */
27
+ function isExecutableDefinitionAST(ast: ASTNode): boolean %checks {
28
+ return (
29
+ ast.kind === 'FragmentDefinition' || ast.kind === 'OperationDefinition'
30
+ );
31
+ }
32
+
33
+ /**
34
+ * @public
35
+ *
36
+ * Determine if an AST node contains a schema definition.
37
+ */
38
+ function isSchemaDefinitionAST(ast: ASTNode): boolean %checks {
39
+ return (
40
+ ast.kind === 'SchemaDefinition' ||
41
+ ast.kind === 'ScalarTypeDefinition' ||
42
+ ast.kind === 'ObjectTypeDefinition' ||
43
+ ast.kind === 'InterfaceTypeDefinition' ||
44
+ ast.kind === 'UnionTypeDefinition' ||
45
+ ast.kind === 'EnumTypeDefinition' ||
46
+ ast.kind === 'InputObjectTypeDefinition' ||
47
+ ast.kind === 'DirectiveDefinition' ||
48
+ ast.kind === 'ScalarTypeExtension' ||
49
+ ast.kind === 'ObjectTypeExtension' ||
50
+ ast.kind === 'InterfaceTypeExtension' ||
51
+ ast.kind === 'UnionTypeExtension' ||
52
+ ast.kind === 'EnumTypeExtension' ||
53
+ ast.kind === 'InputObjectTypeExtension'
54
+ );
55
+ }
56
+
57
+ function generateIDField(idType: ScalarFieldTypeID): ScalarField {
58
+ return {
59
+ kind: 'ScalarField',
60
+ alias: ID,
61
+ args: [],
62
+ directives: [],
63
+ handles: null,
64
+ loc: {kind: 'Generated'},
65
+ metadata: null,
66
+ name: ID,
67
+ type: idType,
68
+ };
69
+ }
70
+
71
+ function getNullableBooleanInput(schema: Schema): InputTypeID {
72
+ return schema.assertInputType(schema.expectBooleanType());
73
+ }
74
+
75
+ function getNonNullBooleanInput(schema: Schema): InputTypeID {
76
+ return schema.assertInputType(
77
+ schema.getNonNullType(schema.expectBooleanType()),
78
+ );
79
+ }
80
+
81
+ function getNullableStringInput(schema: Schema): InputTypeID {
82
+ return schema.assertInputType(schema.expectStringType());
83
+ }
84
+
85
+ function getNonNullStringInput(schema: Schema): InputTypeID {
86
+ return schema.assertInputType(
87
+ schema.getNonNullType(schema.expectStringType()),
88
+ );
89
+ }
90
+
91
+ function getNullableIdInput(schema: Schema): InputTypeID {
92
+ return schema.assertInputType(schema.expectIdType());
93
+ }
94
+
95
+ function getNonNullIdInput(schema: Schema): InputTypeID {
96
+ return schema.assertInputType(schema.getNonNullType(schema.expectIdType()));
97
+ }
98
+
99
+ module.exports = {
100
+ generateIDField,
101
+ isExecutableDefinitionAST,
102
+ isSchemaDefinitionAST,
103
+ getNullableBooleanInput,
104
+ getNonNullBooleanInput,
105
+ getNullableStringInput,
106
+ getNonNullStringInput,
107
+ getNullableIdInput,
108
+ getNonNullIdInput,
109
+ };
@@ -0,0 +1,50 @@
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
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const CompilerContext = require('./CompilerContext');
16
+
17
+ const {visit} = require('./IRVisitor');
18
+
19
+ import type {Fragment, FragmentSpread, Root} from './IR';
20
+
21
+ /**
22
+ * Returns a CompilerContext containing only the documents referenced
23
+ * by and including the provided node.
24
+ */
25
+ function filterContextForNode(
26
+ node: Fragment | Root,
27
+ context: CompilerContext,
28
+ ): CompilerContext {
29
+ const queue = [node];
30
+ let filteredContext = new CompilerContext(context.getSchema()).add(node);
31
+ const visitFragmentSpread = (fragmentSpread: FragmentSpread) => {
32
+ const {name} = fragmentSpread;
33
+ if (!filteredContext.get(name)) {
34
+ const fragment = context.getFragment(name);
35
+ filteredContext = filteredContext.add(fragment);
36
+ queue.push(fragment);
37
+ }
38
+ };
39
+ const visitorConfig = {
40
+ FragmentSpread: (fragmentSpread: FragmentSpread) => {
41
+ visitFragmentSpread(fragmentSpread);
42
+ },
43
+ };
44
+ while (queue.length) {
45
+ visit(queue.pop(), visitorConfig);
46
+ }
47
+ return filteredContext;
48
+ }
49
+
50
+ module.exports = filterContextForNode;
@@ -0,0 +1,156 @@
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
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const {createCompilerError} = require('./CompilerError');
16
+ const {SchemaMetaFieldDef, TypeMetaFieldDef} = require('graphql');
17
+
18
+ import type {Schema, TypeID, FieldID} from './Schema';
19
+ import type {FieldNode} from 'graphql';
20
+
21
+ export type GetFieldDefinitionFn = (
22
+ schema: Schema,
23
+ parentType: TypeID,
24
+ fieldName: string,
25
+ fieldAST: FieldNode,
26
+ ) => ?FieldID;
27
+
28
+ /**
29
+ * Find the definition of a field of the specified type using strict
30
+ * resolution rules per the GraphQL spec.
31
+ */
32
+ function getFieldDefinitionStrict(
33
+ schema: Schema,
34
+ parentType: TypeID,
35
+ fieldName: string,
36
+ ): ?FieldID {
37
+ const type = schema.getRawType(parentType);
38
+ const queryType = schema.getQueryType();
39
+ const isQueryType =
40
+ queryType != null && schema.areEqualTypes(type, queryType);
41
+ const hasTypeName = schema.isAbstractType(type) || schema.isObject(type);
42
+
43
+ let schemaFieldDef;
44
+ if (isQueryType && fieldName === SchemaMetaFieldDef.name) {
45
+ schemaFieldDef =
46
+ queryType != null ? schema.getFieldByName(queryType, '__schema') : null;
47
+ } else if (isQueryType && fieldName === TypeMetaFieldDef.name) {
48
+ schemaFieldDef =
49
+ queryType != null ? schema.getFieldByName(queryType, '__type') : null;
50
+ } else if (hasTypeName && fieldName === '__typename') {
51
+ schemaFieldDef = schema.getFieldByName(
52
+ schema.assertCompositeType(type),
53
+ '__typename',
54
+ );
55
+ } else if (hasTypeName && fieldName === '__id') {
56
+ schemaFieldDef = schema.getFieldByName(
57
+ schema.assertCompositeType(type),
58
+ '__id',
59
+ );
60
+ } else if (schema.isInterface(type) || schema.isObject(type)) {
61
+ const compositeType = schema.assertCompositeType(type);
62
+ if (schema.hasField(compositeType, fieldName)) {
63
+ schemaFieldDef = schema.getFieldByName(compositeType, fieldName);
64
+ } else {
65
+ return null;
66
+ }
67
+ }
68
+ return schemaFieldDef;
69
+ }
70
+
71
+ /**
72
+ * Find the definition of a field of the specified type, first trying
73
+ * the standard spec-compliant resolution process and falling back
74
+ * to legacy mode that supports fat interfaces.
75
+ */
76
+ function getFieldDefinitionLegacy(
77
+ schema: Schema,
78
+ parentType: TypeID,
79
+ fieldName: string,
80
+ fieldAST: FieldNode,
81
+ ): ?FieldID {
82
+ let schemaFieldDef = getFieldDefinitionStrict(schema, parentType, fieldName);
83
+
84
+ if (!schemaFieldDef) {
85
+ schemaFieldDef = getFieldDefinitionLegacyImpl(
86
+ schema,
87
+ parentType,
88
+ fieldName,
89
+ fieldAST,
90
+ );
91
+ }
92
+ return schemaFieldDef ?? null;
93
+ }
94
+
95
+ /**
96
+ * @private
97
+ */
98
+ function getFieldDefinitionLegacyImpl(
99
+ schema: Schema,
100
+ type: TypeID,
101
+ fieldName: string,
102
+ fieldAST: FieldNode,
103
+ ): ?FieldID {
104
+ const rawType = schema.getRawType(type);
105
+ if (
106
+ schema.isAbstractType(rawType) &&
107
+ fieldAST &&
108
+ fieldAST.directives &&
109
+ fieldAST.directives.some(
110
+ directive => getName(directive) === 'fixme_fat_interface',
111
+ )
112
+ ) {
113
+ const possibleTypes = schema.getPossibleTypes(
114
+ schema.assertAbstractType(rawType),
115
+ );
116
+ let schemaFieldDef;
117
+ for (const possibleType of possibleTypes) {
118
+ const possibleField = schema.getFieldByName(possibleType, fieldName);
119
+ if (possibleField) {
120
+ // Fat interface fields can have differing arguments. Try to return
121
+ // a field with matching arguments, but still return a field if the
122
+ // arguments do not match.
123
+ schemaFieldDef = possibleField;
124
+ if (fieldAST && fieldAST.arguments) {
125
+ const argumentsAllExist = fieldAST.arguments.every(
126
+ argument =>
127
+ schema.getFieldArgByName(possibleField, getName(argument)) !=
128
+ null,
129
+ );
130
+ if (argumentsAllExist) {
131
+ break;
132
+ }
133
+ }
134
+ }
135
+ }
136
+ return schemaFieldDef;
137
+ }
138
+ }
139
+
140
+ /**
141
+ * @private
142
+ */
143
+ function getName(ast): string {
144
+ const name = ast.name ? ast.name.value : null;
145
+ if (typeof name !== 'string') {
146
+ throw createCompilerError("Expected ast node to have a 'name'.", null, [
147
+ ast,
148
+ ]);
149
+ }
150
+ return name;
151
+ }
152
+
153
+ module.exports = {
154
+ getFieldDefinitionLegacy,
155
+ getFieldDefinitionStrict,
156
+ };
@@ -0,0 +1,49 @@
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
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ const invariant = require('invariant');
16
+
17
+ import type {ArgumentValue} from './IR';
18
+
19
+ /**
20
+ * Generates an identifier for an argument value. The identifier is based on the
21
+ * structure/order of items and keys in the value.
22
+ */
23
+ function getIdentifierForArgumentValue(value: ArgumentValue): mixed {
24
+ switch (value.kind) {
25
+ case 'Variable':
26
+ return {variable: value.variableName};
27
+ case 'Literal':
28
+ return {value: value.value};
29
+ case 'ListValue':
30
+ return {
31
+ list: value.items.map(item => getIdentifierForArgumentValue(item)),
32
+ };
33
+ case 'ObjectValue':
34
+ return {
35
+ object: value.fields.map(field => ({
36
+ name: field.name,
37
+ value: getIdentifierForArgumentValue(field.value),
38
+ })),
39
+ };
40
+ default:
41
+ invariant(
42
+ false,
43
+ 'getIdentifierForArgumentValue(): Unsupported AST kind `%s`.',
44
+ value.kind,
45
+ );
46
+ }
47
+ }
48
+
49
+ module.exports = getIdentifierForArgumentValue;
@@ -0,0 +1,69 @@
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 invariant = require('invariant');
16
+
17
+ const {printArguments, printDirectives} = require('./IRPrinter');
18
+
19
+ import type {Selection} from './IR';
20
+ import type {Schema} from './Schema';
21
+
22
+ /**
23
+ * Generates an identifier that is unique to a given selection: the alias for
24
+ * fields, the type for inline fragments, and a summary of the condition
25
+ * variable and passing value for conditions.
26
+ */
27
+ function getIdentifierForSelection(schema: Schema, node: Selection): string {
28
+ if (node.kind === 'LinkedField' || node.kind === 'ScalarField') {
29
+ return 'Field: ' + node.directives.length === 0
30
+ ? node.alias
31
+ : node.alias + printDirectives(schema, node.directives);
32
+ } else if (node.kind === 'FragmentSpread') {
33
+ return 'FragmentSpread:' + node.args.length === 0
34
+ ? node.name
35
+ : node.name + printArguments(schema, node.args);
36
+ } else if (node.kind === 'ModuleImport') {
37
+ return 'ModuleImport:';
38
+ } else if (node.kind === 'Defer') {
39
+ return 'Defer:' + node.label;
40
+ } else if (node.kind === 'Stream') {
41
+ return 'Stream:' + node.label;
42
+ } else if (node.kind === 'InlineFragment') {
43
+ return (
44
+ 'InlineFragment:' +
45
+ schema.getTypeString(node.typeCondition) +
46
+ printDirectives(schema, node.directives)
47
+ );
48
+ } else if (node.kind === 'ClientExtension') {
49
+ return 'ClientExtension:';
50
+ } else if (node.kind === 'InlineDataFragmentSpread') {
51
+ return 'InlineDataFragment:' + node.name;
52
+ } else if (node.kind === 'Condition') {
53
+ return (
54
+ 'Condition:' +
55
+ (node.condition.kind === 'Variable'
56
+ ? '$' + node.condition.variableName
57
+ : String(node.condition.value)) +
58
+ String(node.passingValue)
59
+ );
60
+ } else {
61
+ invariant(
62
+ false,
63
+ 'getIdentifierForSelection: Unexpected kind `%s`.',
64
+ (node.kind: empty),
65
+ );
66
+ }
67
+ }
68
+
69
+ module.exports = getIdentifierForSelection;
@@ -0,0 +1,32 @@
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
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ import type {Argument} from './IR';
16
+
17
+ // Copy of Variables type from '../../../react-relay/classic/tools/RelayTypes'
18
+ // Duplicating here rather than importing it since we can't take on a dependency
19
+ // outside of relay-compiler.
20
+ type Variables = {[name: string]: mixed, ...};
21
+
22
+ function getLiteralArgumentValues(args: $ReadOnlyArray<Argument>): Variables {
23
+ const values = {};
24
+ args.forEach(arg => {
25
+ if (arg.value.kind === 'Literal') {
26
+ values[arg.name] = arg.value.value;
27
+ }
28
+ });
29
+ return values;
30
+ }
31
+
32
+ module.exports = getLiteralArgumentValues;
@@ -0,0 +1,19 @@
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
8
+ * @format
9
+ */
10
+
11
+ // flowlint ambiguous-object-type:error
12
+
13
+ 'use strict';
14
+
15
+ function getNormalizationOperationName(name: string): string {
16
+ return `${name}$normalization`;
17
+ }
18
+
19
+ module.exports = getNormalizationOperationName;