123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- 'use strict';
- Object.defineProperty(exports, '__esModule', {
- value: true,
- });
- exports.nodeDefinitions = nodeDefinitions;
- exports.toGlobalId = toGlobalId;
- exports.fromGlobalId = fromGlobalId;
- exports.globalIdField = globalIdField;
- var _graphql = require('graphql');
- var _base = require('../utils/base64');
- function nodeDefinitions(fetchById, typeResolver) {
- const nodeInterface = new _graphql.GraphQLInterfaceType({
- name: 'Node',
- description: 'An object with an ID',
- fields: () => ({
- id: {
- type: new _graphql.GraphQLNonNull(_graphql.GraphQLID),
- description: 'The id of the object.',
- },
- }),
- resolveType: typeResolver,
- });
- const nodeField = {
- description: 'Fetches an object given its ID',
- type: nodeInterface,
- args: {
- id: {
- type: new _graphql.GraphQLNonNull(_graphql.GraphQLID),
- description: 'The ID of an object',
- },
- },
- resolve: (_obj, { id }, context, info) => fetchById(id, context, info),
- };
- const nodesField = {
- description: 'Fetches objects given their IDs',
- type: new _graphql.GraphQLNonNull(new _graphql.GraphQLList(nodeInterface)),
- args: {
- ids: {
- type: new _graphql.GraphQLNonNull(
- new _graphql.GraphQLList(
- new _graphql.GraphQLNonNull(_graphql.GraphQLID),
- ),
- ),
- description: 'The IDs of objects',
- },
- },
- resolve: (_obj, { ids }, context, info) =>
- ids.map((id) => fetchById(id, context, info)),
- };
- return {
- nodeInterface,
- nodeField,
- nodesField,
- };
- }
- function toGlobalId(type, id) {
- return (0, _base.base64)([type, _graphql.GraphQLID.serialize(id)].join(':'));
- }
- function fromGlobalId(globalId) {
- const unbasedGlobalId = (0, _base.unbase64)(globalId);
- const delimiterPos = unbasedGlobalId.indexOf(':');
- return {
- type: unbasedGlobalId.substring(0, delimiterPos),
- id: unbasedGlobalId.substring(delimiterPos + 1),
- };
- }
- function globalIdField(typeName, idFetcher) {
- return {
- description: 'The ID of an object',
- type: new _graphql.GraphQLNonNull(_graphql.GraphQLID),
- resolve: (obj, _args, context, info) =>
- toGlobalId(
- typeName !== null && typeName !== void 0
- ? typeName
- : info.parentType.name,
- idFetcher ? idFetcher(obj, context, info) : obj.id,
- ),
- };
- }
|