Skip to content

Commit

Permalink
Add very basic mutation for updating users (#3918)
Browse files Browse the repository at this point in the history
Add very basic mutation for updating users
  • Loading branch information
artsy-peril[bot] committed Mar 29, 2022
2 parents e3021e9 + 987b21f commit 7c85403
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 0 deletions.
13 changes: 13 additions & 0 deletions _schemaV2.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9929,6 +9929,9 @@ type Mutation {
updateSmsSecondFactor(
input: UpdateSmsSecondFactorInput!
): UpdateSmsSecondFactorPayload

# Update the user
updateUser(input: UpdateUserMutationInput!): UpdateUserMutationPayload
updateUserAddress(input: UpdateUserAddressInput!): UpdateUserAddressPayload
updateUserDefaultAddress(
input: UpdateUserDefaultAddressInput!
Expand Down Expand Up @@ -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!]!
Expand Down
5 changes: 5 additions & 0 deletions src/lib/loaders/loaders_with_authentication/gravity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" }),
Expand Down
2 changes: 2 additions & 0 deletions src/schema/v2/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -260,6 +261,7 @@ export default new GraphQLSchema({
updateCollectorProfile: UpdateCollectorProfile,
updateConversation: UpdateConversationMutation,
updateMyPassword: updateMyPasswordMutation,
updateUser: updateUserMutation,
updateMyUserProfile: UpdateMyUserProfileMutation,
updateNotificationPreferences: updateNotificationPreferencesMutation,
deleteMyUserProfileIcon: deleteCollectorProfileIconMutation,
Expand Down
58 changes: 58 additions & 0 deletions src/schema/v2/users/updateUserMutation.ts
Original file line number Diff line number Diff line change
@@ -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
}
},
})

0 comments on commit 7c85403

Please sign in to comment.