Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add very basic mutation for updating users #3918

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines +49 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jonallured - curious how you landed on this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok good. find damon, copy 😄

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha pretty much!


throw err
}
},
})