node.js 3.3 KB

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