-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
ConnectionFieldTransform.js
357 lines (342 loc) · 10 KB
/
ConnectionFieldTransform.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
const IRTransformer = require('../core/GraphQLIRTransformer');
const {createUserError} = require('../core/RelayCompilerError');
const {buildConnectionMetadata} = require('./ConnectionTransform');
const {ConnectionInterface} = require('relay-runtime');
import type CompilerContext from '../core/GraphQLCompilerContext';
import type {
Connection,
ConnectionField,
Directive,
Fragment,
LinkedField,
Root,
ScalarField,
} from '../core/GraphQLIR';
import type {ConnectionMetadata} from 'relay-runtime';
const SCHEMA_EXTENSION = `
directive @connection_resolver(label: String!) on FIELD
directive @stream_connection_resolver(
label: String!
initial_count: Int!
if: Boolean = true
) on FIELD
`;
type State = {|
+documentName: string,
+labels: Map<string, Directive>,
// The current path
path: Array<?string>,
// Metadata recorded for @connection fields
connectionMetadata: Array<ConnectionMetadata>,
|};
/**
* This transform rewrites LinkedField nodes with @connection_resolver and
* rewrites their edges/pageInfo selections to be wrapped in a Connection node.
*/
function connectionFieldTransform(context: CompilerContext): CompilerContext {
return IRTransformer.transform(
context,
{
Fragment: visitFragmentOrRoot,
LinkedField: (visitLinkedField: $FlowFixMe),
Root: visitFragmentOrRoot,
ScalarField: visitScalarField,
},
node => ({
documentName: node.name,
labels: new Map(),
path: [],
connectionMetadata: [],
}),
);
}
function visitFragmentOrRoot<N: Fragment | Root>(node: N, state: State): N {
const transformedNode = this.traverse(node, state);
const connectionMetadata = state.connectionMetadata;
if (connectionMetadata.length) {
return {
...transformedNode,
metadata: {
...transformedNode.metadata,
connection: connectionMetadata,
},
};
}
return transformedNode;
}
function visitLinkedField(
field: LinkedField,
state: State,
): LinkedField | ConnectionField {
const context: CompilerContext = this.getContext();
const schema = context.getSchema();
const path = state.path.concat(field.alias);
const transformed: LinkedField = this.traverse(field, {
...state,
path,
});
const connectionDirective = transformed.directives.find(
directive =>
directive.name === 'connection_resolver' ||
directive.name === 'stream_connection_resolver',
);
if (connectionDirective == null) {
return transformed;
}
if (schema.isList(schema.getNullableType(transformed.type))) {
throw createUserError(
"@connection_resolver fields must return a single value, not a list, found '" +
`${schema.getTypeString(transformed.type)}'`,
[transformed.loc],
);
}
const labelArg = connectionDirective.args.find(({name}) => name === 'label');
const label = getLiteralStringArgument(connectionDirective, 'label');
if (
typeof label !== 'string' ||
(label !== state.documentName &&
label.indexOf(state.documentName + '$') !== 0)
) {
throw createUserError(
'Invalid usage of @connection_resolver, expected a static string ' +
`'label'. Labels may be the document name ('${state.documentName}') ` +
`or be prefixed with the document name ('${
state.documentName
}$<name>')`,
[labelArg?.loc ?? connectionDirective.loc],
);
}
const previousDirective = state.labels.get(label);
if (previousDirective != null) {
const prevLabelArg = previousDirective.args.find(
({name}) => name === 'label',
);
const previousLocation = prevLabelArg?.loc ?? previousDirective.loc;
if (labelArg) {
throw createUserError(
'Invalid use of @connection_resolver, the provided label is ' +
"not unique. Specify a unique 'label' as a literal string.",
[labelArg?.loc, previousLocation],
);
} else {
throw createUserError(
'Invalid use of @connection_resolver, could not generate a ' +
"default label that is unique. Specify a unique 'label' " +
'as a literal string.',
[connectionDirective.loc, previousLocation],
);
}
}
state.labels.set(label, connectionDirective);
let stream = null;
if (connectionDirective.name === 'stream_connection_resolver') {
const initialCountArg = connectionDirective.args.find(
arg => arg.name === 'initial_count',
);
const ifArg = connectionDirective.args.find(arg => arg.name === 'if');
if (
initialCountArg == null ||
(initialCountArg.value.kind === 'Literal' &&
!Number.isInteger(initialCountArg.value.value))
) {
throw createUserError(
"Invalid use of @connection_resolver, 'initial_count' is required " +
"and must be an integer or variable of type 'Int!''.",
[initialCountArg?.loc ?? connectionDirective.loc],
);
}
stream = {
deferLabel: label,
initialCount: initialCountArg.value,
if: ifArg != null ? ifArg.value : null,
streamLabel: label,
};
}
const {EDGES, PAGE_INFO} = ConnectionInterface.get();
let edgeField: ?LinkedField;
let pageInfoField: ?LinkedField;
const selections = [];
transformed.selections.forEach(selection => {
if (
!(selection.kind === 'LinkedField' || selection.kind === 'ScalarField')
) {
throw createUserError(
'Invalid use of @connection_resolver, selections on the connection ' +
'must be linked or scalar fields.',
[selection.loc],
);
}
if (selection.kind === 'LinkedField') {
if (selection.name === EDGES) {
edgeField = selection;
} else if (selection.name === PAGE_INFO) {
pageInfoField = selection;
} else {
selections.push(selection);
}
} else {
selections.push(selection);
}
});
if (edgeField == null || pageInfoField == null) {
throw createUserError(
`Invalid use of @connection_resolver, fields '${EDGES}' and ` +
`'${PAGE_INFO}' must be fetched.`,
[connectionDirective.loc],
);
}
const connectionType = schema.getRawType(transformed.type);
const edgesFieldDef = schema.isObject(connectionType)
? schema.getFieldByName(schema.assertObjectType(connectionType), 'edges')
: null;
const edgesType =
edgesFieldDef != null
? schema.getRawType(schema.getFieldType(edgesFieldDef))
: null;
const nodeFieldDef =
edgesType != null && schema.isObject(edgesType)
? schema.getFieldByName(schema.assertObjectType(edgesType), 'node')
: null;
const nodeType =
nodeFieldDef != null
? schema.getRawType(schema.getFieldType(nodeFieldDef))
: null;
if (
edgesType == null ||
nodeType == null ||
!(
schema.isObject(nodeType) ||
schema.isInterface(nodeType) ||
schema.isUnion(nodeType)
)
) {
throw createUserError(
'Invalid usage of @connection_resolver, expected field to have shape ' +
"'field { edges { node { ...} } }'.",
[transformed.loc],
);
}
edgeField = {
...edgeField,
selections: [
...edgeField.selections,
{
alias: '__id',
args: [],
directives: [],
handles: null,
kind: 'ScalarField',
loc: edgeField.loc,
metadata: null,
name: '__id',
type: schema.assertScalarFieldType(
schema.getNonNullType(schema.expectIdType()),
),
},
{
alias: 'node',
args: [],
connection: false,
directives: [],
handles: null,
kind: 'LinkedField',
loc: edgeField.loc,
metadata: null,
name: 'node',
selections: [
{
alias: '__id',
args: [],
directives: [],
handles: null,
kind: 'ScalarField',
loc: edgeField.loc,
metadata: null,
name: '__id',
type: schema.assertScalarFieldType(
schema.getNonNullType(schema.expectIdType()),
),
},
],
type: schema.assertLinkedFieldType(nodeType),
},
],
};
selections.push(
({
args: transformed.args,
kind: 'Connection',
label,
loc: transformed.loc,
name: transformed.name,
selections: [edgeField, pageInfoField],
stream,
type: transformed.type,
}: Connection),
);
const connectionMetadata = buildConnectionMetadata(
transformed,
path,
stream != null,
);
state.connectionMetadata.push(connectionMetadata);
return {
alias: transformed.alias,
args: transformed.args,
directives: transformed.directives.filter(
directive => directive !== connectionDirective,
),
kind: 'ConnectionField',
loc: transformed.loc,
metadata: null,
name: transformed.name,
selections,
type: transformed.type,
};
}
function visitScalarField(field: ScalarField): ScalarField {
const connectionDirective = field.directives.find(
directive => directive.name === 'connection_resolver',
);
if (connectionDirective != null) {
throw createUserError(
'The @connection_resolver direction is not supported on scalar fields, ' +
'only fields returning an object/interface/union',
[connectionDirective.loc],
);
}
return field;
}
function getLiteralStringArgument(
directive: Directive,
argName: string,
): ?string {
const arg = directive.args.find(({name}) => name === argName);
if (arg == null) {
return null;
}
const value = arg.value.kind === 'Literal' ? arg.value.value : null;
if (value == null || typeof value !== 'string') {
throw createUserError(
`Expected the '${argName}' value to @${
directive.name
} to be a string literal if provided.`,
[arg.value.loc],
);
}
return value;
}
module.exports = {
SCHEMA_EXTENSION,
transform: connectionFieldTransform,
};