mutation.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. exports.mutationWithClientMutationId = mutationWithClientMutationId;
  6. var _graphql = require('graphql');
  7. /**
  8. * A description of a mutation consumable by mutationWithClientMutationId
  9. * to create a GraphQLFieldConfig for that mutation.
  10. *
  11. * The inputFields and outputFields should not include `clientMutationId`,
  12. * as this will be provided automatically.
  13. *
  14. * An input object will be created containing the input fields, and an
  15. * object will be created containing the output fields.
  16. *
  17. * mutateAndGetPayload will receive an Object with a key for each
  18. * input field, and it should return an Object with a key for each
  19. * output field. It may return synchronously, or return a Promise.
  20. */
  21. /**
  22. * Returns a GraphQLFieldConfig for the mutation described by the
  23. * provided MutationConfig.
  24. */
  25. function mutationWithClientMutationId(config) {
  26. const { name, inputFields, outputFields, mutateAndGetPayload } = config;
  27. const augmentedInputFields = () => ({
  28. ...(0, _graphql.resolveObjMapThunk)(inputFields),
  29. clientMutationId: {
  30. type: _graphql.GraphQLString,
  31. },
  32. });
  33. const augmentedOutputFields = () => ({
  34. ...(0, _graphql.resolveObjMapThunk)(outputFields),
  35. clientMutationId: {
  36. type: _graphql.GraphQLString,
  37. },
  38. });
  39. const outputType = new _graphql.GraphQLObjectType({
  40. name: name + 'Payload',
  41. fields: augmentedOutputFields,
  42. });
  43. const inputType = new _graphql.GraphQLInputObjectType({
  44. name: name + 'Input',
  45. fields: augmentedInputFields,
  46. });
  47. return {
  48. type: outputType,
  49. description: config.description,
  50. deprecationReason: config.deprecationReason,
  51. extensions: config.extensions,
  52. args: {
  53. input: {
  54. type: new _graphql.GraphQLNonNull(inputType),
  55. },
  56. },
  57. resolve: (_, { input }, context, info) => {
  58. const { clientMutationId } = input;
  59. const payload = mutateAndGetPayload(input, context, info);
  60. if (isPromiseLike(payload)) {
  61. return payload.then(injectClientMutationId);
  62. }
  63. return injectClientMutationId(payload);
  64. function injectClientMutationId(data) {
  65. if (typeof data === 'object' && data !== null) {
  66. // @ts-expect-error FIXME It's bad idea to mutate data but we need to pass clientMutationId somehow. Maybe in future we figure out better solution satisfying all our test cases.
  67. data.clientMutationId = clientMutationId;
  68. }
  69. return data;
  70. }
  71. },
  72. };
  73. }
  74. // FIXME: Temporary until graphql-js resolves this issue
  75. // See, https://github.com/graphql/graphql-js/pull/3243#issuecomment-919510590
  76. function isPromiseLike(value) {
  77. return (
  78. typeof (value === null || value === void 0 ? void 0 : value.then) ===
  79. 'function'
  80. );
  81. }