UNPKG

3.37 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true,
5});
6exports.nodeDefinitions = nodeDefinitions;
7exports.toGlobalId = toGlobalId;
8exports.fromGlobalId = fromGlobalId;
9exports.globalIdField = globalIdField;
10var _graphql = require('graphql');
11var _base = require('../utils/base64');
12/**
13 * Given a function to map from an ID to an underlying object, and a function
14 * to map from an underlying object to the concrete GraphQLObjectType it
15 * corresponds to, constructs a `Node` interface that objects can implement,
16 * and a field config for a `node` root field.
17 *
18 * If the typeResolver is omitted, object resolution on the interface will be
19 * handled with the `isTypeOf` method on object types, as with any GraphQL
20 * interface without a provided `resolveType` method.
21 */
22function nodeDefinitions(fetchById, typeResolver) {
23 const nodeInterface = new _graphql.GraphQLInterfaceType({
24 name: 'Node',
25 description: 'An object with an ID',
26 fields: () => ({
27 id: {
28 type: new _graphql.GraphQLNonNull(_graphql.GraphQLID),
29 description: 'The id of the object.',
30 },
31 }),
32 resolveType: typeResolver,
33 });
34 const nodeField = {
35 description: 'Fetches an object given its ID',
36 type: nodeInterface,
37 args: {
38 id: {
39 type: new _graphql.GraphQLNonNull(_graphql.GraphQLID),
40 description: 'The ID of an object',
41 },
42 },
43 resolve: (_obj, { id }, context, info) => fetchById(id, context, info),
44 };
45 const nodesField = {
46 description: 'Fetches objects given their IDs',
47 type: new _graphql.GraphQLNonNull(new _graphql.GraphQLList(nodeInterface)),
48 args: {
49 ids: {
50 type: new _graphql.GraphQLNonNull(
51 new _graphql.GraphQLList(
52 new _graphql.GraphQLNonNull(_graphql.GraphQLID),
53 ),
54 ),
55 description: 'The IDs of objects',
56 },
57 },
58 resolve: (_obj, { ids }, context, info) =>
59 ids.map((id) => fetchById(id, context, info)),
60 };
61 return {
62 nodeInterface,
63 nodeField,
64 nodesField,
65 };
66}
67/**
68 * Takes a type name and an ID specific to that type name, and returns a
69 * "global ID" that is unique among all types.
70 */
71function toGlobalId(type, id) {
72 return (0, _base.base64)([type, _graphql.GraphQLID.serialize(id)].join(':'));
73}
74
75/**
76 * Takes the "global ID" created by toGlobalID, and returns the type name and ID
77 * used to create it.
78 */
79function fromGlobalId(globalId) {
80 const unbasedGlobalId = (0, _base.unbase64)(globalId);
81 const delimiterPos = unbasedGlobalId.indexOf(':');
82 return {
83 type: unbasedGlobalId.substring(0, delimiterPos),
84 id: unbasedGlobalId.substring(delimiterPos + 1),
85 };
86}
87
88/**
89 * Creates the configuration for an id field on a node, using `toGlobalId` to
90 * construct the ID from the provided typename. The type-specific ID is fetched
91 * by calling idFetcher on the object, or if not provided, by accessing the `id`
92 * property on the object.
93 */
94function globalIdField(typeName, idFetcher) {
95 return {
96 description: 'The ID of an object',
97 type: new _graphql.GraphQLNonNull(_graphql.GraphQLID),
98 resolve: (obj, _args, context, info) =>
99 toGlobalId(
100 typeName !== null && typeName !== void 0
101 ? typeName
102 : info.parentType.name,
103 idFetcher ? idFetcher(obj, context, info) : obj.id,
104 ),
105 };
106}