From 987b21f501bb3483d196720d4a252c25b704352e Mon Sep 17 00:00:00 2001 From: Jon Allured Date: Tue, 29 Mar 2022 13:36:14 -0500 Subject: [PATCH] Add very basic mutation for updating users --- _schemaV2.graphql | 13 +++++ .../loaders_with_authentication/gravity.ts | 5 ++ src/schema/v2/schema.ts | 2 + src/schema/v2/users/updateUserMutation.ts | 58 +++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/schema/v2/users/updateUserMutation.ts diff --git a/_schemaV2.graphql b/_schemaV2.graphql index 422fafc8f3..1ec7a56edd 100644 --- a/_schemaV2.graphql +++ b/_schemaV2.graphql @@ -9929,6 +9929,9 @@ type Mutation { updateSmsSecondFactor( input: UpdateSmsSecondFactorInput! ): UpdateSmsSecondFactorPayload + + # Update the user + updateUser(input: UpdateUserMutationInput!): UpdateUserMutationPayload updateUserAddress(input: UpdateUserAddressInput!): UpdateUserAddressPayload updateUserDefaultAddress( input: UpdateUserDefaultAddressInput! @@ -14133,6 +14136,16 @@ type UpdateUserDefaultAddressPayload { userAddressOrErrors: UserAddressOrErrorsUnion! } +input UpdateUserMutationInput { + clientMutationId: String + email: String! + id: String! +} + +type UpdateUserMutationPayload { + clientMutationId: String +} + # Autogenerated input type of UpdateViewingRoomArtworks input UpdateViewingRoomArtworksInput { artworks: [ViewingRoomArtworkInput!]! diff --git a/src/lib/loaders/loaders_with_authentication/gravity.ts b/src/lib/loaders/loaders_with_authentication/gravity.ts index 7bdaabfe97..ec4b0ca446 100644 --- a/src/lib/loaders/loaders_with_authentication/gravity.ts +++ b/src/lib/loaders/loaders_with_authentication/gravity.ts @@ -382,6 +382,11 @@ export default (accessToken, userID, opts) => { ), updateMeLoader: gravityLoader("me", {}, { method: "PUT" }), updateMyPasswordLoader: gravityLoader("me/password", {}, { method: "PUT" }), + updateUserLoader: gravityLoader( + (id) => `user/${id}`, + {}, + { method: "PUT" } + ), usersLoader: gravityLoader("users"), userByEmailLoader: gravityLoader("user", {}, { method: "GET" }), userByIDLoader: gravityLoader((id) => `user/${id}`, {}, { method: "GET" }), diff --git a/src/schema/v2/schema.ts b/src/schema/v2/schema.ts index a78cfa8f6a..81a3d2a285 100644 --- a/src/schema/v2/schema.ts +++ b/src/schema/v2/schema.ts @@ -75,6 +75,7 @@ import { sendConfirmationEmailMutation } from "./me/sendConfirmationEmailMutatio import UpdateCollectorProfile from "./me/update_collector_profile" import UpdateMyUserProfileMutation from "./me/update_me_mutation" import { updateMyPasswordMutation } from "./me/updateMyPasswordMutation" +import { updateUserMutation } from "./users/updateUserMutation" import { deleteCollectorProfileIconMutation } from "./me/deleteCollectorProfileIconMutation" import ObjectIdentification from "./object_identification" import { OrderedSet } from "./OrderedSet" @@ -260,6 +261,7 @@ export default new GraphQLSchema({ updateCollectorProfile: UpdateCollectorProfile, updateConversation: UpdateConversationMutation, updateMyPassword: updateMyPasswordMutation, + updateUser: updateUserMutation, updateMyUserProfile: UpdateMyUserProfileMutation, updateNotificationPreferences: updateNotificationPreferencesMutation, deleteMyUserProfileIcon: deleteCollectorProfileIconMutation, diff --git a/src/schema/v2/users/updateUserMutation.ts b/src/schema/v2/users/updateUserMutation.ts new file mode 100644 index 0000000000..7fddd6cbfd --- /dev/null +++ b/src/schema/v2/users/updateUserMutation.ts @@ -0,0 +1,58 @@ +import { GraphQLNonNull, GraphQLString } from "graphql" +import { mutationWithClientMutationId } from "graphql-relay" +import { ResolverContext } from "types/graphql" +import { snakeCase } from "lodash" + +interface Input { + id: string + email: string +} + +interface GravityInput { + id: string + email: string +} + +interface GravityError { + statusCode: number + body: { error?: string; text?: string; message?: string } +} + +export const updateUserMutation = mutationWithClientMutationId< + Input, + any | null, + ResolverContext +>({ + name: "UpdateUserMutation", + description: "Update the user", + inputFields: { + id: { type: new GraphQLNonNull(GraphQLString) }, + email: { type: new GraphQLNonNull(GraphQLString) }, + }, + outputFields: {}, + mutateAndGetPayload: async (args, { updateUserLoader }) => { + if (!updateUserLoader) { + throw new Error( + "You need to be signed in as an admin to perform this action" + ) + } + + const gravityOptions = Object.keys(args) + .filter((key) => key !== "id") + .reduce( + (acc, key) => ({ ...acc, [snakeCase(key)]: args[key] }), + {} as GravityInput + ) + + try { + return await updateUserLoader?.(args.id, gravityOptions) + } catch (err) { + if ("body" in (err as any)) { + const e = err as GravityError + throw new Error(e.body.text ?? e.body.error ?? e.body.message) + } + + throw err + } + }, +})