-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
RelayCompiler.js
174 lines (156 loc) · 5.34 KB
/
RelayCompiler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/**
* Copyright (c) 2013-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
* @providesModule RelayCompiler
* @format
*/
'use strict';
const RelayCodeGenerator = require('RelayCodeGenerator');
const RelayCompilerContext = require('RelayCompilerContext');
const RelayPrinter = require('RelayPrinter');
const filterContextForNode = require('filterContextForNode');
import type {GeneratedNode} from 'RelayConcreteNode';
import type {Fragment, Root} from 'RelayIR';
import type {IRTransform} from 'RelayIRTransforms';
import type {GraphQLSchema} from 'graphql';
export type CompiledDocumentMap = Map<string, GeneratedNode>;
export type TransformReducer = (
ctx: RelayCompilerContext,
transform: (ctx: RelayCompilerContext) => RelayCompilerContext,
) => RelayCompilerContext;
export interface Compiler {
add(text: string): Array<Root | Fragment>,
compile(): CompiledDocumentMap,
}
export type CompilerTransforms = {
codegenTransforms: Array<IRTransform>,
fragmentTransforms: Array<IRTransform>,
printTransforms: Array<IRTransform>,
queryTransforms: Array<IRTransform>,
};
/**
* A utility class for parsing a corpus of GraphQL documents, transforming them
* with a standardized set of transforms, and generating runtime representations
* of each definition.
*/
class RelayCompiler {
_context: RelayCompilerContext;
_schema: GraphQLSchema;
_transformedQueryContext: ?RelayCompilerContext;
_transforms: CompilerTransforms;
// The context passed in must already have any Relay-specific schema extensions
constructor(
schema: GraphQLSchema,
context: RelayCompilerContext,
transforms: CompilerTransforms,
) {
this._context = context;
// some transforms depend on this being the original schema,
// not the transformed schema/context's schema
this._schema = schema;
this._transforms = transforms;
}
clone(): RelayCompiler {
return new RelayCompiler(this._schema, this._context, this._transforms);
}
context(): RelayCompilerContext {
return this._context;
}
addDefinitions(definitions: Array<Fragment | Root>): Array<Root | Fragment> {
this._context = this._context.addAll(definitions);
return this._context.documents();
}
// Can only be called once per compiler. Once run, will use cached context
// To re-run, clone the compiler.
transformedQueryContext(): RelayCompilerContext {
if (this._transformedQueryContext) {
return this._transformedQueryContext;
}
this._transformedQueryContext = this._transforms.queryTransforms.reduce(
(ctx, transform) => transform(ctx, this._schema),
this._context,
);
return this._transformedQueryContext;
}
compile(): CompiledDocumentMap {
const transformContext = ((ctx, transform) =>
transform(ctx, this._schema): any);
const fragmentContext = this._transforms.fragmentTransforms.reduce(
transformContext,
this._context,
);
const queryContext = this.transformedQueryContext();
const printContext = this._transforms.printTransforms.reduce(
transformContext,
queryContext,
);
const codeGenContext = this._transforms.codegenTransforms.reduce(
transformContext,
queryContext,
);
const compiledDocuments = new Map();
fragmentContext.documents().forEach(node => {
if (node.kind !== 'Fragment') {
return;
}
const generatedFragment = RelayCodeGenerator.generate(node);
compiledDocuments.set(node.name, generatedFragment);
});
queryContext.documents().forEach(node => {
if (node.kind !== 'Root') {
return;
}
const {name} = node;
// The unflattened query is used for printing, since flattening creates an
// invalid query.
const text = filterContextForNode(
printContext.getRoot(name),
printContext,
)
.documents()
.map(RelayPrinter.print)
.join('\n');
// The original query (with fragment spreads) is converted to a fragment
// for reading out the root data.
const sourceNode = fragmentContext.getRoot(name);
const rootFragment = buildFragmentForRoot(sourceNode);
const generatedFragment = RelayCodeGenerator.generate(rootFragment);
// The flattened query is used for codegen in order to reduce the number of
// duplicate fields that must be processed during response normalization.
const codeGenNode = codeGenContext.getRoot(name);
const generatedQuery = RelayCodeGenerator.generate(codeGenNode);
const batchQuery = {
fragment: generatedFragment,
id: null,
kind: 'Batch',
metadata: node.metadata || {},
name,
query: generatedQuery,
text,
};
compiledDocuments.set(name, batchQuery);
});
return compiledDocuments;
}
}
/**
* Construct the fragment equivalent of a root node.
*/
function buildFragmentForRoot(root: Root): Fragment {
return {
argumentDefinitions: (root.argumentDefinitions: $FlowIssue),
directives: root.directives,
kind: 'Fragment',
metadata: null,
name: root.name,
selections: root.selections,
type: root.type,
};
}
module.exports = RelayCompiler;