-
-
Notifications
You must be signed in to change notification settings - Fork 569
/
createConnectionGqlField.ts
369 lines (341 loc) 路 12.8 KB
/
createConnectionGqlField.ts
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
358
359
360
361
362
363
364
365
366
367
368
369
import {
GraphQLScalarType,
GraphQLNonNull,
GraphQLObjectType,
GraphQLFieldConfig,
GraphQLArgumentConfig,
GraphQLEnumType,
GraphQLEnumValueConfig,
GraphQLList,
GraphQLBoolean,
GraphQLInt,
Kind,
} from 'graphql'
import { Paginator } from '../../../interface'
import { buildObject, formatName, memoize2, scrib } from '../../utils'
import getGqlType from '../getGqlType'
import BuildToken from '../BuildToken'
// TODO: doc
export default function createConnectionGqlField <TSource, TInput, TItemValue>(
buildToken: BuildToken,
paginator: Paginator<TInput, TItemValue>,
config: {
description?: string,
inputArgEntries?: Array<[string, GraphQLArgumentConfig<mixed>]>,
getPaginatorInput: (source: TSource, args: { [key: string]: mixed }) => TInput,
},
): GraphQLFieldConfig<TSource, Connection<TInput, TItemValue, mixed>> {
const gqlType = getGqlType(buildToken, paginator.itemType, false)
// This is the type of all the connection arguments.
type ConnectionArgs<TCursor> = {
orderBy: string,
before?: NamespacedCursor<TCursor>,
after?: NamespacedCursor<TCursor>,
first?: number,
last?: number,
offset?: number,
}
return {
description: config.description || `Reads and enables paginatation through a set of ${scrib.type(gqlType)}.`,
type: getConnectionGqlType(buildToken, paginator),
args: buildObject<GraphQLArgumentConfig<mixed>>([
// Only include an `orderBy` field if there are ways in which we can
// order.
paginator.orderings && paginator.orderings.size > 0 && ['orderBy', createOrderByGqlArg(buildToken, paginator)],
['before', {
description: 'Read all values in the set before (above) this cursor.',
type: _cursorType,
}],
['after', {
description: 'Read all values in the set after (below) this cursor.',
type: _cursorType,
}],
['first', {
description: 'Only read the first `n` values of the set.',
type: GraphQLInt,
}],
['last', {
description: 'Only read the last `n` values of the set.',
type: GraphQLInt,
}],
['offset', {
description: 'Skip the first `n` values from our `after` cursor, an alternative to cursor based pagination. May not be used with `last`.',
type: GraphQLInt,
}],
// Add all of the field entries that will eventually make up our
// condition.
...(config.inputArgEntries ? config.inputArgEntries : []),
]),
// Note that this resolver is an arrow function. This is so that we can
// keep the correct `this` reference.
async resolve <TCursor>(
source: TSource,
args: ConnectionArgs<TCursor>,
context: mixed,
): Promise<Connection<TInput, TItemValue, TCursor>> {
const {
orderBy: orderingName,
before: beforeCursor,
after: afterCursor,
first,
last,
offset: _offset,
} = args
// Throw an error if the user is trying to use a cursor from another
// ordering. Note that if no ordering is defined we expect the
// `orderingName` to be `null`. This is because when we deserialize the
// base64 encoded JSON any undefineds will become nulls.
if (beforeCursor && beforeCursor.orderingName !== orderingName)
throw new Error('`before` cursor can not be used for this `orderBy` value.')
if (afterCursor && afterCursor.orderingName !== orderingName)
throw new Error('`after` cursor can not be used for this `orderBy` value.')
// Don鈥檛 allow the use of cursors with `offset`.
if (beforeCursor != null && _offset != null || beforeCursor != null && _offset != null)
throw new Error('Cannot use `before`/`after` cursors with `offset`!')
// Get our input.
const input = config.getPaginatorInput(source, args)
// Construct the page config.
const pageConfig: Paginator.PageConfig<TCursor> = {
beforeCursor: beforeCursor && beforeCursor.cursor,
afterCursor: afterCursor && afterCursor.cursor,
first,
last,
_offset,
}
// Get our ordering.
const ordering = paginator.orderings.get(orderingName) as Paginator.Ordering<TInput, TItemValue, TCursor>
// Finally, actually get the page data.
const page = await ordering.readPage(context, input, pageConfig)
return {
paginator,
orderingName,
input,
page,
}
},
}
}
const getConnectionGqlType = memoize2(_createConnectionGqlType)
/**
* Creates a concrete GraphQL connection object type.
*/
export function _createConnectionGqlType <TInput, TItemValue>(
buildToken: BuildToken,
paginator: Paginator<TInput, TItemValue>,
): GraphQLObjectType<Connection<TInput, TItemValue, mixed>> {
const gqlType = getGqlType(buildToken, paginator.itemType, false)
const gqlEdgeType = getEdgeGqlType(buildToken, paginator)
return new GraphQLObjectType<Connection<TInput, TItemValue, mixed>>({
name: formatName.type(`${paginator.name}-connection`),
description: `A connection to a list of ${scrib.type(gqlType)} values.`,
fields: () => ({
pageInfo: {
description: 'Information to aid in pagination.',
type: new GraphQLNonNull(_pageInfoType),
resolve: source => source,
},
totalCount: {
description: `The count of *all* ${scrib.type(gqlType)} you could get from the connection.`,
type: GraphQLInt,
resolve: ({ input }, _args, context) =>
paginator.count(context, input),
},
edges: {
description: `A list of edges which contains the ${scrib.type(gqlType)} and cursor to aid in pagination.`,
type: new GraphQLList(gqlEdgeType),
resolve: <TCursor>({ orderingName, page }: Connection<TInput, TItemValue, TCursor>): Array<Edge<TInput, TItemValue, TCursor>> =>
page.values.map(({ cursor, value }) => ({ paginator, orderingName, cursor, value })),
},
nodes: {
description: `A list of ${scrib.type(gqlType)} objects.`,
type: new GraphQLList(gqlType),
resolve: ({ page }): Array<TItemValue> =>
page.values.map(({ value }) => value),
},
}),
})
}
export const getEdgeGqlType = memoize2(_createEdgeGqlType)
/**
* Creates a concrete GraphQL edge object type.
*/
export function _createEdgeGqlType <TInput, TItemValue>(
buildToken: BuildToken,
paginator: Paginator<TInput, TItemValue>,
): GraphQLObjectType<Edge<TInput, TItemValue, mixed>> {
const gqlType = getGqlType(buildToken, paginator.itemType, false)
return new GraphQLObjectType<Edge<TInput, TItemValue, mixed>>({
name: formatName.type(`${paginator.name}-edge`),
description: `A ${scrib.type(gqlType)} edge in the connection.`,
fields: () => ({
cursor: {
description: 'A cursor for use in pagination.',
type: _cursorType,
resolve: <TCursor>({ orderingName, cursor }: Edge<TInput, TItemValue, TCursor>): NamespacedCursor<TCursor> | null =>
cursor && { orderingName, cursor },
},
node: {
description: `The ${scrib.type(gqlType)} at the end of the edge.`,
type: gqlType,
resolve: ({ value }) => value,
},
}),
})
}
/**
* Creates an argument for the `orderBy` field. The argument will be a correct
* ordering value for the paginator.
*/
export function createOrderByGqlArg <TInput, TItemValue>(
buildToken: BuildToken,
paginator: Paginator<TInput, TItemValue>,
): GraphQLArgumentConfig<string> {
const gqlType = getGqlType(buildToken, paginator.itemType, false)
const enumType = getOrderByGqlEnumType(buildToken, paginator)
return {
description: `The method to use when ordering ${scrib.type(gqlType)}.`,
type: enumType,
defaultValue: Array.from(paginator.orderings).find(([, ordering]) => ordering === paginator.defaultOrdering)![0],
}
}
const _getOrderByGqlEnumType = memoize2(_createOrderByGqlEnumType)
// We use a second `getOrderByEnumType` so we can maintain the function
// prototype which gets mangled in memoization.
function getOrderByGqlEnumType <TInput, TItemValue>(
buildToken: BuildToken,
paginator: Paginator<TInput, TItemValue>,
): GraphQLEnumType<string> {
return _getOrderByGqlEnumType(buildToken, paginator)
}
/**
* Creates a GraphQL type which can be used by the user to select an ordering
* strategy.
*/
export function _createOrderByGqlEnumType <TInput, TItemValue>(
buildToken: BuildToken,
paginator: Paginator<TInput, TItemValue>,
): GraphQLEnumType<string> {
const gqlType = getGqlType(buildToken, paginator.itemType, false)
return new GraphQLEnumType({
name: formatName.type(`${paginator.name}-order-by`),
description: `Methods to use when ordering ${scrib.type(gqlType)}.`,
values: buildObject(
Array.from(paginator.orderings)
.map<[string, GraphQLEnumValueConfig<string>]>(
ordering => [formatName.enumValue(ordering[0]), { value: ordering[0] }],
),
),
})
}
/**
* The cursor type is a scalar string type that represents a single edge in a
* connection.
*
* @private
*/
export const _cursorType: GraphQLScalarType<NamespacedCursor<mixed>> =
new GraphQLScalarType<NamespacedCursor<mixed>>({
name: 'Cursor',
description: 'A location in a connection that can be used for resuming pagination.',
serialize: value => serializeCursor(value),
parseValue: value => typeof value === 'string' ? deserializeCursor(value) : null,
parseLiteral: ast => ast.kind === Kind.STRING ? deserializeCursor(ast.value) : null,
})
/**
* Takes a namespaced cursor and serializes it into a base64 encoded
* string.
*
* @private
*/
function serializeCursor ({ orderingName, cursor }: NamespacedCursor<mixed>): string {
return new Buffer(JSON.stringify([orderingName, cursor])).toString('base64')
}
/**
* Deserializes a base64 encoded namespace cursor into the correct data type.
*
* @private
*/
function deserializeCursor (serializedCursor: string): NamespacedCursor<mixed> {
const [orderingName, cursor] = JSON.parse(new Buffer(serializedCursor, 'base64').toString())
return { orderingName, cursor }
}
/**
* The page info type contains information about the current page of results
* returned by the connection.
*
* @private
*/
export const _pageInfoType: GraphQLObjectType<Connection<mixed, mixed, mixed>> =
new GraphQLObjectType<Connection<mixed, mixed, mixed>>({
name: 'PageInfo',
description: 'Information about pagination in a connection.',
fields: {
hasNextPage: {
description: 'When paginating forwards, are there more items?',
type: new GraphQLNonNull(GraphQLBoolean),
resolve: ({ page }) => page.hasNextPage(),
},
hasPreviousPage: {
description: 'When paginating backwards, are there more items?',
type: new GraphQLNonNull(GraphQLBoolean),
resolve: ({ page }) => page.hasPreviousPage(),
},
startCursor: {
description: 'When paginating backwards, the cursor to continue.',
type: _cursorType,
resolve: ({ orderingName, page }): NamespacedCursor<mixed> | null =>
page.values[0]
? { orderingName, cursor: page.values[0].cursor }
: null,
},
endCursor: {
description: 'When paginating forwards, the cursor to continue.',
type: _cursorType,
resolve: ({ orderingName, page }): NamespacedCursor<mixed> | null =>
page.values[page.values.length - 1]
? { orderingName, cursor: page.values[page.values.length - 1].cursor }
: null,
},
},
})
/**
* This is the type for the value of all connection types. It contains the
* paginator for identification, ordering for cursor serialization, condition
* for fetching the correct count, and of course the actual page of data.
*
* @private
*/
interface Connection<TInput, TItemValue, TCursor> {
paginator: Paginator<TInput, TItemValue>
orderingName: string
input: TInput
page: Paginator.Page<TItemValue, TCursor>
}
/**
* This is the type for the value of all edge types. Similarly to the
* connection value it has a paginator for identification, ordering for cursor
* serialization, and of course a value and its associated cursor.
*
* @private
*/
interface Edge<TInput, TItemValue, TCursor> {
paginator: Paginator<TInput, TItemValue>
orderingName: string
cursor: TCursor | null
value: TItemValue
}
/**
* A namespaced cursor is a cursor with identifying information. A cursor that
* was returned by one paginator can not be used by another paginator and a
* cursor created with one ordering can not be used with another ordering.
* Therefore we need to serialize this information with our cursor to make the
* appropriate checks and ensure the cursor is valid for our resolve context.
*
* @private
*/
type NamespacedCursor<TCursor> = {
// TODO: Evaluate if we really want to enforce paginator name checks.
// paginatorName: string,
orderingName: string | null,
cursor: TCursor,
}