relay-compiler 0.0.0-main-8ff54d69 → 0.0.0-main-38f1c96e
Sign up to get free protection for your applications and to get access to all the features.
- package/cli.js +23 -0
- package/index.js +19 -3
- package/linux-x64/relay +0 -0
- package/macos-x64/relay +0 -0
- package/package.json +2 -24
- package/win-x64/relay.exe +0 -0
- package/bin/RelayCompilerBin.js.flow +0 -168
- package/bin/RelayCompilerMain.js.flow +0 -515
- package/bin/__fixtures__/plugin-module.js.flow +0 -17
- package/bin/relay-compiler +0 -19068
- package/codegen/CodegenDirectory.js.flow +0 -372
- package/codegen/CodegenRunner.js.flow +0 -424
- package/codegen/CodegenTypes.js.flow +0 -28
- package/codegen/CodegenWatcher.js.flow +0 -250
- package/codegen/NormalizationCodeGenerator.js.flow +0 -566
- package/codegen/ReaderCodeGenerator.js.flow +0 -510
- package/codegen/RelayCodeGenerator.js.flow +0 -85
- package/codegen/RelayFileWriter.js.flow +0 -365
- package/codegen/SourceControl.js.flow +0 -58
- package/codegen/compileRelayArtifacts.js.flow +0 -181
- package/codegen/createPrintRequireModuleDependency.js.flow +0 -19
- package/codegen/sortObjectByKey.js.flow +0 -25
- package/codegen/writeRelayGeneratedFile.js.flow +0 -235
- package/core/ASTCache.js.flow +0 -73
- package/core/ASTConvert.js.flow +0 -230
- package/core/CompilerContext.js.flow +0 -189
- package/core/CompilerError.js.flow +0 -255
- package/core/DotGraphQLParser.js.flow +0 -37
- package/core/GraphQLCompilerProfiler.js.flow +0 -341
- package/core/GraphQLDerivedFromMetadata.js.flow +0 -36
- package/core/GraphQLWatchmanClient.js.flow +0 -109
- package/core/IR.js.flow +0 -326
- package/core/IRPrinter.js.flow +0 -472
- package/core/IRTransformer.js.flow +0 -376
- package/core/IRValidator.js.flow +0 -259
- package/core/IRVisitor.js.flow +0 -150
- package/core/JSModuleParser.js.flow +0 -24
- package/core/RelayCompilerScope.js.flow +0 -199
- package/core/RelayFindGraphQLTags.js.flow +0 -118
- package/core/RelayGraphQLEnumsGenerator.js.flow +0 -55
- package/core/RelayIRTransforms.js.flow +0 -138
- package/core/RelayParser.js.flow +0 -1741
- package/core/RelaySourceModuleParser.js.flow +0 -133
- package/core/Schema.js.flow +0 -2035
- package/core/SchemaUtils.js.flow +0 -120
- package/core/filterContextForNode.js.flow +0 -49
- package/core/getFieldDefinition.js.flow +0 -156
- package/core/getIdentifierForArgumentValue.js.flow +0 -49
- package/core/getIdentifierForSelection.js.flow +0 -68
- package/core/getLiteralArgumentValues.js.flow +0 -32
- package/core/getNormalizationOperationName.js.flow +0 -19
- package/core/inferRootArgumentDefinitions.js.flow +0 -322
- package/index.js.flow +0 -198
- package/language/RelayLanguagePluginInterface.js.flow +0 -283
- package/language/javascript/FindGraphQLTags.js.flow +0 -136
- package/language/javascript/RelayFlowBabelFactories.js.flow +0 -176
- package/language/javascript/RelayFlowGenerator.js.flow +0 -1096
- package/language/javascript/RelayFlowTypeTransformers.js.flow +0 -181
- package/language/javascript/RelayLanguagePluginJavaScript.js.flow +0 -33
- package/language/javascript/formatGeneratedModule.js.flow +0 -65
- package/lib/bin/RelayCompilerBin.js +0 -143
- package/lib/bin/RelayCompilerMain.js +0 -488
- package/lib/bin/__fixtures__/plugin-module.js +0 -16
- package/lib/codegen/CodegenDirectory.js +0 -335
- package/lib/codegen/CodegenRunner.js +0 -433
- package/lib/codegen/CodegenTypes.js +0 -11
- package/lib/codegen/CodegenWatcher.js +0 -271
- package/lib/codegen/NormalizationCodeGenerator.js +0 -487
- package/lib/codegen/ReaderCodeGenerator.js +0 -473
- package/lib/codegen/RelayCodeGenerator.js +0 -75
- package/lib/codegen/RelayFileWriter.js +0 -270
- package/lib/codegen/SourceControl.js +0 -60
- package/lib/codegen/compileRelayArtifacts.js +0 -157
- package/lib/codegen/createPrintRequireModuleDependency.js +0 -19
- package/lib/codegen/sortObjectByKey.js +0 -41
- package/lib/codegen/writeRelayGeneratedFile.js +0 -206
- package/lib/core/ASTCache.js +0 -70
- package/lib/core/ASTConvert.js +0 -198
- package/lib/core/CompilerContext.js +0 -165
- package/lib/core/CompilerError.js +0 -252
- package/lib/core/DotGraphQLParser.js +0 -40
- package/lib/core/GraphQLCompilerProfiler.js +0 -299
- package/lib/core/GraphQLDerivedFromMetadata.js +0 -31
- package/lib/core/GraphQLWatchmanClient.js +0 -150
- package/lib/core/IR.js +0 -11
- package/lib/core/IRPrinter.js +0 -388
- package/lib/core/IRTransformer.js +0 -345
- package/lib/core/IRValidator.js +0 -226
- package/lib/core/IRVisitor.js +0 -45
- package/lib/core/JSModuleParser.js +0 -18
- package/lib/core/RelayCompilerScope.js +0 -183
- package/lib/core/RelayFindGraphQLTags.js +0 -79
- package/lib/core/RelayGraphQLEnumsGenerator.js +0 -50
- package/lib/core/RelayIRTransforms.js +0 -109
- package/lib/core/RelayParser.js +0 -1381
- package/lib/core/RelaySourceModuleParser.js +0 -104
- package/lib/core/Schema.js +0 -1877
- package/lib/core/SchemaUtils.js +0 -98
- package/lib/core/filterContextForNode.js +0 -50
- package/lib/core/getFieldDefinition.js +0 -145
- package/lib/core/getIdentifierForArgumentValue.js +0 -54
- package/lib/core/getIdentifierForSelection.js +0 -49
- package/lib/core/getLiteralArgumentValues.js +0 -26
- package/lib/core/getNormalizationOperationName.js +0 -17
- package/lib/core/inferRootArgumentDefinitions.js +0 -351
- package/lib/index.js +0 -178
- package/lib/language/RelayLanguagePluginInterface.js +0 -26
- package/lib/language/javascript/FindGraphQLTags.js +0 -126
- package/lib/language/javascript/RelayFlowBabelFactories.js +0 -160
- package/lib/language/javascript/RelayFlowGenerator.js +0 -856
- package/lib/language/javascript/RelayFlowTypeTransformers.js +0 -119
- package/lib/language/javascript/RelayLanguagePluginJavaScript.js +0 -30
- package/lib/language/javascript/formatGeneratedModule.js +0 -36
- package/lib/reporters/ConsoleReporter.js +0 -61
- package/lib/reporters/MultiReporter.js +0 -45
- package/lib/reporters/Reporter.js +0 -11
- package/lib/runner/Artifacts.js +0 -323
- package/lib/runner/BufferedFilesystem.js +0 -262
- package/lib/runner/GraphQLASTNodeGroup.js +0 -256
- package/lib/runner/GraphQLASTUtils.js +0 -23
- package/lib/runner/GraphQLNodeMap.js +0 -81
- package/lib/runner/Sources.js +0 -271
- package/lib/runner/StrictMap.js +0 -134
- package/lib/runner/compileArtifacts.js +0 -39
- package/lib/runner/extractAST.js +0 -77
- package/lib/runner/getChangedNodeNames.js +0 -82
- package/lib/runner/getSchemaInstance.js +0 -30
- package/lib/runner/types.js +0 -12
- package/lib/test-utils/TestSchema.js +0 -27
- package/lib/test-utils/parseGraphQLText.js +0 -30
- package/lib/transforms/ApplyFragmentArgumentTransform.js +0 -393
- package/lib/transforms/ClientExtensionsTransform.js +0 -221
- package/lib/transforms/ConnectionTransform.js +0 -639
- package/lib/transforms/DeclarativeConnectionMutationTransform.js +0 -218
- package/lib/transforms/DeferStreamTransform.js +0 -246
- package/lib/transforms/DisallowIdAsAlias.js +0 -40
- package/lib/transforms/DisallowTypenameOnRoot.js +0 -53
- package/lib/transforms/FieldHandleTransform.js +0 -79
- package/lib/transforms/FilterCompilerDirectivesTransform.js +0 -29
- package/lib/transforms/FilterDirectivesTransform.js +0 -42
- package/lib/transforms/FlattenTransform.js +0 -306
- package/lib/transforms/GenerateIDFieldTransform.js +0 -135
- package/lib/transforms/GenerateTypeNameTransform.js +0 -149
- package/lib/transforms/InlineDataFragmentTransform.js +0 -100
- package/lib/transforms/InlineFragmentsTransform.js +0 -61
- package/lib/transforms/MaskTransform.js +0 -117
- package/lib/transforms/MatchTransform.js +0 -434
- package/lib/transforms/ReactFlightComponentTransform.js +0 -158
- package/lib/transforms/RefetchableFragmentTransform.js +0 -249
- package/lib/transforms/RelayDirectiveTransform.js +0 -83
- package/lib/transforms/RequiredFieldTransform.js +0 -369
- package/lib/transforms/SkipClientExtensionsTransform.js +0 -46
- package/lib/transforms/SkipHandleFieldTransform.js +0 -45
- package/lib/transforms/SkipRedundantNodesTransform.js +0 -261
- package/lib/transforms/SkipSplitOperationTransform.js +0 -32
- package/lib/transforms/SkipUnreachableNodeTransform.js +0 -158
- package/lib/transforms/SkipUnusedVariablesTransform.js +0 -75
- package/lib/transforms/SplitModuleImportTransform.js +0 -82
- package/lib/transforms/TestOperationTransform.js +0 -144
- package/lib/transforms/TransformUtils.js +0 -21
- package/lib/transforms/ValidateGlobalVariablesTransform.js +0 -92
- package/lib/transforms/ValidateRequiredArgumentsTransform.js +0 -114
- package/lib/transforms/ValidateServerOnlyDirectivesTransform.js +0 -108
- package/lib/transforms/ValidateUnusedVariablesTransform.js +0 -96
- package/lib/transforms/query-generators/FetchableQueryGenerator.js +0 -157
- package/lib/transforms/query-generators/NodeQueryGenerator.js +0 -166
- package/lib/transforms/query-generators/QueryQueryGenerator.js +0 -48
- package/lib/transforms/query-generators/ViewerQueryGenerator.js +0 -77
- package/lib/transforms/query-generators/index.js +0 -60
- package/lib/transforms/query-generators/utils.js +0 -92
- package/lib/util/CodeMarker.js +0 -80
- package/lib/util/DefaultHandleKey.js +0 -15
- package/lib/util/RelayCompilerCache.js +0 -97
- package/lib/util/Rollout.js +0 -40
- package/lib/util/TimeReporter.js +0 -83
- package/lib/util/areEqualArgValues.js +0 -135
- package/lib/util/argumentContainsVariables.js +0 -37
- package/lib/util/dedupeJSONStringify.js +0 -160
- package/lib/util/generateAbstractTypeRefinementKey.js +0 -24
- package/lib/util/getDefinitionNodeHash.js +0 -22
- package/lib/util/getModuleName.js +0 -32
- package/lib/util/joinArgumentDefinitions.js +0 -67
- package/lib/util/md5.js +0 -17
- package/lib/util/murmurHash.js +0 -86
- package/lib/util/nullthrowsOSS.js +0 -23
- package/lib/util/orList.js +0 -36
- package/lib/util/partitionArray.js +0 -35
- package/relay-compiler.js +0 -17
- package/relay-compiler.min.js +0 -22
- package/reporters/ConsoleReporter.js.flow +0 -81
- package/reporters/MultiReporter.js.flow +0 -43
- package/reporters/Reporter.js.flow +0 -19
- package/runner/Artifacts.js.flow +0 -215
- package/runner/BufferedFilesystem.js.flow +0 -194
- package/runner/GraphQLASTNodeGroup.js.flow +0 -174
- package/runner/GraphQLASTUtils.js.flow +0 -26
- package/runner/GraphQLNodeMap.js.flow +0 -55
- package/runner/Sources.js.flow +0 -227
- package/runner/StrictMap.js.flow +0 -96
- package/runner/compileArtifacts.js.flow +0 -75
- package/runner/extractAST.js.flow +0 -98
- package/runner/getChangedNodeNames.js.flow +0 -48
- package/runner/getSchemaInstance.js.flow +0 -36
- package/runner/types.js.flow +0 -37
- package/test-utils/TestSchema.js.flow +0 -30
- package/test-utils/parseGraphQLText.js.flow +0 -41
- package/transforms/ApplyFragmentArgumentTransform.js.flow +0 -524
- package/transforms/ClientExtensionsTransform.js.flow +0 -224
- package/transforms/ConnectionTransform.js.flow +0 -850
- package/transforms/DeclarativeConnectionMutationTransform.js.flow +0 -245
- package/transforms/DeferStreamTransform.js.flow +0 -263
- package/transforms/DisallowIdAsAlias.js.flow +0 -46
- package/transforms/DisallowTypenameOnRoot.js.flow +0 -44
- package/transforms/FieldHandleTransform.js.flow +0 -77
- package/transforms/FilterCompilerDirectivesTransform.js.flow +0 -33
- package/transforms/FilterDirectivesTransform.js.flow +0 -45
- package/transforms/FlattenTransform.js.flow +0 -458
- package/transforms/GenerateIDFieldTransform.js.flow +0 -151
- package/transforms/GenerateTypeNameTransform.js.flow +0 -159
- package/transforms/InlineDataFragmentTransform.js.flow +0 -123
- package/transforms/InlineFragmentsTransform.js.flow +0 -70
- package/transforms/MaskTransform.js.flow +0 -124
- package/transforms/MatchTransform.js.flow +0 -587
- package/transforms/ReactFlightComponentTransform.js.flow +0 -207
- package/transforms/RefetchableFragmentTransform.js.flow +0 -266
- package/transforms/RelayDirectiveTransform.js.flow +0 -96
- package/transforms/RequiredFieldTransform.js.flow +0 -413
- package/transforms/SkipClientExtensionsTransform.js.flow +0 -54
- package/transforms/SkipHandleFieldTransform.js.flow +0 -44
- package/transforms/SkipRedundantNodesTransform.js.flow +0 -277
- package/transforms/SkipSplitOperationTransform.js.flow +0 -37
- package/transforms/SkipUnreachableNodeTransform.js.flow +0 -148
- package/transforms/SkipUnusedVariablesTransform.js.flow +0 -59
- package/transforms/SplitModuleImportTransform.js.flow +0 -97
- package/transforms/TestOperationTransform.js.flow +0 -142
- package/transforms/TransformUtils.js.flow +0 -26
- package/transforms/ValidateGlobalVariablesTransform.js.flow +0 -80
- package/transforms/ValidateRequiredArgumentsTransform.js.flow +0 -130
- package/transforms/ValidateServerOnlyDirectivesTransform.js.flow +0 -128
- package/transforms/ValidateUnusedVariablesTransform.js.flow +0 -88
- package/transforms/query-generators/FetchableQueryGenerator.js.flow +0 -188
- package/transforms/query-generators/NodeQueryGenerator.js.flow +0 -217
- package/transforms/query-generators/QueryQueryGenerator.js.flow +0 -57
- package/transforms/query-generators/ViewerQueryGenerator.js.flow +0 -97
- package/transforms/query-generators/index.js.flow +0 -89
- package/transforms/query-generators/utils.js.flow +0 -76
- package/util/CodeMarker.js.flow +0 -79
- package/util/DefaultHandleKey.js.flow +0 -17
- package/util/RelayCompilerCache.js.flow +0 -86
- package/util/Rollout.js.flow +0 -39
- package/util/TimeReporter.js.flow +0 -79
- package/util/areEqualArgValues.js.flow +0 -126
- package/util/argumentContainsVariables.js.flow +0 -38
- package/util/dedupeJSONStringify.js.flow +0 -156
- package/util/generateAbstractTypeRefinementKey.js.flow +0 -29
- package/util/getDefinitionNodeHash.js.flow +0 -24
- package/util/getModuleName.js.flow +0 -39
- package/util/joinArgumentDefinitions.js.flow +0 -105
- package/util/md5.js.flow +0 -19
- package/util/murmurHash.js.flow +0 -94
- package/util/nullthrowsOSS.js.flow +0 -25
- package/util/orList.js.flow +0 -37
- package/util/partitionArray.js.flow +0 -37
@@ -1,566 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Copyright (c) Facebook, Inc. and its affiliates.
|
3
|
-
*
|
4
|
-
* This source code is licensed under the MIT license found in the
|
5
|
-
* LICENSE file in the root directory of this source tree.
|
6
|
-
*
|
7
|
-
* @flow strict-local
|
8
|
-
* @format
|
9
|
-
*/
|
10
|
-
|
11
|
-
// flowlint ambiguous-object-type:error
|
12
|
-
|
13
|
-
'use strict';
|
14
|
-
import type {ScalarField} from '../core/IR';
|
15
|
-
|
16
|
-
import type {
|
17
|
-
Argument,
|
18
|
-
ArgumentValue,
|
19
|
-
ClientExtension,
|
20
|
-
Condition,
|
21
|
-
Defer,
|
22
|
-
InlineFragment,
|
23
|
-
LinkedField,
|
24
|
-
LocalArgumentDefinition,
|
25
|
-
Metadata,
|
26
|
-
ModuleImport,
|
27
|
-
Root,
|
28
|
-
Selection,
|
29
|
-
SplitOperation,
|
30
|
-
Stream,
|
31
|
-
} from '../core/IR';
|
32
|
-
import type {Schema, TypeID} from '../core/Schema';
|
33
|
-
import type {
|
34
|
-
NormalizationArgument,
|
35
|
-
NormalizationDefer,
|
36
|
-
NormalizationField,
|
37
|
-
NormalizationLinkedField,
|
38
|
-
NormalizationLinkedHandle,
|
39
|
-
NormalizationLocalArgumentDefinition,
|
40
|
-
NormalizationModuleImport,
|
41
|
-
NormalizationOperation,
|
42
|
-
NormalizationSelection,
|
43
|
-
NormalizationSplitOperation,
|
44
|
-
NormalizationStream,
|
45
|
-
NormalizationTypeDiscriminator,
|
46
|
-
} from 'relay-runtime';
|
47
|
-
|
48
|
-
const {createCompilerError, createUserError} = require('../core/CompilerError');
|
49
|
-
const argumentContainsVariables = require('../util/argumentContainsVariables');
|
50
|
-
const generateAbstractTypeRefinementKey = require('../util/generateAbstractTypeRefinementKey');
|
51
|
-
const partitionArray = require('../util/partitionArray');
|
52
|
-
const sortObjectByKey = require('./sortObjectByKey');
|
53
|
-
const {getStorageKey, stableCopy} = require('relay-runtime');
|
54
|
-
|
55
|
-
/**
|
56
|
-
* @public
|
57
|
-
*
|
58
|
-
* Converts an IR node into a plain JS object representation that can be
|
59
|
-
* used at runtime.
|
60
|
-
*/
|
61
|
-
declare function generate(schema: Schema, node: Root): NormalizationOperation;
|
62
|
-
declare function generate(
|
63
|
-
schema: Schema,
|
64
|
-
node: SplitOperation,
|
65
|
-
): NormalizationSplitOperation;
|
66
|
-
function generate(
|
67
|
-
schema: Schema,
|
68
|
-
node: Root | SplitOperation,
|
69
|
-
): NormalizationOperation | NormalizationSplitOperation {
|
70
|
-
switch (node.kind) {
|
71
|
-
case 'Root':
|
72
|
-
return generateRoot(schema, node);
|
73
|
-
case 'SplitOperation':
|
74
|
-
return generateSplitOperation(schema, node);
|
75
|
-
default:
|
76
|
-
throw createCompilerError(
|
77
|
-
`NormalizationCodeGenerator: Unsupported AST kind '${node.kind}'.`,
|
78
|
-
[node.loc],
|
79
|
-
);
|
80
|
-
}
|
81
|
-
}
|
82
|
-
|
83
|
-
function generateRoot(schema: Schema, node: Root): NormalizationOperation {
|
84
|
-
return {
|
85
|
-
argumentDefinitions: generateArgumentDefinitions(
|
86
|
-
schema,
|
87
|
-
node.argumentDefinitions,
|
88
|
-
),
|
89
|
-
kind: 'Operation',
|
90
|
-
name: node.name,
|
91
|
-
selections: generateSelections(schema, node.selections),
|
92
|
-
};
|
93
|
-
}
|
94
|
-
|
95
|
-
function generateSplitOperation(
|
96
|
-
schema: Schema,
|
97
|
-
node: SplitOperation,
|
98
|
-
): NormalizationSplitOperation {
|
99
|
-
return {
|
100
|
-
kind: 'SplitOperation',
|
101
|
-
metadata: sortObjectByKey(node.metadata),
|
102
|
-
name: node.name,
|
103
|
-
selections: generateSelections(schema, node.selections),
|
104
|
-
};
|
105
|
-
}
|
106
|
-
|
107
|
-
function generateSelections(
|
108
|
-
schema: Schema,
|
109
|
-
selections: $ReadOnlyArray<Selection>,
|
110
|
-
): $ReadOnlyArray<NormalizationSelection> {
|
111
|
-
const normalizationSelections: Array<NormalizationSelection> = [];
|
112
|
-
selections.forEach(selection => {
|
113
|
-
switch (selection.kind) {
|
114
|
-
case 'Condition':
|
115
|
-
normalizationSelections.push(generateCondition(schema, selection));
|
116
|
-
break;
|
117
|
-
case 'ClientExtension':
|
118
|
-
normalizationSelections.push(
|
119
|
-
generateClientExtension(schema, selection),
|
120
|
-
);
|
121
|
-
break;
|
122
|
-
case 'ScalarField':
|
123
|
-
// NOTE: Inline fragments in normalization ast have the abstractKey
|
124
|
-
// but we skip the corresponding ScalarField for the type discriminator
|
125
|
-
// selection, since it's guaranteed to be a duplicate of a parent __typename
|
126
|
-
// selection.
|
127
|
-
const abstractKey = selection.metadata?.abstractKey;
|
128
|
-
if (typeof abstractKey === 'string') {
|
129
|
-
normalizationSelections.push(generateTypeDiscriminator(abstractKey));
|
130
|
-
} else {
|
131
|
-
normalizationSelections.push(...generateScalarField(selection));
|
132
|
-
}
|
133
|
-
break;
|
134
|
-
case 'ModuleImport':
|
135
|
-
normalizationSelections.push(generateModuleImport(selection));
|
136
|
-
break;
|
137
|
-
case 'InlineFragment':
|
138
|
-
normalizationSelections.push(generateInlineFragment(schema, selection));
|
139
|
-
break;
|
140
|
-
case 'LinkedField':
|
141
|
-
normalizationSelections.push(...generateLinkedField(schema, selection));
|
142
|
-
break;
|
143
|
-
case 'Defer':
|
144
|
-
normalizationSelections.push(generateDefer(schema, selection));
|
145
|
-
break;
|
146
|
-
case 'Stream':
|
147
|
-
normalizationSelections.push(generateStream(schema, selection));
|
148
|
-
break;
|
149
|
-
case 'InlineDataFragmentSpread':
|
150
|
-
case 'FragmentSpread':
|
151
|
-
throw new createCompilerError(
|
152
|
-
`NormalizationCodeGenerator: Unexpected IR node ${selection.kind}.`,
|
153
|
-
[selection.loc],
|
154
|
-
);
|
155
|
-
default:
|
156
|
-
(selection: empty);
|
157
|
-
throw new Error();
|
158
|
-
}
|
159
|
-
});
|
160
|
-
return normalizationSelections;
|
161
|
-
}
|
162
|
-
|
163
|
-
function generateArgumentDefinitions(
|
164
|
-
schema: Schema,
|
165
|
-
nodes: $ReadOnlyArray<LocalArgumentDefinition>,
|
166
|
-
): $ReadOnlyArray<NormalizationLocalArgumentDefinition> {
|
167
|
-
return nodes.map(node => {
|
168
|
-
return {
|
169
|
-
defaultValue: stableCopy(node.defaultValue),
|
170
|
-
kind: 'LocalArgument',
|
171
|
-
name: node.name,
|
172
|
-
};
|
173
|
-
});
|
174
|
-
}
|
175
|
-
|
176
|
-
function generateClientExtension(
|
177
|
-
schema: Schema,
|
178
|
-
node: ClientExtension,
|
179
|
-
): NormalizationSelection {
|
180
|
-
return {
|
181
|
-
kind: 'ClientExtension',
|
182
|
-
selections: generateSelections(schema, node.selections),
|
183
|
-
};
|
184
|
-
}
|
185
|
-
|
186
|
-
function generateCondition(
|
187
|
-
schema: Schema,
|
188
|
-
node: Condition,
|
189
|
-
): NormalizationSelection {
|
190
|
-
if (node.condition.kind !== 'Variable') {
|
191
|
-
throw createCompilerError(
|
192
|
-
"NormalizationCodeGenerator: Expected 'Condition' with static " +
|
193
|
-
'value to be pruned or inlined',
|
194
|
-
[node.condition.loc],
|
195
|
-
);
|
196
|
-
}
|
197
|
-
return {
|
198
|
-
condition: node.condition.variableName,
|
199
|
-
kind: 'Condition',
|
200
|
-
passingValue: node.passingValue,
|
201
|
-
selections: generateSelections(schema, node.selections),
|
202
|
-
};
|
203
|
-
}
|
204
|
-
|
205
|
-
function generateDefer(schema: Schema, node: Defer): NormalizationDefer {
|
206
|
-
if (
|
207
|
-
!(
|
208
|
-
node.if == null ||
|
209
|
-
node.if.kind === 'Variable' ||
|
210
|
-
(node.if.kind === 'Literal' && node.if.value === true)
|
211
|
-
)
|
212
|
-
) {
|
213
|
-
throw createCompilerError(
|
214
|
-
'NormalizationCodeGenerator: Expected @defer `if` condition to be ' +
|
215
|
-
'a variable, unspecified, or the literal `true`.',
|
216
|
-
[node.if?.loc ?? node.loc],
|
217
|
-
);
|
218
|
-
}
|
219
|
-
return {
|
220
|
-
if:
|
221
|
-
node.if != null && node.if.kind === 'Variable'
|
222
|
-
? node.if.variableName
|
223
|
-
: null,
|
224
|
-
kind: 'Defer',
|
225
|
-
label: node.label,
|
226
|
-
selections: generateSelections(schema, node.selections),
|
227
|
-
};
|
228
|
-
}
|
229
|
-
|
230
|
-
function generateInlineFragment(
|
231
|
-
schema: Schema,
|
232
|
-
node: InlineFragment,
|
233
|
-
): NormalizationSelection {
|
234
|
-
const rawType = schema.getRawType(node.typeCondition);
|
235
|
-
const isAbstractType = schema.isAbstractType(rawType);
|
236
|
-
const abstractKey = isAbstractType
|
237
|
-
? generateAbstractTypeRefinementKey(schema, rawType)
|
238
|
-
: null;
|
239
|
-
let selections = generateSelections(schema, node.selections);
|
240
|
-
|
241
|
-
if (isAbstractType) {
|
242
|
-
// Maintain a few invariants:
|
243
|
-
// - InlineFragment (and `selections` arrays generally) cannot be empty
|
244
|
-
// - Don't emit a TypeDiscriminator under an InlineFragment unless it has
|
245
|
-
// a different abstractKey
|
246
|
-
// This means we have to handle two cases:
|
247
|
-
// - The inline fragment only contains a TypeDiscriminator with the same
|
248
|
-
// abstractKey: replace the Fragment w the Discriminator
|
249
|
-
// - The inline fragment contains other selections: return all the selections
|
250
|
-
// minus any Discriminators w the same key
|
251
|
-
const [discriminators, otherSelections] = partitionArray(
|
252
|
-
selections,
|
253
|
-
selection =>
|
254
|
-
selection.kind === 'TypeDiscriminator' &&
|
255
|
-
selection.abstractKey === abstractKey,
|
256
|
-
);
|
257
|
-
const discriminator = discriminators[0];
|
258
|
-
if (discriminator != null && otherSelections.length === 0) {
|
259
|
-
return discriminator;
|
260
|
-
} else {
|
261
|
-
selections = otherSelections;
|
262
|
-
}
|
263
|
-
}
|
264
|
-
|
265
|
-
return {
|
266
|
-
kind: 'InlineFragment',
|
267
|
-
selections,
|
268
|
-
type: schema.getTypeString(rawType),
|
269
|
-
abstractKey,
|
270
|
-
};
|
271
|
-
}
|
272
|
-
|
273
|
-
function generateLinkedField(
|
274
|
-
schema: Schema,
|
275
|
-
node: LinkedField,
|
276
|
-
): $ReadOnlyArray<NormalizationSelection> {
|
277
|
-
// Note: it is important that the arguments of this field be sorted to
|
278
|
-
// ensure stable generation of storage keys for equivalent arguments
|
279
|
-
// which may have originally appeared in different orders across an app.
|
280
|
-
const handles =
|
281
|
-
(node.handles &&
|
282
|
-
node.handles.map(handle => {
|
283
|
-
let handleNode: NormalizationLinkedHandle = {
|
284
|
-
alias: node.alias === node.name ? null : node.alias,
|
285
|
-
args: generateArgs(node.args),
|
286
|
-
filters: handle.filters,
|
287
|
-
handle: handle.name,
|
288
|
-
key: handle.key,
|
289
|
-
kind: 'LinkedHandle',
|
290
|
-
name: node.name,
|
291
|
-
};
|
292
|
-
// NOTE: this intentionally adds a dynamic key in order to avoid
|
293
|
-
// triggering updates to existing queries that do not use dynamic
|
294
|
-
// keys.
|
295
|
-
if (handle.dynamicKey != null) {
|
296
|
-
const dynamicKeyArgName = '__dynamicKey';
|
297
|
-
handleNode = {
|
298
|
-
...handleNode,
|
299
|
-
dynamicKey: {
|
300
|
-
kind: 'Variable',
|
301
|
-
name: dynamicKeyArgName,
|
302
|
-
variableName: handle.dynamicKey.variableName,
|
303
|
-
},
|
304
|
-
};
|
305
|
-
}
|
306
|
-
if (handle.handleArgs != null) {
|
307
|
-
const handleArgs = generateArgs(handle.handleArgs);
|
308
|
-
if (handleArgs != null) {
|
309
|
-
handleNode = {
|
310
|
-
...handleNode,
|
311
|
-
handleArgs,
|
312
|
-
};
|
313
|
-
}
|
314
|
-
}
|
315
|
-
return handleNode;
|
316
|
-
})) ||
|
317
|
-
[];
|
318
|
-
const type = schema.getRawType(node.type);
|
319
|
-
let field: NormalizationLinkedField = {
|
320
|
-
alias: node.alias === node.name ? null : node.alias,
|
321
|
-
args: generateArgs(node.args),
|
322
|
-
concreteType: !schema.isAbstractType(type)
|
323
|
-
? schema.getTypeString(type)
|
324
|
-
: null,
|
325
|
-
kind: 'LinkedField',
|
326
|
-
name: node.name,
|
327
|
-
plural: isPlural(schema, node.type),
|
328
|
-
selections: generateSelections(schema, node.selections),
|
329
|
-
storageKey: null,
|
330
|
-
};
|
331
|
-
// Precompute storageKey if possible
|
332
|
-
const storageKey = getStaticStorageKey(field, node.metadata);
|
333
|
-
if (storageKey != null) {
|
334
|
-
field = {...field, storageKey};
|
335
|
-
}
|
336
|
-
return [field].concat(handles);
|
337
|
-
}
|
338
|
-
|
339
|
-
function generateModuleImport(node: ModuleImport): NormalizationModuleImport {
|
340
|
-
const fragmentName = node.name;
|
341
|
-
const regExpMatch = fragmentName.match(
|
342
|
-
/^([a-zA-Z][a-zA-Z0-9]*)(?:_([a-zA-Z][_a-zA-Z0-9]*))?$/,
|
343
|
-
);
|
344
|
-
if (!regExpMatch) {
|
345
|
-
throw createCompilerError(
|
346
|
-
'NormalizationCodeGenerator: @module fragments should be named ' +
|
347
|
-
`'FragmentName_propName', got '${fragmentName}'.`,
|
348
|
-
[node.loc],
|
349
|
-
);
|
350
|
-
}
|
351
|
-
const fragmentPropName = regExpMatch[2];
|
352
|
-
if (typeof fragmentPropName !== 'string') {
|
353
|
-
throw createCompilerError(
|
354
|
-
'NormalizationCodeGenerator: @module fragments should be named ' +
|
355
|
-
`'FragmentName_propName', got '${fragmentName}'.`,
|
356
|
-
[node.loc],
|
357
|
-
);
|
358
|
-
}
|
359
|
-
return {
|
360
|
-
args: null,
|
361
|
-
documentName: node.key,
|
362
|
-
fragmentName,
|
363
|
-
fragmentPropName,
|
364
|
-
kind: 'ModuleImport',
|
365
|
-
};
|
366
|
-
}
|
367
|
-
|
368
|
-
function generateTypeDiscriminator(
|
369
|
-
abstractKey: string,
|
370
|
-
): NormalizationTypeDiscriminator {
|
371
|
-
return {
|
372
|
-
kind: 'TypeDiscriminator',
|
373
|
-
abstractKey,
|
374
|
-
};
|
375
|
-
}
|
376
|
-
|
377
|
-
function generateScalarField(node: ScalarField): Array<NormalizationSelection> {
|
378
|
-
// flowlint-next-line sketchy-null-mixed:off
|
379
|
-
if (node.metadata?.skipNormalizationNode) {
|
380
|
-
return [];
|
381
|
-
}
|
382
|
-
// Note: it is important that the arguments of this field be sorted to
|
383
|
-
// ensure stable generation of storage keys for equivalent arguments
|
384
|
-
// which may have originally appeared in different orders across an app.
|
385
|
-
const handles =
|
386
|
-
(node.handles &&
|
387
|
-
node.handles.map(handle => {
|
388
|
-
if (handle.dynamicKey != null) {
|
389
|
-
throw createUserError(
|
390
|
-
'Dynamic key values are not supported on scalar fields.',
|
391
|
-
[handle.dynamicKey.loc],
|
392
|
-
);
|
393
|
-
}
|
394
|
-
const nodeHandle = {
|
395
|
-
alias: node.alias === node.name ? null : node.alias,
|
396
|
-
args: generateArgs(node.args),
|
397
|
-
filters: handle.filters,
|
398
|
-
handle: handle.name,
|
399
|
-
key: handle.key,
|
400
|
-
kind: 'ScalarHandle',
|
401
|
-
name: node.name,
|
402
|
-
};
|
403
|
-
|
404
|
-
if (handle.handleArgs != null) {
|
405
|
-
// $FlowFixMe handleArgs exists in Handle
|
406
|
-
nodeHandle.handleArgs = generateArgs(handle.handleArgs);
|
407
|
-
}
|
408
|
-
|
409
|
-
return nodeHandle;
|
410
|
-
})) ||
|
411
|
-
[];
|
412
|
-
let field = {
|
413
|
-
alias: node.alias === node.name ? null : node.alias,
|
414
|
-
args: generateArgs(node.args),
|
415
|
-
kind: 'ScalarField',
|
416
|
-
name: node.name,
|
417
|
-
storageKey: null,
|
418
|
-
};
|
419
|
-
// Precompute storageKey if possible
|
420
|
-
const storageKey = getStaticStorageKey(field, node.metadata);
|
421
|
-
if (storageKey != null) {
|
422
|
-
field = {...field, storageKey};
|
423
|
-
}
|
424
|
-
if (node.metadata?.flight === true) {
|
425
|
-
field = {...field, kind: 'FlightField'};
|
426
|
-
}
|
427
|
-
return [field].concat(handles);
|
428
|
-
}
|
429
|
-
|
430
|
-
function generateStream(schema: Schema, node: Stream): NormalizationStream {
|
431
|
-
if (
|
432
|
-
!(
|
433
|
-
node.if == null ||
|
434
|
-
node.if.kind === 'Variable' ||
|
435
|
-
(node.if.kind === 'Literal' && node.if.value === true)
|
436
|
-
)
|
437
|
-
) {
|
438
|
-
throw createCompilerError(
|
439
|
-
'NormalizationCodeGenerator: Expected @stream `if` condition to be ' +
|
440
|
-
'a variable, unspecified, or the literal `true`.',
|
441
|
-
[node.if?.loc ?? node.loc],
|
442
|
-
);
|
443
|
-
}
|
444
|
-
return {
|
445
|
-
if:
|
446
|
-
node.if != null && node.if.kind === 'Variable'
|
447
|
-
? node.if.variableName
|
448
|
-
: null,
|
449
|
-
kind: 'Stream',
|
450
|
-
label: node.label,
|
451
|
-
selections: generateSelections(schema, node.selections),
|
452
|
-
};
|
453
|
-
}
|
454
|
-
|
455
|
-
function generateArgumentValue(
|
456
|
-
name: string,
|
457
|
-
value: ArgumentValue,
|
458
|
-
): NormalizationArgument | null {
|
459
|
-
switch (value.kind) {
|
460
|
-
case 'Variable':
|
461
|
-
return {
|
462
|
-
kind: 'Variable',
|
463
|
-
name: name,
|
464
|
-
variableName: value.variableName,
|
465
|
-
};
|
466
|
-
case 'Literal':
|
467
|
-
return value.value === null
|
468
|
-
? null
|
469
|
-
: {
|
470
|
-
kind: 'Literal',
|
471
|
-
name: name,
|
472
|
-
value: stableCopy(value.value),
|
473
|
-
};
|
474
|
-
case 'ObjectValue': {
|
475
|
-
const objectKeys = value.fields.map(field => field.name).sort();
|
476
|
-
const objectValues = new Map(
|
477
|
-
value.fields.map(field => {
|
478
|
-
return [field.name, field.value];
|
479
|
-
}),
|
480
|
-
);
|
481
|
-
return {
|
482
|
-
fields: objectKeys.map(fieldName => {
|
483
|
-
const fieldValue = objectValues.get(fieldName);
|
484
|
-
if (fieldValue == null) {
|
485
|
-
throw createCompilerError('Expected to have object field value');
|
486
|
-
}
|
487
|
-
return (
|
488
|
-
generateArgumentValue(fieldName, fieldValue) ?? {
|
489
|
-
kind: 'Literal',
|
490
|
-
name: fieldName,
|
491
|
-
value: null,
|
492
|
-
}
|
493
|
-
);
|
494
|
-
}),
|
495
|
-
kind: 'ObjectValue',
|
496
|
-
name: name,
|
497
|
-
};
|
498
|
-
}
|
499
|
-
case 'ListValue': {
|
500
|
-
return {
|
501
|
-
items: value.items.map((item, index) => {
|
502
|
-
return generateArgumentValue(`${name}.${index}`, item);
|
503
|
-
}),
|
504
|
-
kind: 'ListValue',
|
505
|
-
name: name,
|
506
|
-
};
|
507
|
-
}
|
508
|
-
default:
|
509
|
-
throw createUserError(
|
510
|
-
'NormalizationCodeGenerator: Complex argument values (Lists or ' +
|
511
|
-
'InputObjects with nested variables) are not supported.',
|
512
|
-
[value.loc],
|
513
|
-
);
|
514
|
-
}
|
515
|
-
}
|
516
|
-
|
517
|
-
function generateArgs(
|
518
|
-
args: $ReadOnlyArray<Argument>,
|
519
|
-
): ?$ReadOnlyArray<NormalizationArgument> {
|
520
|
-
const concreteArguments = [];
|
521
|
-
args.forEach(arg => {
|
522
|
-
const concreteArgument = generateArgumentValue(arg.name, arg.value);
|
523
|
-
if (concreteArgument !== null) {
|
524
|
-
concreteArguments.push(concreteArgument);
|
525
|
-
}
|
526
|
-
});
|
527
|
-
return concreteArguments.length === 0
|
528
|
-
? null
|
529
|
-
: concreteArguments.sort(nameComparator);
|
530
|
-
}
|
531
|
-
|
532
|
-
function nameComparator(
|
533
|
-
a: {+name: string, ...},
|
534
|
-
b: {+name: string, ...},
|
535
|
-
): number {
|
536
|
-
return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
|
537
|
-
}
|
538
|
-
|
539
|
-
/**
|
540
|
-
* Pre-computes storage key if possible and advantageous. Storage keys are
|
541
|
-
* generated for fields with supplied arguments that are all statically known
|
542
|
-
* (ie. literals, no variables) at build time.
|
543
|
-
*/
|
544
|
-
function getStaticStorageKey(
|
545
|
-
field: NormalizationField,
|
546
|
-
metadata: Metadata,
|
547
|
-
): ?string {
|
548
|
-
const metadataStorageKey = metadata?.storageKey;
|
549
|
-
if (typeof metadataStorageKey === 'string') {
|
550
|
-
return metadataStorageKey;
|
551
|
-
}
|
552
|
-
if (
|
553
|
-
!field.args ||
|
554
|
-
field.args.length === 0 ||
|
555
|
-
field.args.some(argumentContainsVariables)
|
556
|
-
) {
|
557
|
-
return null;
|
558
|
-
}
|
559
|
-
return getStorageKey(field, {});
|
560
|
-
}
|
561
|
-
|
562
|
-
function isPlural(schema: Schema, type: TypeID): boolean {
|
563
|
-
return schema.isList(schema.getNullableType(type));
|
564
|
-
}
|
565
|
-
|
566
|
-
module.exports = {generate};
|