1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, '__esModule', {
|
4 | value: true,
|
5 | });
|
6 | exports.nodeDefinitions = nodeDefinitions;
|
7 | exports.toGlobalId = toGlobalId;
|
8 | exports.fromGlobalId = fromGlobalId;
|
9 | exports.globalIdField = globalIdField;
|
10 | var _graphql = require('graphql');
|
11 | var _base = require('../utils/base64');
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | function 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 |
|
69 |
|
70 |
|
71 | function toGlobalId(type, id) {
|
72 | return (0, _base.base64)([type, _graphql.GraphQLID.serialize(id)].join(':'));
|
73 | }
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | function 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 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | function 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 | }
|