-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathconnection.js
154 lines (142 loc) · 4.13 KB
/
connection.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
/* @flow */
/**
* Copyright (c) 2015, 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.
*/
import {
GraphQLBoolean,
GraphQLInt,
GraphQLNonNull,
GraphQLList,
GraphQLObjectType,
GraphQLString
} from 'graphql';
import type {
GraphQLFieldConfigArgumentMap,
GraphQLFieldConfigMap,
GraphQLFieldResolver,
Thunk,
} from 'graphql';
/**
* Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field
* whose return type is a connection type with forward pagination.
*/
export const forwardConnectionArgs: GraphQLFieldConfigArgumentMap = {
after: {
type: GraphQLString
},
first: {
type: GraphQLInt
},
};
/**
* Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field
* whose return type is a connection type with backward pagination.
*/
export const backwardConnectionArgs: GraphQLFieldConfigArgumentMap = {
before: {
type: GraphQLString
},
last: {
type: GraphQLInt
},
};
/**
* Returns a GraphQLFieldConfigArgumentMap appropriate to include on a field
* whose return type is a connection type with bidirectional pagination.
*/
export const connectionArgs: GraphQLFieldConfigArgumentMap = {
...forwardConnectionArgs,
...backwardConnectionArgs,
};
type ConnectionConfig = {
name?: ?string,
nodeType: GraphQLObjectType,
resolveNode?: ?GraphQLFieldResolver<*, *>,
resolveCursor?: ?GraphQLFieldResolver<*, *>,
edgeFields?: ?Thunk<GraphQLFieldConfigMap<*, *>>,
connectionFields?: ?Thunk<GraphQLFieldConfigMap<*, *>>,
};
type GraphQLConnectionDefinitions = {
edgeType: GraphQLObjectType,
connectionType: GraphQLObjectType
};
function resolveMaybeThunk<T>(thingOrThunk: Thunk<T>): T {
return typeof thingOrThunk === 'function' ? thingOrThunk() : thingOrThunk;
}
/**
* Returns a GraphQLObjectType for a connection with the given name,
* and whose nodes are of the specified type.
*/
export function connectionDefinitions(
config: ConnectionConfig
): GraphQLConnectionDefinitions {
const {nodeType} = config;
const name = config.name || nodeType.name;
const edgeFields = config.edgeFields || {};
const connectionFields = config.connectionFields || {};
const resolveNode = config.resolveNode;
const resolveCursor = config.resolveCursor;
const edgeType = new GraphQLObjectType({
name: name + 'Edge',
description: 'An edge in a connection.',
fields: () => ({
node: {
type: nodeType,
resolve: resolveNode,
description: 'The item at the end of the edge',
},
cursor: {
type: new GraphQLNonNull(GraphQLString),
resolve: resolveCursor,
description: 'A cursor for use in pagination'
},
...(resolveMaybeThunk(edgeFields): any)
}),
});
const connectionType = new GraphQLObjectType({
name: name + 'Connection',
description: 'A connection to a list of items.',
fields: () => ({
pageInfo: {
type: new GraphQLNonNull(pageInfoType),
description: 'Information to aid in pagination.'
},
edges: {
type: new GraphQLList(edgeType),
description: 'A list of edges.'
},
...(resolveMaybeThunk(connectionFields): any)
}),
});
return {edgeType, connectionType};
}
/**
* The common page info type used by all connections.
*/
const pageInfoType = new GraphQLObjectType({
name: 'PageInfo',
description: 'Information about pagination in a connection.',
fields: () => ({
hasNextPage: {
type: new GraphQLNonNull(GraphQLBoolean),
description: 'When paginating forwards, are there more items?'
},
hasPreviousPage: {
type: new GraphQLNonNull(GraphQLBoolean),
description: 'When paginating backwards, are there more items?'
},
startCursor: {
type: GraphQLString,
description: 'When paginating backwards, the cursor to continue.'
},
endCursor: {
type: GraphQLString,
description: 'When paginating forwards, the cursor to continue.'
},
})
});