UNPKG

3.67 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5function createRelayHelpers(t) {
6 function nodeDefinitions(idFetcher) {
7 const nodeInterface = t.interfaceType({
8 name: 'Node',
9 description: 'An object with an ID',
10 fields: () => [
11 t.abstractField('id', t.NonNull(t.ID), {
12 description: 'The id of the object.',
13 }),
14 ],
15 });
16 const nodeField = t.field('node', {
17 type: nodeInterface,
18 args: {
19 id: t.arg(t.NonNullInput(t.ID), 'The ID of an object'),
20 },
21 resolve: (_src, { id }, context, info) => idFetcher(id, context, info),
22 });
23 return { nodeInterface, nodeField };
24 }
25 const forwardConnectionArgs = {
26 after: t.arg(t.String),
27 first: t.arg(t.Int),
28 };
29 const backwardConnectionArgs = {
30 before: t.arg(t.String),
31 last: t.arg(t.Int),
32 };
33 const connectionArgs = {
34 ...forwardConnectionArgs,
35 ...backwardConnectionArgs,
36 };
37 const pageInfoType = t.objectType({
38 name: 'PageInfo',
39 description: 'Information about pagination in a connection.',
40 fields: () => [
41 t.defaultField('hasNextPage', t.NonNull(t.Boolean), {
42 description: 'When paginating forwards, are there more items?',
43 }),
44 t.defaultField('hasPreviousPage', t.NonNull(t.Boolean), {
45 description: 'When paginating backwards, are there more items?',
46 }),
47 t.defaultField('startCursor', t.String, {
48 description: 'When paginating backwards, the cursor to continue.',
49 }),
50 t.defaultField('endCursor', t.String, {
51 description: 'When paginating forwards, the cursor to continue.',
52 }),
53 ],
54 });
55 /**
56 * Returns ObjectTypes for a connection with the given name,
57 * and whose nodes are of the specified type.
58 */
59 function connectionDefinitions(config) {
60 const { nodeType } = config;
61 const name = config.name || nodeType.name;
62 // TODO
63 const edgeFields = config.edgeFields || (() => []);
64 const connectionFields = config.connectionFields || (() => []);
65 const edgeType = t.objectType({
66 name: name + 'Edge',
67 description: 'An edge in a connection.',
68 fields: () => [
69 t.defaultField('node', t.NonNull(nodeType), {
70 description: 'The item at the end of the edge',
71 }),
72 t.defaultField('cursor', t.NonNull(t.String), {
73 description: 'A cursor for use in pagination',
74 }),
75 ...edgeFields(),
76 ],
77 });
78 const connectionType = t.objectType({
79 name: name + 'Connection',
80 description: 'A connection to a list of items.',
81 fields: () => [
82 t.defaultField('pageInfo', t.NonNull(pageInfoType), {
83 description: 'Information to aid in pagination.',
84 }),
85 t.defaultField('edges', t.List(edgeType), {
86 description: 'A list of edges.',
87 }),
88 ...connectionFields(),
89 ],
90 });
91 return { edgeType, connectionType };
92 }
93 return {
94 nodeDefinitions,
95 forwardConnectionArgs,
96 backwardConnectionArgs,
97 connectionArgs,
98 pageInfoType,
99 connectionDefinitions,
100 };
101}
102
103exports.createRelayHelpers = createRelayHelpers;