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.
- package/bin/RelayCompilerBin.js.flow +169 -0
- package/bin/RelayCompilerMain.js.flow +508 -0
- package/bin/__fixtures__/plugin-module.js.flow +17 -0
- package/bin/relay-compiler +2002 -1733
- package/codegen/CodegenDirectory.js.flow +375 -0
- package/codegen/CodegenRunner.js.flow +431 -0
- package/codegen/CodegenTypes.js.flow +28 -0
- package/codegen/CodegenWatcher.js.flow +254 -0
- package/codegen/NormalizationCodeGenerator.js.flow +499 -0
- package/codegen/ReaderCodeGenerator.js.flow +453 -0
- package/codegen/RelayCodeGenerator.js.flow +76 -0
- package/codegen/RelayFileWriter.js.flow +366 -0
- package/codegen/SourceControl.js.flow +58 -0
- package/codegen/compileRelayArtifacts.js.flow +182 -0
- package/codegen/createPrintRequireModuleDependency.js.flow +21 -0
- package/codegen/writeRelayGeneratedFile.js.flow +194 -0
- package/core/ASTCache.js.flow +73 -0
- package/core/ASTConvert.js.flow +233 -0
- package/core/CompilerContext.js.flow +190 -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 +48 -0
- package/core/GraphQLWatchmanClient.js.flow +111 -0
- package/core/IR.js.flow +329 -0
- package/core/IRPrinter.js.flow +488 -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 +130 -0
- package/core/RelayParser.js.flow +1759 -0
- package/core/RelaySourceModuleParser.js.flow +135 -0
- package/core/Schema.js.flow +1985 -0
- package/core/SchemaUtils.js.flow +109 -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 +202 -0
- package/language/RelayLanguagePluginInterface.js.flow +283 -0
- package/language/javascript/FindGraphQLTags.js.flow +233 -0
- package/language/javascript/RelayFlowBabelFactories.js.flow +180 -0
- package/language/javascript/RelayFlowGenerator.js.flow +1040 -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 +113 -119
- package/lib/codegen/CodegenDirectory.js +2 -6
- package/lib/codegen/CodegenRunner.js +34 -75
- package/lib/codegen/CodegenWatcher.js +13 -21
- package/lib/codegen/NormalizationCodeGenerator.js +43 -40
- package/lib/codegen/ReaderCodeGenerator.js +43 -35
- package/lib/codegen/RelayCodeGenerator.js +7 -5
- package/lib/codegen/RelayFileWriter.js +15 -36
- package/lib/codegen/compileRelayArtifacts.js +16 -30
- package/lib/codegen/writeRelayGeneratedFile.js +56 -98
- package/lib/core/ASTCache.js +1 -3
- package/lib/core/CompilerContext.js +1 -3
- package/lib/core/CompilerError.js +27 -54
- package/lib/core/GraphQLCompilerProfiler.js +8 -12
- package/lib/core/GraphQLWatchmanClient.js +4 -12
- package/lib/core/IRPrinter.js +5 -5
- 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 +9 -5
- package/lib/core/RelayParser.js +44 -56
- package/lib/core/RelaySourceModuleParser.js +1 -3
- package/lib/core/Schema.js +78 -65
- package/lib/core/getFieldDefinition.js +12 -15
- package/lib/core/getIdentifierForSelection.js +1 -1
- package/lib/core/inferRootArgumentDefinitions.js +27 -36
- package/lib/language/javascript/RelayFlowGenerator.js +49 -78
- 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 +49 -78
- package/lib/runner/StrictMap.js +20 -36
- package/lib/runner/getChangedNodeNames.js +30 -62
- package/lib/transforms/ApplyFragmentArgumentTransform.js +33 -28
- package/lib/transforms/ClientExtensionsTransform.js +7 -12
- package/lib/transforms/ConnectionTransform.js +26 -24
- package/lib/transforms/DeferStreamTransform.js +20 -16
- package/lib/transforms/DisallowTypenameOnRoot.js +55 -0
- package/lib/transforms/FieldHandleTransform.js +6 -2
- package/lib/transforms/FlattenTransform.js +19 -14
- package/lib/transforms/GenerateIDFieldTransform.js +7 -3
- package/lib/transforms/GenerateTypeNameTransform.js +6 -2
- package/lib/transforms/InlineDataFragmentTransform.js +7 -3
- package/lib/transforms/MaskTransform.js +17 -17
- package/lib/transforms/MatchTransform.js +110 -32
- package/lib/transforms/RefetchableFragmentTransform.js +21 -17
- package/lib/transforms/RelayDirectiveTransform.js +11 -3
- package/lib/transforms/SkipClientExtensionsTransform.js +8 -0
- package/lib/transforms/SkipHandleFieldTransform.js +6 -2
- package/lib/transforms/SkipRedundantNodesTransform.js +6 -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 +7 -3
- 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 +7 -2
- package/lib/transforms/query-generators/QueryQueryGenerator.js +1 -0
- 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 +12 -15
- package/lib/util/RelayCompilerCache.js +1 -3
- package/lib/util/dedupeJSONStringify.js +15 -12
- package/lib/util/getModuleName.js +1 -1
- package/package.json +2 -2
- 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 +218 -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 +474 -0
- package/transforms/ClientExtensionsTransform.js.flow +220 -0
- package/transforms/ConnectionTransform.js.flow +869 -0
- package/transforms/DeferStreamTransform.js.flow +258 -0
- package/transforms/DisallowIdAsAlias.js.flow +47 -0
- package/transforms/DisallowTypenameOnRoot.js.flow +45 -0
- package/transforms/FieldHandleTransform.js.flow +80 -0
- package/transforms/FilterDirectivesTransform.js.flow +45 -0
- package/transforms/FlattenTransform.js.flow +456 -0
- package/transforms/GenerateIDFieldTransform.js.flow +134 -0
- package/transforms/GenerateTypeNameTransform.js.flow +81 -0
- package/transforms/InlineDataFragmentTransform.js.flow +124 -0
- package/transforms/InlineFragmentsTransform.js.flow +71 -0
- package/transforms/MaskTransform.js.flow +126 -0
- package/transforms/MatchTransform.js.flow +583 -0
- package/transforms/RefetchableFragmentTransform.js.flow +272 -0
- package/transforms/RelayDirectiveTransform.js.flow +99 -0
- package/transforms/SkipClientExtensionsTransform.js.flow +54 -0
- package/transforms/SkipHandleFieldTransform.js.flow +44 -0
- package/transforms/SkipRedundantNodesTransform.js.flow +253 -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 +138 -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 +190 -0
- package/transforms/query-generators/NodeQueryGenerator.js.flow +206 -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 +72 -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/dedupeJSONStringify.js.flow +152 -0
- package/util/getDefinitionNodeHash.js.flow +25 -0
- package/util/getModuleName.js.flow +39 -0
- package/util/joinArgumentDefinitions.js.flow +99 -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,250 @@
|
|
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 {GraphQLError} = require('graphql');
|
16
|
+
|
17
|
+
import type {Location} from './IR';
|
18
|
+
import type {ASTNode, Source, SourceLocation} from 'graphql';
|
19
|
+
|
20
|
+
// Combined results of multiple user errors
|
21
|
+
export opaque type CombinedUserError: Error = Error;
|
22
|
+
|
23
|
+
// Error describing invalid application code from which the compiler
|
24
|
+
// can recover, ie which does not prevent processing of other inputs.
|
25
|
+
export opaque type UserError: Error = GraphQLError;
|
26
|
+
|
27
|
+
// Error describing invalid application code from which the compiler
|
28
|
+
// *cannot* recover, ie where the validity of subsequent inputs cannot
|
29
|
+
// be determined. Try to avoid these errors.
|
30
|
+
export opaque type NonRecoverableUserError: Error = Error;
|
31
|
+
|
32
|
+
// Error describing a bug in the compiler
|
33
|
+
export opaque type CompilerError: Error = Error;
|
34
|
+
|
35
|
+
/**
|
36
|
+
* Creates an error describing invalid application code (GraphQL/Schema)
|
37
|
+
* that must be fixed by the end developer. This should only be used
|
38
|
+
* for local errors that don't affect processing of other user code.
|
39
|
+
*/
|
40
|
+
function createUserError(
|
41
|
+
message: string,
|
42
|
+
locations?: ?$ReadOnlyArray<Location>,
|
43
|
+
nodes?: ?$ReadOnlyArray<ASTNode>,
|
44
|
+
): UserError {
|
45
|
+
let messageWithLocations = message;
|
46
|
+
if (locations != null) {
|
47
|
+
const printedLocations = printLocations(locations);
|
48
|
+
messageWithLocations =
|
49
|
+
printedLocations.length === 0
|
50
|
+
? message
|
51
|
+
: [message, ...printedLocations].join('\n\n') + '\n';
|
52
|
+
}
|
53
|
+
return new GraphQLError(messageWithLocations, nodes ?? []);
|
54
|
+
}
|
55
|
+
|
56
|
+
/**
|
57
|
+
* Similar to createUserError but for errors that are *not* recoverable:
|
58
|
+
* the compiler should not continue to process other inputs because their
|
59
|
+
* validity can't be determined.
|
60
|
+
*/
|
61
|
+
function createNonRecoverableUserError(
|
62
|
+
message: string,
|
63
|
+
locations?: ?$ReadOnlyArray<Location>,
|
64
|
+
nodes?: ?$ReadOnlyArray<ASTNode>,
|
65
|
+
): NonRecoverableUserError {
|
66
|
+
let messageWithLocations = message;
|
67
|
+
if (locations != null) {
|
68
|
+
const printedLocations = printLocations(locations);
|
69
|
+
messageWithLocations =
|
70
|
+
printedLocations.length === 0
|
71
|
+
? message
|
72
|
+
: [message, ...printedLocations].join('\n\n') + '\n';
|
73
|
+
}
|
74
|
+
const error = new GraphQLError(messageWithLocations, nodes ?? []);
|
75
|
+
return new Error(error.message);
|
76
|
+
}
|
77
|
+
|
78
|
+
/**
|
79
|
+
* Creates an error describing a problem with the compiler itself - such
|
80
|
+
* as a broken invariant - that must be fixed within the compiler.
|
81
|
+
*/
|
82
|
+
function createCompilerError(
|
83
|
+
message: string,
|
84
|
+
locations?: ?$ReadOnlyArray<Location>,
|
85
|
+
nodes?: ?$ReadOnlyArray<ASTNode>,
|
86
|
+
): CompilerError {
|
87
|
+
let messageWithLocations = message;
|
88
|
+
if (locations != null) {
|
89
|
+
const printedLocations = printLocations(locations);
|
90
|
+
messageWithLocations =
|
91
|
+
printedLocations.length === 0
|
92
|
+
? message
|
93
|
+
: [message, ...printedLocations].join('\n\n') + '\n';
|
94
|
+
}
|
95
|
+
const error = new GraphQLError(
|
96
|
+
`Internal Error: ${messageWithLocations}`,
|
97
|
+
nodes ?? [],
|
98
|
+
);
|
99
|
+
return new Error(error.message);
|
100
|
+
}
|
101
|
+
|
102
|
+
/**
|
103
|
+
* Iterates over the elements of some iterable value, calling the
|
104
|
+
* supplied callback for each item with a guard for user errors.
|
105
|
+
*
|
106
|
+
* Non-user errors abort the iteration and are instantly rethrown.
|
107
|
+
* User errors are collected and rethrown at the end, if multiple user errors
|
108
|
+
* occur, a combined error is thrown.
|
109
|
+
*/
|
110
|
+
function eachWithCombinedError<T>(iterable: Iterable<T>, fn: T => void): void {
|
111
|
+
const errors: Array<UserError> = [];
|
112
|
+
for (const item of iterable) {
|
113
|
+
try {
|
114
|
+
fn(item);
|
115
|
+
} catch (error) {
|
116
|
+
if (error instanceof GraphQLError) {
|
117
|
+
errors.push(error);
|
118
|
+
} else {
|
119
|
+
throw error;
|
120
|
+
}
|
121
|
+
}
|
122
|
+
}
|
123
|
+
if (errors.length > 0) {
|
124
|
+
if (errors.length === 1) {
|
125
|
+
throw errors[0];
|
126
|
+
}
|
127
|
+
throw createUserError(
|
128
|
+
`Encountered ${errors.length} errors:\n` +
|
129
|
+
errors
|
130
|
+
.map(error =>
|
131
|
+
String(error)
|
132
|
+
.split('\n')
|
133
|
+
.map((line, index) => (index === 0 ? `- ${line}` : ` ${line}`))
|
134
|
+
.join('\n'),
|
135
|
+
)
|
136
|
+
.join('\n'),
|
137
|
+
);
|
138
|
+
}
|
139
|
+
}
|
140
|
+
|
141
|
+
function printLocations(locations: $ReadOnlyArray<Location>): Array<string> {
|
142
|
+
const printedLocations = [];
|
143
|
+
for (const location of locations) {
|
144
|
+
let sourceLocation = location;
|
145
|
+
while (sourceLocation.kind === 'Derived') {
|
146
|
+
sourceLocation = sourceLocation.source;
|
147
|
+
}
|
148
|
+
switch (sourceLocation.kind) {
|
149
|
+
case 'Source': {
|
150
|
+
// source location
|
151
|
+
const prefix =
|
152
|
+
sourceLocation === location ? 'Source: ' : 'Source (derived): ';
|
153
|
+
printedLocations.push(
|
154
|
+
prefix +
|
155
|
+
highlightSourceAtLocation(
|
156
|
+
sourceLocation.source,
|
157
|
+
getLocation(sourceLocation.source, sourceLocation.start),
|
158
|
+
),
|
159
|
+
);
|
160
|
+
break;
|
161
|
+
}
|
162
|
+
case 'Generated': {
|
163
|
+
printedLocations.push('Source: (generated)');
|
164
|
+
break;
|
165
|
+
}
|
166
|
+
case 'Unknown': {
|
167
|
+
printedLocations.push('Source: (unknown)');
|
168
|
+
break;
|
169
|
+
}
|
170
|
+
default: {
|
171
|
+
(sourceLocation: empty);
|
172
|
+
throw createCompilerError(
|
173
|
+
`CompilerError: cannot print location '${String(sourceLocation)}'.`,
|
174
|
+
);
|
175
|
+
}
|
176
|
+
}
|
177
|
+
}
|
178
|
+
return printedLocations;
|
179
|
+
}
|
180
|
+
|
181
|
+
/**
|
182
|
+
* Render a helpful description of the location of the error in the GraphQL
|
183
|
+
* Source document.
|
184
|
+
*/
|
185
|
+
function highlightSourceAtLocation(
|
186
|
+
source: Source,
|
187
|
+
location: SourceLocation,
|
188
|
+
): string {
|
189
|
+
const firstLineColumnOffset = source.locationOffset.column - 1;
|
190
|
+
const body = whitespace(firstLineColumnOffset) + source.body;
|
191
|
+
|
192
|
+
const lineIndex = location.line - 1;
|
193
|
+
const lineOffset = source.locationOffset.line - 1;
|
194
|
+
const lineNum = location.line + lineOffset;
|
195
|
+
|
196
|
+
const columnOffset = location.line === 1 ? firstLineColumnOffset : 0;
|
197
|
+
const columnNum = location.column + columnOffset;
|
198
|
+
|
199
|
+
const lines = body.split(/\r\n|[\n\r]/g);
|
200
|
+
return (
|
201
|
+
`${source.name} (${lineNum}:${columnNum})\n` +
|
202
|
+
printPrefixedLines([
|
203
|
+
// Lines specified like this: ["prefix", "string"],
|
204
|
+
[`${lineNum - 1}: `, lines[lineIndex - 1]],
|
205
|
+
[`${lineNum}: `, lines[lineIndex]],
|
206
|
+
['', whitespace(columnNum - 1) + '^'],
|
207
|
+
[`${lineNum + 1}: `, lines[lineIndex + 1]],
|
208
|
+
])
|
209
|
+
);
|
210
|
+
}
|
211
|
+
|
212
|
+
function printPrefixedLines(lines: Array<[string, string]>): string {
|
213
|
+
const existingLines = lines.filter(([_, line]) => line !== undefined);
|
214
|
+
|
215
|
+
let padLen = 0;
|
216
|
+
for (const [prefix] of existingLines) {
|
217
|
+
padLen = Math.max(padLen, prefix.length);
|
218
|
+
}
|
219
|
+
|
220
|
+
return existingLines
|
221
|
+
.map(([prefix, line]) => lpad(padLen, prefix) + line)
|
222
|
+
.join('\n');
|
223
|
+
}
|
224
|
+
|
225
|
+
function whitespace(len: number): string {
|
226
|
+
return Array(len + 1).join(' ');
|
227
|
+
}
|
228
|
+
|
229
|
+
function lpad(len: number, str: string): string {
|
230
|
+
return whitespace(len - str.length) + str;
|
231
|
+
}
|
232
|
+
|
233
|
+
function getLocation(source: Source, position: number): SourceLocation {
|
234
|
+
const lineRegexp = /\r\n|[\n\r]/g;
|
235
|
+
let line = 1;
|
236
|
+
let column = position + 1;
|
237
|
+
let match;
|
238
|
+
while ((match = lineRegexp.exec(source.body)) && match.index < position) {
|
239
|
+
line += 1;
|
240
|
+
column = position + 1 - (match.index + match[0].length);
|
241
|
+
}
|
242
|
+
return {line, column};
|
243
|
+
}
|
244
|
+
|
245
|
+
module.exports = {
|
246
|
+
createCompilerError,
|
247
|
+
createNonRecoverableUserError,
|
248
|
+
createUserError,
|
249
|
+
eachWithCombinedError,
|
250
|
+
};
|
@@ -0,0 +1,39 @@
|
|
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 ASTCache = require('./ASTCache');
|
16
|
+
|
17
|
+
const fs = require('fs');
|
18
|
+
const path = require('path');
|
19
|
+
|
20
|
+
const {parse, Source} = require('graphql');
|
21
|
+
|
22
|
+
import type {File} from '../codegen/CodegenTypes';
|
23
|
+
import type {DocumentNode} from 'graphql';
|
24
|
+
|
25
|
+
function parseFile(baseDir: string, file: File): ?DocumentNode {
|
26
|
+
const text = fs.readFileSync(path.join(baseDir, file.relPath), 'utf8');
|
27
|
+
return parse(new Source(text, file.relPath), {
|
28
|
+
experimentalFragmentVariables: true,
|
29
|
+
});
|
30
|
+
}
|
31
|
+
|
32
|
+
function getParser(baseDir: string): ASTCache {
|
33
|
+
return new ASTCache({baseDir, parse: parseFile});
|
34
|
+
}
|
35
|
+
|
36
|
+
module.exports = {
|
37
|
+
parseFile,
|
38
|
+
getParser,
|
39
|
+
};
|
@@ -0,0 +1,341 @@
|
|
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
|
+
/**
|
18
|
+
* The compiler profiler builds a "call graph" of high level operations as a
|
19
|
+
* means of tracking time spent over the course of running the compiler.
|
20
|
+
*/
|
21
|
+
|
22
|
+
type Trace =
|
23
|
+
| MetadataTrace
|
24
|
+
| BeginDurationTrace
|
25
|
+
| EndDurationTrace
|
26
|
+
| EventTrace
|
27
|
+
| BeginAsyncTrace
|
28
|
+
| EndAsyncTrace;
|
29
|
+
|
30
|
+
type MetadataTrace = {|
|
31
|
+
ph: 'M',
|
32
|
+
pid: number, // Process ID
|
33
|
+
tid: number, // Thread ID
|
34
|
+
name: 'process_name' | 'thread_name',
|
35
|
+
args: {|name: string|},
|
36
|
+
|};
|
37
|
+
|
38
|
+
type BeginDurationTrace = {|
|
39
|
+
ph: 'B',
|
40
|
+
name: string, // Event name
|
41
|
+
pid: number, // Process ID
|
42
|
+
tid: number, // Thread ID
|
43
|
+
ts: number, // Relative timestamp integer microseconds
|
44
|
+
|};
|
45
|
+
|
46
|
+
type EndDurationTrace = {|
|
47
|
+
ph: 'E',
|
48
|
+
name: string, // Event name
|
49
|
+
pid: number, // Process ID
|
50
|
+
tid: number, // Thread ID
|
51
|
+
ts: number, // Relative timestamp integer microseconds
|
52
|
+
|};
|
53
|
+
|
54
|
+
type EventTrace = {|
|
55
|
+
ph: 'X',
|
56
|
+
name: string, // Event name
|
57
|
+
pid: number, // Process ID
|
58
|
+
tid: number, // Thread ID
|
59
|
+
ts: number, // Relative timestamp integer in microseconds
|
60
|
+
dur: number, // Duration timestamp integer in microseconds
|
61
|
+
|};
|
62
|
+
|
63
|
+
type BeginAsyncTrace = {|
|
64
|
+
ph: 'b',
|
65
|
+
name: string, // Async event name
|
66
|
+
cat: string, // Async category
|
67
|
+
id: number, // Async tree ID
|
68
|
+
pid: number, // Process ID
|
69
|
+
tid: number, // Thread ID
|
70
|
+
ts: number, // Relative timestamp integer microseconds
|
71
|
+
|};
|
72
|
+
|
73
|
+
type EndAsyncTrace = {|
|
74
|
+
ph: 'e',
|
75
|
+
name: string, // Async event name
|
76
|
+
cat: string, // Async category
|
77
|
+
id: number, // Async tree ID
|
78
|
+
pid: number, // Process ID
|
79
|
+
tid: number, // Thread ID
|
80
|
+
ts: number, // Relative timestamp integer microseconds
|
81
|
+
|};
|
82
|
+
|
83
|
+
let enabled = false;
|
84
|
+
const traces: Array<Trace> = [
|
85
|
+
{
|
86
|
+
ph: 'M',
|
87
|
+
pid: 0,
|
88
|
+
tid: 0,
|
89
|
+
name: 'process_name',
|
90
|
+
args: {name: 'relay-compiler'},
|
91
|
+
},
|
92
|
+
{
|
93
|
+
ph: 'M',
|
94
|
+
pid: 0,
|
95
|
+
tid: 0,
|
96
|
+
name: 'thread_name',
|
97
|
+
args: {name: 'relay-compiler'},
|
98
|
+
},
|
99
|
+
];
|
100
|
+
const stack: Array<BeginDurationTrace> = [];
|
101
|
+
|
102
|
+
function enable(): void {
|
103
|
+
enabled = true;
|
104
|
+
}
|
105
|
+
|
106
|
+
function getTraces(): Array<Trace> {
|
107
|
+
return traces;
|
108
|
+
}
|
109
|
+
|
110
|
+
/**
|
111
|
+
* Run the provided function as part of a stack profile.
|
112
|
+
*/
|
113
|
+
function run<T>(name: string, fn: () => T): T {
|
114
|
+
return instrument(fn, name)();
|
115
|
+
}
|
116
|
+
|
117
|
+
/**
|
118
|
+
* Run the provided async function as part context in a stack profile.
|
119
|
+
* See instrumentAsyncContext() for limitations and usage notes.
|
120
|
+
*/
|
121
|
+
function asyncContext<T: Promise<$FlowFixMe>>(name: string, fn: () => T): T {
|
122
|
+
return instrumentAsyncContext(fn, name)();
|
123
|
+
}
|
124
|
+
|
125
|
+
/**
|
126
|
+
* Wait for the provided async operation as an async profile.
|
127
|
+
*/
|
128
|
+
function waitFor<T: Promise<$FlowFixMe>>(name: string, fn: () => T): T {
|
129
|
+
return instrumentWait(fn, name)();
|
130
|
+
}
|
131
|
+
|
132
|
+
/**
|
133
|
+
* Return a new instrumented sync function to be part of a stack profile.
|
134
|
+
*
|
135
|
+
* This instruments synchronous functions to be displayed in a stack
|
136
|
+
* visualization. To instrument async functions, see instrumentAsyncContext()
|
137
|
+
* and instrumentWait().
|
138
|
+
*/
|
139
|
+
function instrument<F: (...$FlowFixMe) => mixed>(fn: F, name?: string): F {
|
140
|
+
if (!enabled) {
|
141
|
+
return fn;
|
142
|
+
}
|
143
|
+
const profileName =
|
144
|
+
name ??
|
145
|
+
// $FlowFixMe - Flow no longer considers statics of functions as any
|
146
|
+
fn.displayName ??
|
147
|
+
fn.name;
|
148
|
+
const instrumented = function() {
|
149
|
+
const traceId = start(profileName);
|
150
|
+
try {
|
151
|
+
return fn.apply(this, arguments);
|
152
|
+
} finally {
|
153
|
+
end(traceId);
|
154
|
+
}
|
155
|
+
};
|
156
|
+
instrumented.displayName = profileName;
|
157
|
+
return (instrumented: $FlowFixMe);
|
158
|
+
}
|
159
|
+
|
160
|
+
/**
|
161
|
+
* Return a new instrumented async function which provides context for a stack.
|
162
|
+
*
|
163
|
+
* Because the resulting profiling information will be incorporated into a
|
164
|
+
* stack visualization, the instrumented function must represent a distinct
|
165
|
+
* region of time which does not overlap with any other async context.
|
166
|
+
*
|
167
|
+
* In other words, functions instrumented with instrumentAsyncContext must not
|
168
|
+
* run in parallel via Promise.all().
|
169
|
+
*
|
170
|
+
* To instrument functions which will run in parallel, use instrumentWait().
|
171
|
+
*/
|
172
|
+
function instrumentAsyncContext<F: (...$FlowFixMe) => Promise<$FlowFixMe>>(
|
173
|
+
fn: F,
|
174
|
+
name?: string,
|
175
|
+
): F {
|
176
|
+
if (!enabled) {
|
177
|
+
return fn;
|
178
|
+
}
|
179
|
+
|
180
|
+
const profileName: string =
|
181
|
+
name ??
|
182
|
+
// $FlowFixMe - Flow no longer considers statics of functions as any
|
183
|
+
fn.displayName ??
|
184
|
+
fn.name;
|
185
|
+
const instrumented = async function() {
|
186
|
+
const traceId = start(profileName);
|
187
|
+
try {
|
188
|
+
return await fn.apply(this, arguments);
|
189
|
+
} finally {
|
190
|
+
end(traceId);
|
191
|
+
}
|
192
|
+
};
|
193
|
+
instrumented.displayName = profileName;
|
194
|
+
return (instrumented: $FlowFixMe);
|
195
|
+
}
|
196
|
+
|
197
|
+
/**
|
198
|
+
* Return a new instrumented function which performs an awaited async operation.
|
199
|
+
*
|
200
|
+
* The instrumented function is not included in the overall run time of the
|
201
|
+
* compiler, instead it captures the time waiting on some asynchronous external
|
202
|
+
* resource such as network or filesystem which are often run in parallel.
|
203
|
+
*/
|
204
|
+
function instrumentWait<F: (...$FlowFixMe) => Promise<$FlowFixMe>>(
|
205
|
+
fn: F,
|
206
|
+
name?: string,
|
207
|
+
): F {
|
208
|
+
if (!enabled) {
|
209
|
+
return fn;
|
210
|
+
}
|
211
|
+
const profileName: string =
|
212
|
+
name ??
|
213
|
+
// $FlowFixMe - Flow no longer considers statics of functions as any
|
214
|
+
fn.displayName ??
|
215
|
+
fn.name;
|
216
|
+
const instrumented = async function() {
|
217
|
+
const traceId = startWait(profileName);
|
218
|
+
try {
|
219
|
+
return await fn.apply(this, arguments);
|
220
|
+
} finally {
|
221
|
+
end(traceId);
|
222
|
+
}
|
223
|
+
};
|
224
|
+
instrumented.displayName = profileName;
|
225
|
+
return (instrumented: $FlowFixMe);
|
226
|
+
}
|
227
|
+
|
228
|
+
const T_ZERO = process.hrtime();
|
229
|
+
|
230
|
+
// Return a Uint32 of microtime duration since program start.
|
231
|
+
function microtime(): number {
|
232
|
+
const hrtime = process.hrtime(T_ZERO);
|
233
|
+
// eslint-disable-next-line no-bitwise
|
234
|
+
return 0 | (hrtime[0] * 1e6 + Math.round(hrtime[1] / 1e3));
|
235
|
+
}
|
236
|
+
|
237
|
+
/**
|
238
|
+
* Start a stack profile with a particular name, returns an ID to pass to end().
|
239
|
+
*
|
240
|
+
* Other profiles may start before this one ends, which will be represented as
|
241
|
+
* nested operations, however all nested operations must end before this ends.
|
242
|
+
*
|
243
|
+
* In particular, be careful to end after errors.
|
244
|
+
*/
|
245
|
+
function start(name: string): number {
|
246
|
+
const beginTrace = {
|
247
|
+
ph: 'B',
|
248
|
+
name,
|
249
|
+
pid: 0,
|
250
|
+
tid: 0,
|
251
|
+
ts: microtime(),
|
252
|
+
};
|
253
|
+
traces.push(beginTrace);
|
254
|
+
stack.push(beginTrace);
|
255
|
+
return traces.length - 1;
|
256
|
+
}
|
257
|
+
|
258
|
+
let asyncID = 0;
|
259
|
+
|
260
|
+
/**
|
261
|
+
* Start an async wait profile with a particular name, returns an ID to pass
|
262
|
+
* to end().
|
263
|
+
*
|
264
|
+
* Other profiles may start before this one ends, which will be represented as
|
265
|
+
* nested operations, however all nested operations must end before this ends.
|
266
|
+
*
|
267
|
+
* In particular, be careful to end after errors.
|
268
|
+
*/
|
269
|
+
function startWait(name: string): number {
|
270
|
+
traces.push({
|
271
|
+
ph: 'b',
|
272
|
+
name,
|
273
|
+
cat: 'wait',
|
274
|
+
id: asyncID++,
|
275
|
+
pid: 0,
|
276
|
+
tid: 0,
|
277
|
+
ts: microtime(),
|
278
|
+
});
|
279
|
+
return traces.length - 1;
|
280
|
+
}
|
281
|
+
|
282
|
+
function end(traceIdx: number): void {
|
283
|
+
const trace = traces[traceIdx];
|
284
|
+
|
285
|
+
if (trace.ph === 'b') {
|
286
|
+
traces.push({
|
287
|
+
ph: 'e',
|
288
|
+
cat: trace.cat,
|
289
|
+
name: trace.name,
|
290
|
+
id: trace.id,
|
291
|
+
pid: trace.pid,
|
292
|
+
tid: trace.tid,
|
293
|
+
ts: microtime(),
|
294
|
+
});
|
295
|
+
return;
|
296
|
+
}
|
297
|
+
|
298
|
+
invariant(trace.ph === 'B', 'Begin trace phase');
|
299
|
+
invariant(
|
300
|
+
stack.pop() === trace,
|
301
|
+
'GraphQLCompilerProfiler: The profile trace %s ended before nested traces. ' +
|
302
|
+
'If it is async, try using Profile.waitFor or Profile.profileWait.',
|
303
|
+
trace.name,
|
304
|
+
);
|
305
|
+
|
306
|
+
const prevTrace = traces[traces.length - 1];
|
307
|
+
|
308
|
+
if (trace === prevTrace) {
|
309
|
+
traces[traceIdx] = {
|
310
|
+
ph: 'X',
|
311
|
+
name: trace.name,
|
312
|
+
pid: trace.pid,
|
313
|
+
tid: trace.tid,
|
314
|
+
ts: trace.ts,
|
315
|
+
dur: microtime() - trace.ts,
|
316
|
+
};
|
317
|
+
return;
|
318
|
+
}
|
319
|
+
|
320
|
+
traces.push({
|
321
|
+
ph: 'E',
|
322
|
+
name: trace.name,
|
323
|
+
pid: trace.pid,
|
324
|
+
tid: trace.tid,
|
325
|
+
ts: microtime(),
|
326
|
+
});
|
327
|
+
}
|
328
|
+
|
329
|
+
module.exports = {
|
330
|
+
enable,
|
331
|
+
getTraces,
|
332
|
+
run,
|
333
|
+
asyncContext,
|
334
|
+
waitFor,
|
335
|
+
instrument,
|
336
|
+
instrumentAsyncContext,
|
337
|
+
instrumentWait,
|
338
|
+
start,
|
339
|
+
startWait,
|
340
|
+
end,
|
341
|
+
};
|
@@ -0,0 +1,48 @@
|
|
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
|
+
import type {Fragment, Request, Root, SplitOperation} from './IR';
|
16
|
+
import type {GeneratedNode} from 'relay-runtime';
|
17
|
+
|
18
|
+
/**
|
19
|
+
* Helpers to retieve the name of the document from which the input derives:
|
20
|
+
* this is either the name of the input itself (if it is not a derived node)
|
21
|
+
* or the metadata.derivedFrom property for derived nodes.
|
22
|
+
*/
|
23
|
+
|
24
|
+
// Version for generated nodes
|
25
|
+
function getReaderSourceDefinitionName(node: GeneratedNode): string {
|
26
|
+
const [name, derivedFrom] =
|
27
|
+
node.kind === 'Request'
|
28
|
+
? [node.params.name, node.params.metadata?.derivedFrom]
|
29
|
+
: node.kind === 'SplitOperation'
|
30
|
+
? [node.name, node.metadata?.derivedFrom]
|
31
|
+
: [node.name, null];
|
32
|
+
return typeof derivedFrom === 'string' ? derivedFrom : name;
|
33
|
+
}
|
34
|
+
|
35
|
+
// Version for IR
|
36
|
+
function getSourceDefinitionName(
|
37
|
+
node: Fragment | Request | Root | SplitOperation,
|
38
|
+
): string {
|
39
|
+
const derivedFrom =
|
40
|
+
node.kind === 'Request' ||
|
41
|
+
node.kind === 'Root' ||
|
42
|
+
node.kind === 'SplitOperation'
|
43
|
+
? node.metadata?.derivedFrom
|
44
|
+
: null;
|
45
|
+
return typeof derivedFrom === 'string' ? derivedFrom : node.name;
|
46
|
+
}
|
47
|
+
|
48
|
+
module.exports = {getReaderSourceDefinitionName, getSourceDefinitionName};
|