relay-compiler 9.0.0 → 10.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/bin/RelayCompilerBin.js.flow +169 -0
- package/bin/RelayCompilerMain.js.flow +515 -0
- package/bin/__fixtures__/plugin-module.js.flow +17 -0
- package/bin/relay-compiler +3862 -2505
- package/codegen/CodegenDirectory.js.flow +375 -0
- package/codegen/CodegenRunner.js.flow +432 -0
- package/codegen/CodegenTypes.js.flow +28 -0
- package/codegen/CodegenWatcher.js.flow +254 -0
- package/codegen/NormalizationCodeGenerator.js.flow +571 -0
- package/codegen/ReaderCodeGenerator.js.flow +512 -0
- package/codegen/RelayCodeGenerator.js.flow +85 -0
- package/codegen/RelayFileWriter.js.flow +367 -0
- package/codegen/SourceControl.js.flow +58 -0
- package/codegen/compileRelayArtifacts.js.flow +182 -0
- package/codegen/createPrintRequireModuleDependency.js.flow +21 -0
- package/codegen/sortObjectByKey.js.flow +25 -0
- package/codegen/writeRelayGeneratedFile.js.flow +223 -0
- package/core/ASTCache.js.flow +74 -0
- package/core/ASTConvert.js.flow +233 -0
- package/core/CompilerContext.js.flow +191 -0
- package/core/CompilerError.js.flow +250 -0
- package/core/DotGraphQLParser.js.flow +39 -0
- package/core/GraphQLCompilerProfiler.js.flow +341 -0
- package/core/GraphQLDerivedFromMetadata.js.flow +36 -0
- package/core/GraphQLWatchmanClient.js.flow +111 -0
- package/core/IR.js.flow +326 -0
- package/core/IRPrinter.js.flow +477 -0
- package/core/IRTransformer.js.flow +377 -0
- package/core/IRValidator.js.flow +260 -0
- package/core/IRVisitor.js.flow +150 -0
- package/core/JSModuleParser.js.flow +24 -0
- package/core/RelayCompilerScope.js.flow +199 -0
- package/core/RelayFindGraphQLTags.js.flow +119 -0
- package/core/RelayGraphQLEnumsGenerator.js.flow +55 -0
- package/core/RelayIRTransforms.js.flow +138 -0
- package/core/RelayParser.js.flow +1731 -0
- package/core/RelaySourceModuleParser.js.flow +135 -0
- package/core/Schema.js.flow +2037 -0
- package/core/SchemaUtils.js.flow +120 -0
- package/core/filterContextForNode.js.flow +50 -0
- package/core/getFieldDefinition.js.flow +156 -0
- package/core/getIdentifierForArgumentValue.js.flow +49 -0
- package/core/getIdentifierForSelection.js.flow +69 -0
- package/core/getLiteralArgumentValues.js.flow +32 -0
- package/core/getNormalizationOperationName.js.flow +19 -0
- package/core/inferRootArgumentDefinitions.js.flow +323 -0
- package/index.js +1 -1
- package/index.js.flow +200 -0
- package/language/RelayLanguagePluginInterface.js.flow +283 -0
- package/language/javascript/FindGraphQLTags.js.flow +137 -0
- package/language/javascript/RelayFlowBabelFactories.js.flow +176 -0
- package/language/javascript/RelayFlowGenerator.js.flow +1099 -0
- package/language/javascript/RelayFlowTypeTransformers.js.flow +184 -0
- package/language/javascript/RelayLanguagePluginJavaScript.js.flow +34 -0
- package/language/javascript/formatGeneratedModule.js.flow +65 -0
- package/lib/bin/RelayCompilerBin.js +10 -0
- package/lib/bin/RelayCompilerMain.js +127 -130
- package/lib/codegen/CodegenDirectory.js +2 -6
- package/lib/codegen/CodegenRunner.js +37 -76
- package/lib/codegen/CodegenWatcher.js +13 -21
- package/lib/codegen/NormalizationCodeGenerator.js +131 -50
- package/lib/codegen/ReaderCodeGenerator.js +116 -49
- package/lib/codegen/RelayCodeGenerator.js +17 -6
- package/lib/codegen/RelayFileWriter.js +15 -37
- package/lib/codegen/compileRelayArtifacts.js +16 -30
- package/lib/codegen/sortObjectByKey.js +43 -0
- package/lib/codegen/writeRelayGeneratedFile.js +86 -96
- package/lib/core/ASTCache.js +3 -4
- package/lib/core/CompilerContext.js +3 -4
- package/lib/core/CompilerError.js +27 -54
- package/lib/core/GraphQLCompilerProfiler.js +8 -12
- package/lib/core/GraphQLDerivedFromMetadata.js +1 -10
- package/lib/core/GraphQLWatchmanClient.js +4 -12
- package/lib/core/IRPrinter.js +16 -21
- package/lib/core/IRTransformer.js +8 -6
- package/lib/core/IRValidator.js +1 -3
- package/lib/core/RelayCompilerScope.js +4 -4
- package/lib/core/RelayGraphQLEnumsGenerator.js +12 -15
- package/lib/core/RelayIRTransforms.js +23 -13
- package/lib/core/RelayParser.js +53 -89
- package/lib/core/RelaySourceModuleParser.js +1 -3
- package/lib/core/Schema.js +106 -77
- package/lib/core/SchemaUtils.js +15 -1
- package/lib/core/getFieldDefinition.js +12 -15
- package/lib/core/getIdentifierForSelection.js +1 -1
- package/lib/core/inferRootArgumentDefinitions.js +27 -36
- package/lib/index.js +1 -3
- package/lib/language/javascript/FindGraphQLTags.js +7 -72
- package/lib/language/javascript/RelayFlowBabelFactories.js +5 -5
- package/lib/language/javascript/RelayFlowGenerator.js +131 -108
- package/lib/language/javascript/RelayFlowTypeTransformers.js +1 -3
- package/lib/reporters/ConsoleReporter.js +1 -3
- package/lib/reporters/MultiReporter.js +1 -3
- package/lib/runner/Artifacts.js +69 -170
- package/lib/runner/BufferedFilesystem.js +32 -66
- package/lib/runner/GraphQLASTNodeGroup.js +54 -120
- package/lib/runner/GraphQLNodeMap.js +14 -19
- package/lib/runner/Sources.js +70 -85
- package/lib/runner/StrictMap.js +21 -37
- package/lib/runner/getChangedNodeNames.js +30 -62
- package/lib/transforms/ApplyFragmentArgumentTransform.js +71 -31
- package/lib/transforms/ClientExtensionsTransform.js +15 -15
- package/lib/transforms/ConnectionTransform.js +26 -38
- package/lib/transforms/DeclarativeConnectionMutationTransform.js +225 -0
- package/lib/transforms/DeferStreamTransform.js +27 -17
- package/lib/transforms/DisallowTypenameOnRoot.js +55 -0
- package/lib/transforms/FieldHandleTransform.js +7 -3
- package/lib/transforms/FilterCompilerDirectivesTransform.js +29 -0
- package/lib/transforms/FlattenTransform.js +23 -19
- package/lib/transforms/GenerateIDFieldTransform.js +56 -35
- package/lib/transforms/GenerateTypeNameTransform.js +84 -10
- package/lib/transforms/InlineDataFragmentTransform.js +9 -4
- package/lib/transforms/MaskTransform.js +17 -17
- package/lib/transforms/MatchTransform.js +114 -32
- package/lib/transforms/ReactFlightComponentTransform.js +162 -0
- package/lib/transforms/RefetchableFragmentTransform.js +21 -17
- package/lib/transforms/RelayDirectiveTransform.js +8 -3
- package/lib/transforms/RequiredFieldTransform.js +380 -0
- package/lib/transforms/SkipClientExtensionsTransform.js +8 -0
- package/lib/transforms/SkipHandleFieldTransform.js +6 -2
- package/lib/transforms/SkipRedundantNodesTransform.js +9 -2
- package/lib/transforms/SkipSplitOperationTransform.js +32 -0
- package/lib/transforms/SkipUnreachableNodeTransform.js +9 -2
- package/lib/transforms/SkipUnusedVariablesTransform.js +18 -17
- package/lib/transforms/SplitModuleImportTransform.js +2 -2
- package/lib/transforms/TestOperationTransform.js +26 -20
- package/lib/transforms/ValidateGlobalVariablesTransform.js +18 -30
- package/lib/transforms/ValidateRequiredArgumentsTransform.js +12 -15
- package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +16 -30
- package/lib/transforms/ValidateUnusedVariablesTransform.js +18 -30
- package/lib/transforms/query-generators/FetchableQueryGenerator.js +161 -0
- package/lib/transforms/query-generators/NodeQueryGenerator.js +22 -3
- package/lib/transforms/query-generators/QueryQueryGenerator.js +2 -1
- package/lib/transforms/query-generators/ViewerQueryGenerator.js +1 -0
- package/lib/transforms/query-generators/index.js +23 -6
- package/lib/transforms/query-generators/utils.js +17 -16
- package/lib/util/RelayCompilerCache.js +2 -4
- package/lib/util/argumentContainsVariables.js +37 -0
- package/lib/util/dedupeJSONStringify.js +15 -12
- package/lib/util/generateAbstractTypeRefinementKey.js +24 -0
- package/lib/util/getModuleName.js +1 -1
- package/lib/util/joinArgumentDefinitions.js +3 -1
- package/package.json +7 -7
- package/relay-compiler.js +4 -4
- package/relay-compiler.min.js +4 -4
- package/reporters/ConsoleReporter.js.flow +81 -0
- package/reporters/MultiReporter.js.flow +43 -0
- package/reporters/Reporter.js.flow +19 -0
- package/runner/Artifacts.js.flow +219 -0
- package/runner/BufferedFilesystem.js.flow +194 -0
- package/runner/GraphQLASTNodeGroup.js.flow +176 -0
- package/runner/GraphQLASTUtils.js.flow +26 -0
- package/runner/GraphQLNodeMap.js.flow +55 -0
- package/runner/Sources.js.flow +228 -0
- package/runner/StrictMap.js.flow +96 -0
- package/runner/compileArtifacts.js.flow +76 -0
- package/runner/extractAST.js.flow +100 -0
- package/runner/getChangedNodeNames.js.flow +48 -0
- package/runner/getSchemaInstance.js.flow +36 -0
- package/runner/types.js.flow +37 -0
- package/transforms/ApplyFragmentArgumentTransform.js.flow +526 -0
- package/transforms/ClientExtensionsTransform.js.flow +224 -0
- package/transforms/ConnectionTransform.js.flow +855 -0
- package/transforms/DeclarativeConnectionMutationTransform.js.flow +246 -0
- package/transforms/DeferStreamTransform.js.flow +265 -0
- package/transforms/DisallowIdAsAlias.js.flow +47 -0
- package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
- package/transforms/FieldHandleTransform.js.flow +79 -0
- package/transforms/FilterCompilerDirectivesTransform.js.flow +33 -0
- package/transforms/FilterDirectivesTransform.js.flow +45 -0
- package/transforms/FlattenTransform.js.flow +454 -0
- package/transforms/GenerateIDFieldTransform.js.flow +152 -0
- package/transforms/GenerateTypeNameTransform.js.flow +161 -0
- package/transforms/InlineDataFragmentTransform.js.flow +125 -0
- package/transforms/InlineFragmentsTransform.js.flow +71 -0
- package/transforms/MaskTransform.js.flow +126 -0
- package/transforms/MatchTransform.js.flow +589 -0
- package/transforms/ReactFlightComponentTransform.js.flow +195 -0
- package/transforms/RefetchableFragmentTransform.js.flow +272 -0
- package/transforms/RelayDirectiveTransform.js.flow +97 -0
- package/transforms/RequiredFieldTransform.js.flow +415 -0
- package/transforms/SkipClientExtensionsTransform.js.flow +54 -0
- package/transforms/SkipHandleFieldTransform.js.flow +44 -0
- package/transforms/SkipRedundantNodesTransform.js.flow +257 -0
- package/transforms/SkipSplitOperationTransform.js.flow +37 -0
- package/transforms/SkipUnreachableNodeTransform.js.flow +149 -0
- package/transforms/SkipUnusedVariablesTransform.js.flow +59 -0
- package/transforms/SplitModuleImportTransform.js.flow +98 -0
- package/transforms/TestOperationTransform.js.flow +142 -0
- package/transforms/TransformUtils.js.flow +26 -0
- package/transforms/ValidateGlobalVariablesTransform.js.flow +81 -0
- package/transforms/ValidateRequiredArgumentsTransform.js.flow +127 -0
- package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +112 -0
- package/transforms/ValidateUnusedVariablesTransform.js.flow +89 -0
- package/transforms/query-generators/FetchableQueryGenerator.js.flow +189 -0
- package/transforms/query-generators/NodeQueryGenerator.js.flow +219 -0
- package/transforms/query-generators/QueryQueryGenerator.js.flow +57 -0
- package/transforms/query-generators/ViewerQueryGenerator.js.flow +97 -0
- package/transforms/query-generators/index.js.flow +90 -0
- package/transforms/query-generators/utils.js.flow +76 -0
- package/util/CodeMarker.js.flow +79 -0
- package/util/DefaultHandleKey.js.flow +17 -0
- package/util/RelayCompilerCache.js.flow +88 -0
- package/util/Rollout.js.flow +39 -0
- package/util/TimeReporter.js.flow +79 -0
- package/util/areEqualOSS.js.flow +123 -0
- package/util/argumentContainsVariables.js.flow +38 -0
- package/util/dedupeJSONStringify.js.flow +152 -0
- package/util/generateAbstractTypeRefinementKey.js.flow +29 -0
- package/util/getDefinitionNodeHash.js.flow +25 -0
- package/util/getModuleName.js.flow +39 -0
- package/util/joinArgumentDefinitions.js.flow +105 -0
- package/util/md5.js.flow +22 -0
- package/util/murmurHash.js.flow +94 -0
- package/util/nullthrowsOSS.js.flow +25 -0
- package/util/orList.js.flow +37 -0
- package/util/partitionArray.js.flow +37 -0
@@ -0,0 +1,33 @@
|
|
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
|
+
'use strict';
|
12
|
+
|
13
|
+
const IRTransformer = require('../core/IRTransformer');
|
14
|
+
|
15
|
+
import type CompilerContext from '../core/CompilerContext';
|
16
|
+
import type {Directive} from '../core/IR';
|
17
|
+
|
18
|
+
const COMPILE_TIME_DIRECTIVES = new Set(['required']);
|
19
|
+
|
20
|
+
/**
|
21
|
+
* A transform that removes any directives that are only interpreted by the Relay compiler.
|
22
|
+
*/
|
23
|
+
function filterDirectivesTransform(context: CompilerContext): CompilerContext {
|
24
|
+
return IRTransformer.transform(context, {
|
25
|
+
Directive: (directive: Directive): ?Directive => {
|
26
|
+
return COMPILE_TIME_DIRECTIVES.has(directive.name) ? null : directive;
|
27
|
+
},
|
28
|
+
});
|
29
|
+
}
|
30
|
+
|
31
|
+
module.exports = {
|
32
|
+
transform: filterDirectivesTransform,
|
33
|
+
};
|
@@ -0,0 +1,45 @@
|
|
1
|
+
/**
|
2
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
3
|
+
*
|
4
|
+
* This source code is licensed under the MIT license found in the
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
6
|
+
*
|
7
|
+
* @flow strict-local
|
8
|
+
* @format
|
9
|
+
*/
|
10
|
+
|
11
|
+
// flowlint ambiguous-object-type:error
|
12
|
+
|
13
|
+
'use strict';
|
14
|
+
|
15
|
+
const IRTransformer = require('../core/IRTransformer');
|
16
|
+
|
17
|
+
import type CompilerContext from '../core/CompilerContext';
|
18
|
+
import type {Directive} from '../core/IR';
|
19
|
+
|
20
|
+
/**
|
21
|
+
* A transform that removes any directives that were not present in the
|
22
|
+
* server schema.
|
23
|
+
*/
|
24
|
+
function filterDirectivesTransform(context: CompilerContext): CompilerContext {
|
25
|
+
const schemaDirectives = new Set(
|
26
|
+
context
|
27
|
+
.getSchema()
|
28
|
+
.getDirectives()
|
29
|
+
.filter(directive => !directive.isClient)
|
30
|
+
.map(schemaDirective => schemaDirective.name),
|
31
|
+
);
|
32
|
+
const visitDirective = (directive: Directive): ?Directive => {
|
33
|
+
if (schemaDirectives.has(directive.name)) {
|
34
|
+
return directive;
|
35
|
+
}
|
36
|
+
return null;
|
37
|
+
};
|
38
|
+
return IRTransformer.transform(context, {
|
39
|
+
Directive: visitDirective,
|
40
|
+
});
|
41
|
+
}
|
42
|
+
|
43
|
+
module.exports = {
|
44
|
+
transform: filterDirectivesTransform,
|
45
|
+
};
|
@@ -0,0 +1,454 @@
|
|
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
|
+
* @format
|
8
|
+
* @flow
|
9
|
+
*/
|
10
|
+
|
11
|
+
// flowlint ambiguous-object-type:error
|
12
|
+
|
13
|
+
'use strict';
|
14
|
+
|
15
|
+
const IRTransformer = require('../core/IRTransformer');
|
16
|
+
|
17
|
+
const areEqual = require('../util/areEqualOSS');
|
18
|
+
const getIdentifierForSelection = require('../core/getIdentifierForSelection');
|
19
|
+
|
20
|
+
const {createCompilerError, createUserError} = require('../core/CompilerError');
|
21
|
+
|
22
|
+
import type CompilerContext from '../core/CompilerContext';
|
23
|
+
import type {
|
24
|
+
Argument,
|
25
|
+
Field,
|
26
|
+
Handle,
|
27
|
+
InlineFragment,
|
28
|
+
LinkedField,
|
29
|
+
Node,
|
30
|
+
ScalarField,
|
31
|
+
Selection,
|
32
|
+
} from '../core/IR';
|
33
|
+
import type {Schema, TypeID} from '../core/Schema';
|
34
|
+
|
35
|
+
export type FlattenOptions = {|+isForCodegen?: boolean|};
|
36
|
+
|
37
|
+
type State = {|
|
38
|
+
+isForCodegen: boolean,
|
39
|
+
parentType: ?TypeID,
|
40
|
+
|};
|
41
|
+
|
42
|
+
/**
|
43
|
+
* Transform that flattens inline fragments, fragment spreads, and conditionals.
|
44
|
+
*
|
45
|
+
* Inline fragments are inlined (replaced with their selections) when:
|
46
|
+
* - The fragment type matches the type of its parent, and its `isForCodegen`,
|
47
|
+
* or if it's for printing, there is no directive on the inline fragment.
|
48
|
+
*/
|
49
|
+
function flattenTransformImpl(
|
50
|
+
context: CompilerContext,
|
51
|
+
options?: FlattenOptions,
|
52
|
+
): CompilerContext {
|
53
|
+
const state = {
|
54
|
+
isForCodegen: !!(options && options.isForCodegen),
|
55
|
+
parentType: null,
|
56
|
+
};
|
57
|
+
const visitorFn = memoizedFlattenSelection(new Map());
|
58
|
+
return IRTransformer.transform(
|
59
|
+
context,
|
60
|
+
{
|
61
|
+
Condition: visitorFn,
|
62
|
+
Defer: visitorFn,
|
63
|
+
Fragment: visitorFn,
|
64
|
+
InlineDataFragmentSpread: visitorFn,
|
65
|
+
InlineFragment: visitorFn,
|
66
|
+
LinkedField: visitorFn,
|
67
|
+
ModuleImport: visitorFn,
|
68
|
+
Root: visitorFn,
|
69
|
+
SplitOperation: visitorFn,
|
70
|
+
},
|
71
|
+
() => state,
|
72
|
+
);
|
73
|
+
}
|
74
|
+
|
75
|
+
function memoizedFlattenSelection(cache: Map<Node, Map<?TypeID, any>>) {
|
76
|
+
return function flattenSelectionsFn<T: Node>(node: T, state: State): T {
|
77
|
+
const context: CompilerContext = this.getContext();
|
78
|
+
let nodeCache = cache.get(node);
|
79
|
+
if (nodeCache == null) {
|
80
|
+
nodeCache = new Map();
|
81
|
+
cache.set(node, nodeCache);
|
82
|
+
}
|
83
|
+
// Determine the current type.
|
84
|
+
const parentType = state.parentType;
|
85
|
+
const result = nodeCache.get(parentType);
|
86
|
+
if (result != null) {
|
87
|
+
return result;
|
88
|
+
}
|
89
|
+
|
90
|
+
const type =
|
91
|
+
node.kind === 'LinkedField' ||
|
92
|
+
node.kind === 'Fragment' ||
|
93
|
+
node.kind === 'Root' ||
|
94
|
+
node.kind === 'SplitOperation'
|
95
|
+
? node.type
|
96
|
+
: node.kind === 'InlineFragment'
|
97
|
+
? node.typeCondition
|
98
|
+
: parentType;
|
99
|
+
if (type == null) {
|
100
|
+
throw createCompilerError('FlattenTransform: Expected a parent type.', [
|
101
|
+
node.loc,
|
102
|
+
]);
|
103
|
+
}
|
104
|
+
|
105
|
+
// Flatten the selections in this node, creating a new node with flattened
|
106
|
+
// selections if possible, then deeply traverse the flattened node, while
|
107
|
+
// keeping track of the parent type.
|
108
|
+
const nextSelections = new Map();
|
109
|
+
const hasFlattened = flattenSelectionsInto(
|
110
|
+
context.getSchema(),
|
111
|
+
nextSelections,
|
112
|
+
node,
|
113
|
+
state,
|
114
|
+
type,
|
115
|
+
);
|
116
|
+
const flattenedNode = hasFlattened
|
117
|
+
? {...node, selections: Array.from(nextSelections.values())}
|
118
|
+
: node;
|
119
|
+
state.parentType = type;
|
120
|
+
const deeplyFlattenedNode = this.traverse(flattenedNode, state);
|
121
|
+
state.parentType = parentType;
|
122
|
+
nodeCache.set(parentType, deeplyFlattenedNode);
|
123
|
+
return deeplyFlattenedNode;
|
124
|
+
};
|
125
|
+
}
|
126
|
+
|
127
|
+
/**
|
128
|
+
* @private
|
129
|
+
*/
|
130
|
+
function flattenSelectionsInto(
|
131
|
+
schema: Schema,
|
132
|
+
flattenedSelections: Map<string, Selection>,
|
133
|
+
node: Node,
|
134
|
+
state: State,
|
135
|
+
type: TypeID,
|
136
|
+
): boolean {
|
137
|
+
let hasFlattened = false;
|
138
|
+
node.selections.forEach(selection => {
|
139
|
+
if (
|
140
|
+
selection.kind === 'InlineFragment' &&
|
141
|
+
shouldFlattenInlineFragment(schema, selection, state, type)
|
142
|
+
) {
|
143
|
+
hasFlattened = true;
|
144
|
+
flattenSelectionsInto(
|
145
|
+
schema,
|
146
|
+
flattenedSelections,
|
147
|
+
selection,
|
148
|
+
state,
|
149
|
+
type,
|
150
|
+
);
|
151
|
+
return;
|
152
|
+
}
|
153
|
+
const nodeIdentifier = getIdentifierForSelection(schema, selection);
|
154
|
+
const flattenedSelection = flattenedSelections.get(nodeIdentifier);
|
155
|
+
// If this selection hasn't been seen before, keep track of it.
|
156
|
+
if (!flattenedSelection) {
|
157
|
+
flattenedSelections.set(nodeIdentifier, selection);
|
158
|
+
return;
|
159
|
+
}
|
160
|
+
// Otherwise a similar selection exists which should be merged.
|
161
|
+
hasFlattened = true;
|
162
|
+
if (flattenedSelection.kind === 'InlineFragment') {
|
163
|
+
if (selection.kind !== 'InlineFragment') {
|
164
|
+
throw createCompilerError(
|
165
|
+
`FlattenTransform: Expected an InlineFragment, got a '${selection.kind}'`,
|
166
|
+
[selection.loc],
|
167
|
+
);
|
168
|
+
}
|
169
|
+
flattenedSelections.set(nodeIdentifier, {
|
170
|
+
...flattenedSelection,
|
171
|
+
selections: mergeSelections(
|
172
|
+
schema,
|
173
|
+
flattenedSelection,
|
174
|
+
selection,
|
175
|
+
state,
|
176
|
+
selection.typeCondition,
|
177
|
+
),
|
178
|
+
});
|
179
|
+
} else if (flattenedSelection.kind === 'Condition') {
|
180
|
+
if (selection.kind !== 'Condition') {
|
181
|
+
throw createCompilerError(
|
182
|
+
`FlattenTransform: Expected a Condition, got a '${selection.kind}'`,
|
183
|
+
[selection.loc],
|
184
|
+
);
|
185
|
+
}
|
186
|
+
flattenedSelections.set(nodeIdentifier, {
|
187
|
+
...flattenedSelection,
|
188
|
+
selections: mergeSelections(
|
189
|
+
schema,
|
190
|
+
flattenedSelection,
|
191
|
+
selection,
|
192
|
+
state,
|
193
|
+
type,
|
194
|
+
),
|
195
|
+
});
|
196
|
+
} else if (flattenedSelection.kind === 'ClientExtension') {
|
197
|
+
if (selection.kind !== 'ClientExtension') {
|
198
|
+
throw createCompilerError(
|
199
|
+
`FlattenTransform: Expected a ClientExtension, got a '${selection.kind}'`,
|
200
|
+
[selection.loc],
|
201
|
+
);
|
202
|
+
}
|
203
|
+
flattenedSelections.set(nodeIdentifier, {
|
204
|
+
...flattenedSelection,
|
205
|
+
selections: mergeSelections(
|
206
|
+
schema,
|
207
|
+
flattenedSelection,
|
208
|
+
selection,
|
209
|
+
state,
|
210
|
+
type,
|
211
|
+
),
|
212
|
+
});
|
213
|
+
} else if (flattenedSelection.kind === 'FragmentSpread') {
|
214
|
+
// Ignore duplicate fragment spreads.
|
215
|
+
} else if (flattenedSelection.kind === 'ModuleImport') {
|
216
|
+
if (selection.kind !== 'ModuleImport') {
|
217
|
+
throw createCompilerError(
|
218
|
+
`FlattenTransform: Expected a ModuleImport, got a '${selection.kind}'`,
|
219
|
+
[selection.loc],
|
220
|
+
);
|
221
|
+
}
|
222
|
+
if (
|
223
|
+
selection.name !== flattenedSelection.name ||
|
224
|
+
selection.module !== flattenedSelection.module ||
|
225
|
+
selection.key !== flattenedSelection.key
|
226
|
+
) {
|
227
|
+
throw createUserError(
|
228
|
+
'Found conflicting @module selections: use a unique alias on the ' +
|
229
|
+
'parent fields.',
|
230
|
+
[selection.loc, flattenedSelection.loc],
|
231
|
+
);
|
232
|
+
}
|
233
|
+
flattenedSelections.set(nodeIdentifier, {
|
234
|
+
...flattenedSelection,
|
235
|
+
selections: mergeSelections(
|
236
|
+
schema,
|
237
|
+
flattenedSelection,
|
238
|
+
selection,
|
239
|
+
state,
|
240
|
+
type,
|
241
|
+
),
|
242
|
+
});
|
243
|
+
} else if (flattenedSelection.kind === 'Defer') {
|
244
|
+
if (selection.kind !== 'Defer') {
|
245
|
+
throw createCompilerError(
|
246
|
+
`FlattenTransform: Expected a Defer, got a '${selection.kind}'`,
|
247
|
+
[selection.loc],
|
248
|
+
);
|
249
|
+
}
|
250
|
+
flattenedSelections.set(nodeIdentifier, {
|
251
|
+
kind: 'Defer',
|
252
|
+
...flattenedSelection,
|
253
|
+
selections: mergeSelections(
|
254
|
+
schema,
|
255
|
+
flattenedSelection,
|
256
|
+
selection,
|
257
|
+
state,
|
258
|
+
type,
|
259
|
+
),
|
260
|
+
});
|
261
|
+
} else if (flattenedSelection.kind === 'Stream') {
|
262
|
+
if (selection.kind !== 'Stream') {
|
263
|
+
throw createCompilerError(
|
264
|
+
`FlattenTransform: Expected a Stream, got a '${selection.kind}'`,
|
265
|
+
[selection.loc],
|
266
|
+
);
|
267
|
+
}
|
268
|
+
flattenedSelections.set(nodeIdentifier, {
|
269
|
+
kind: 'Stream',
|
270
|
+
...flattenedSelection,
|
271
|
+
selections: mergeSelections(
|
272
|
+
schema,
|
273
|
+
flattenedSelection,
|
274
|
+
selection,
|
275
|
+
state,
|
276
|
+
type,
|
277
|
+
),
|
278
|
+
});
|
279
|
+
} else if (flattenedSelection.kind === 'LinkedField') {
|
280
|
+
if (selection.kind !== 'LinkedField') {
|
281
|
+
throw createCompilerError(
|
282
|
+
`FlattenTransform: Expected a LinkedField, got a '${selection.kind}'`,
|
283
|
+
[selection.loc],
|
284
|
+
);
|
285
|
+
}
|
286
|
+
assertUniqueArgsForAlias(selection, flattenedSelection);
|
287
|
+
// NOTE: not using object spread here as this code is pretty hot
|
288
|
+
flattenedSelections.set(nodeIdentifier, {
|
289
|
+
kind: 'LinkedField',
|
290
|
+
alias: flattenedSelection.alias,
|
291
|
+
args: flattenedSelection.args,
|
292
|
+
connection: flattenedSelection.connection || selection.connection,
|
293
|
+
directives: flattenedSelection.directives,
|
294
|
+
handles: mergeHandles(flattenedSelection, selection),
|
295
|
+
loc: flattenedSelection.loc,
|
296
|
+
metadata: flattenedSelection.metadata,
|
297
|
+
name: flattenedSelection.name,
|
298
|
+
selections: mergeSelections(
|
299
|
+
schema,
|
300
|
+
flattenedSelection,
|
301
|
+
selection,
|
302
|
+
state,
|
303
|
+
selection.type,
|
304
|
+
),
|
305
|
+
type: flattenedSelection.type,
|
306
|
+
});
|
307
|
+
} else if (flattenedSelection.kind === 'ScalarField') {
|
308
|
+
if (selection.kind !== 'ScalarField') {
|
309
|
+
throw createCompilerError(
|
310
|
+
`FlattenTransform: Expected a ScalarField, got a '${selection.kind}'`,
|
311
|
+
[selection.loc],
|
312
|
+
);
|
313
|
+
}
|
314
|
+
assertUniqueArgsForAlias(selection, flattenedSelection);
|
315
|
+
if (selection.handles && selection.handles.length > 0) {
|
316
|
+
flattenedSelections.set(nodeIdentifier, {
|
317
|
+
kind: 'ScalarField',
|
318
|
+
...flattenedSelection,
|
319
|
+
handles: mergeHandles(selection, flattenedSelection),
|
320
|
+
});
|
321
|
+
}
|
322
|
+
} else if (flattenedSelection.kind === 'InlineDataFragmentSpread') {
|
323
|
+
throw createCompilerError(
|
324
|
+
'FlattenTransform: did not expect an InlineDataFragmentSpread node. ' +
|
325
|
+
'Only expecting InlineDataFragmentSpread in reader ASTs and this ' +
|
326
|
+
'transform to run only on normalization ASTs.',
|
327
|
+
[selection.loc],
|
328
|
+
);
|
329
|
+
} else {
|
330
|
+
(flattenedSelection.kind: empty);
|
331
|
+
throw createCompilerError(
|
332
|
+
`FlattenTransform: Unknown kind '${flattenedSelection.kind}'`,
|
333
|
+
);
|
334
|
+
}
|
335
|
+
});
|
336
|
+
return hasFlattened;
|
337
|
+
}
|
338
|
+
|
339
|
+
/**
|
340
|
+
* @private
|
341
|
+
*/
|
342
|
+
function mergeSelections(
|
343
|
+
schema: Schema,
|
344
|
+
nodeA: Node,
|
345
|
+
nodeB: Node,
|
346
|
+
state: State,
|
347
|
+
type: TypeID,
|
348
|
+
): $ReadOnlyArray<Selection> {
|
349
|
+
const flattenedSelections = new Map();
|
350
|
+
flattenSelectionsInto(schema, flattenedSelections, nodeA, state, type);
|
351
|
+
flattenSelectionsInto(schema, flattenedSelections, nodeB, state, type);
|
352
|
+
return Array.from(flattenedSelections.values());
|
353
|
+
}
|
354
|
+
|
355
|
+
/**
|
356
|
+
* @private
|
357
|
+
* TODO(T19327202) This is redundant with OverlappingFieldsCanBeMergedRule once
|
358
|
+
* it can be enabled.
|
359
|
+
*/
|
360
|
+
function assertUniqueArgsForAlias(field: Field, otherField: Field): void {
|
361
|
+
if (!areEqualFields(field, otherField)) {
|
362
|
+
throw createUserError(
|
363
|
+
'Expected all fields on the same parent with the name or alias ' +
|
364
|
+
`'${field.alias}' to have the same name and arguments.`,
|
365
|
+
[field.loc, otherField.loc],
|
366
|
+
);
|
367
|
+
}
|
368
|
+
}
|
369
|
+
|
370
|
+
/**
|
371
|
+
* @private
|
372
|
+
*/
|
373
|
+
function shouldFlattenInlineFragment(
|
374
|
+
schema: Schema,
|
375
|
+
fragment: InlineFragment,
|
376
|
+
state: State,
|
377
|
+
type: TypeID,
|
378
|
+
): boolean {
|
379
|
+
return (
|
380
|
+
schema.areEqualTypes(fragment.typeCondition, schema.getRawType(type)) &&
|
381
|
+
(state.isForCodegen || fragment.directives.length === 0)
|
382
|
+
);
|
383
|
+
}
|
384
|
+
|
385
|
+
/**
|
386
|
+
* @private
|
387
|
+
*
|
388
|
+
* Verify that two fields are equal in all properties other than their
|
389
|
+
* selections.
|
390
|
+
*/
|
391
|
+
function areEqualFields(thisField: Field, thatField: Field): boolean {
|
392
|
+
return (
|
393
|
+
thisField.kind === thatField.kind &&
|
394
|
+
thisField.name === thatField.name &&
|
395
|
+
thisField.alias === thatField.alias &&
|
396
|
+
areEqualArgs(thisField.args, thatField.args)
|
397
|
+
);
|
398
|
+
}
|
399
|
+
|
400
|
+
/**
|
401
|
+
* Verify that two sets of arguments are equivalent - same argument names
|
402
|
+
* and values. Notably this ignores the types of arguments and values, which
|
403
|
+
* may not always be inferred identically.
|
404
|
+
*/
|
405
|
+
function areEqualArgs(
|
406
|
+
thisArgs: $ReadOnlyArray<Argument>,
|
407
|
+
thatArgs: $ReadOnlyArray<Argument>,
|
408
|
+
): boolean {
|
409
|
+
return (
|
410
|
+
thisArgs.length === thatArgs.length &&
|
411
|
+
thisArgs.every((thisArg, index) => {
|
412
|
+
const thatArg = thatArgs[index];
|
413
|
+
return (
|
414
|
+
thisArg.name === thatArg.name &&
|
415
|
+
thisArg.value.kind === thatArg.value.kind &&
|
416
|
+
(thisArg.value: any).variableName ===
|
417
|
+
(thatArg.value: any).variableName &&
|
418
|
+
areEqual((thisArg.value: any).value, (thatArg.value: any).value)
|
419
|
+
);
|
420
|
+
})
|
421
|
+
);
|
422
|
+
}
|
423
|
+
|
424
|
+
/**
|
425
|
+
* @private
|
426
|
+
*/
|
427
|
+
function mergeHandles<T: LinkedField | ScalarField>(
|
428
|
+
nodeA: T,
|
429
|
+
nodeB: T,
|
430
|
+
): ?$ReadOnlyArray<Handle> {
|
431
|
+
if (!nodeA.handles) {
|
432
|
+
return nodeB.handles;
|
433
|
+
}
|
434
|
+
if (!nodeB.handles) {
|
435
|
+
return nodeA.handles;
|
436
|
+
}
|
437
|
+
const uniqueItems = new Map();
|
438
|
+
nodeA.handles
|
439
|
+
.concat(nodeB.handles)
|
440
|
+
.forEach(item => uniqueItems.set(item.name + item.key, item));
|
441
|
+
return Array.from(uniqueItems.values());
|
442
|
+
}
|
443
|
+
|
444
|
+
function transformWithOptions(
|
445
|
+
options: FlattenOptions,
|
446
|
+
): (context: CompilerContext) => CompilerContext {
|
447
|
+
return function flattenTransform(context: CompilerContext): CompilerContext {
|
448
|
+
return flattenTransformImpl(context, options);
|
449
|
+
};
|
450
|
+
}
|
451
|
+
|
452
|
+
module.exports = {
|
453
|
+
transformWithOptions,
|
454
|
+
};
|
@@ -0,0 +1,152 @@
|
|
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 {generateIDField} = require('../core/SchemaUtils');
|
18
|
+
const {hasUnaliasedSelection} = require('./TransformUtils');
|
19
|
+
|
20
|
+
import type CompilerContext from '../core/CompilerContext';
|
21
|
+
import type {InlineFragment, LinkedField, ScalarField} from '../core/IR';
|
22
|
+
import type {CompositeTypeID} from '../core/Schema';
|
23
|
+
|
24
|
+
const ID = 'id';
|
25
|
+
const NODE_TYPE = 'Node';
|
26
|
+
|
27
|
+
type State = {|
|
28
|
+
idFieldForType: CompositeTypeID => ScalarField,
|
29
|
+
idFragmentForType: CompositeTypeID => InlineFragment,
|
30
|
+
|};
|
31
|
+
|
32
|
+
/**
|
33
|
+
* A transform that adds an `id` field on any type that has an id field but
|
34
|
+
* where there is no unaliased `id` selection.
|
35
|
+
*/
|
36
|
+
function generateIDFieldTransform(context: CompilerContext): CompilerContext {
|
37
|
+
const schema = context.getSchema();
|
38
|
+
|
39
|
+
const typeToIDField = new Map();
|
40
|
+
function idFieldForType(type: CompositeTypeID): ScalarField {
|
41
|
+
let idField = typeToIDField.get(type);
|
42
|
+
if (idField == null) {
|
43
|
+
idField = generateIDField(schema, type);
|
44
|
+
typeToIDField.set(type, idField);
|
45
|
+
}
|
46
|
+
return idField;
|
47
|
+
}
|
48
|
+
|
49
|
+
const typeToIDFragment = new Map();
|
50
|
+
function idFragmentForType(type: CompositeTypeID): InlineFragment {
|
51
|
+
let fragment = typeToIDFragment.get(type);
|
52
|
+
if (fragment == null) {
|
53
|
+
fragment = {
|
54
|
+
kind: 'InlineFragment',
|
55
|
+
directives: [],
|
56
|
+
loc: {kind: 'Generated'},
|
57
|
+
metadata: null,
|
58
|
+
selections: [idFieldForType(type)],
|
59
|
+
typeCondition: type,
|
60
|
+
};
|
61
|
+
typeToIDFragment.set(type, fragment);
|
62
|
+
}
|
63
|
+
return fragment;
|
64
|
+
}
|
65
|
+
|
66
|
+
const state = {
|
67
|
+
idFieldForType,
|
68
|
+
idFragmentForType,
|
69
|
+
};
|
70
|
+
return IRTransformer.transform(
|
71
|
+
context,
|
72
|
+
{
|
73
|
+
LinkedField: visitLinkedField,
|
74
|
+
},
|
75
|
+
() => state,
|
76
|
+
);
|
77
|
+
}
|
78
|
+
|
79
|
+
function visitLinkedField(field: LinkedField, state: State): LinkedField {
|
80
|
+
const transformedNode = this.traverse(field, state);
|
81
|
+
|
82
|
+
// If the field already has an unaliased `id` field, do nothing
|
83
|
+
if (hasUnaliasedSelection(field, ID)) {
|
84
|
+
return transformedNode;
|
85
|
+
}
|
86
|
+
|
87
|
+
const context: CompilerContext = this.getContext();
|
88
|
+
const schema = context.getSchema();
|
89
|
+
const unmodifiedType = schema.assertCompositeType(
|
90
|
+
schema.getRawType(field.type),
|
91
|
+
);
|
92
|
+
|
93
|
+
// If the field type has an `id` subfield add an `id` selection
|
94
|
+
if (
|
95
|
+
schema.canHaveSelections(unmodifiedType) &&
|
96
|
+
schema.hasId(unmodifiedType)
|
97
|
+
) {
|
98
|
+
return {
|
99
|
+
...transformedNode,
|
100
|
+
selections: [
|
101
|
+
...transformedNode.selections,
|
102
|
+
state.idFieldForType(unmodifiedType),
|
103
|
+
],
|
104
|
+
};
|
105
|
+
}
|
106
|
+
|
107
|
+
// If the field type is abstract, then generate a `... on Node { id }`
|
108
|
+
// fragment if *any* concrete type implements Node. Then generate a
|
109
|
+
// `... on PossibleType { id }` for every concrete type that does *not*
|
110
|
+
// implement `Node`
|
111
|
+
const nodeType = schema.getTypeFromString(NODE_TYPE);
|
112
|
+
if (!nodeType) {
|
113
|
+
return transformedNode;
|
114
|
+
}
|
115
|
+
|
116
|
+
const nodeInterface = schema.assertInterfaceType(nodeType);
|
117
|
+
|
118
|
+
if (schema.isAbstractType(unmodifiedType)) {
|
119
|
+
const selections = [...transformedNode.selections];
|
120
|
+
if (schema.mayImplement(unmodifiedType, nodeInterface)) {
|
121
|
+
selections.push(state.idFragmentForType(nodeInterface));
|
122
|
+
}
|
123
|
+
Array.from(
|
124
|
+
schema
|
125
|
+
.getPossibleTypes(schema.assertAbstractType(unmodifiedType))
|
126
|
+
.values(),
|
127
|
+
)
|
128
|
+
.filter(
|
129
|
+
concreteType =>
|
130
|
+
!schema.implementsInterface(
|
131
|
+
schema.assertCompositeType(concreteType),
|
132
|
+
nodeInterface,
|
133
|
+
) && schema.hasId(concreteType),
|
134
|
+
)
|
135
|
+
.sort((a, b) =>
|
136
|
+
schema.getTypeString(a) < schema.getTypeString(b) ? -1 : 1,
|
137
|
+
)
|
138
|
+
.forEach(concreteType => {
|
139
|
+
selections.push(state.idFragmentForType(concreteType));
|
140
|
+
});
|
141
|
+
return {
|
142
|
+
...transformedNode,
|
143
|
+
selections,
|
144
|
+
};
|
145
|
+
}
|
146
|
+
|
147
|
+
return transformedNode;
|
148
|
+
}
|
149
|
+
|
150
|
+
module.exports = {
|
151
|
+
transform: generateIDFieldTransform,
|
152
|
+
};
|