Created
May 18, 2020 14:53
-
-
Save sibelius/353127006347040cbef0e594ec6dfeaa to your computer and use it in GitHub Desktop.
createEditSingleFieldMutation function that generate an edit single field mutation for a given model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GraphQLID, GraphQLNonNull, GraphQLNullableType } from 'graphql'; | |
import { mutationWithClientMutationId } from 'graphql-relay'; | |
import { Model } from 'mongoose'; | |
import { errorField } from '../fields/errorField'; | |
import { successField } from '../fields/successField'; | |
import { getObjectId } from '../getObjectId'; | |
type MutationArgs = { | |
id: string; | |
}; | |
type CreateEditSingleFieldArgs<Context> = { | |
name: string; | |
model: Model<any>; | |
clearCache: (context: Context, id: string) => void; | |
notFoundMessage: (context: Context) => string; | |
successMessage: (context: Context) => string; | |
fieldName: string; | |
fieldType: GraphQLNullableType; | |
outputFields: object; | |
process: (args: MutationArgs, context: Context, item: Model<any>) => Promise<void>; | |
}; | |
export const createEditSingleFieldMutation = <Context extends object>({ | |
name, | |
model, | |
clearCache, | |
notFoundMessage, | |
successMessage, | |
fieldName, | |
fieldType, | |
outputFields, | |
process, | |
}: CreateEditSingleFieldArgs<Context>) => { | |
const mutation = mutationWithClientMutationId({ | |
name, | |
inputFields: { | |
id: { | |
type: GraphQLNonNull(GraphQLID), | |
}, | |
[fieldName]: { | |
type: GraphQLNonNull(fieldType), | |
}, | |
}, | |
mutateAndGetPayload: async (args: MutationArgs, context: Context) => { | |
const item = await model.findOne({ | |
_id: getObjectId(args.id), | |
}); | |
if (!item) { | |
return { | |
error: notFoundMessage ? notFoundMessage(context) : context.t('Item not found'), | |
}; | |
} | |
await model.updateOne( | |
{ | |
_id: item._id, | |
}, | |
{ | |
$set: { | |
[fieldName]: args[fieldName], | |
}, | |
}, | |
); | |
clearCache(context, item._id); | |
if (process) { | |
await process(args, context, item); | |
} | |
return { | |
id: item._id, | |
error: null, | |
success: successMessage ? successMessage(context) : context.t('Successfully edited'), | |
}; | |
}, | |
outputFields: { | |
...errorField, | |
...successField, | |
...outputFields, | |
}, | |
}); | |
return mutation; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mutation = createEditSingleFieldMutation({ | |
name: 'UserEditName', | |
model: User, | |
fieldName: 'title', | |
fieldType: GraphQLString, | |
clearCache: UserLoader.clearCache, | |
notFoundMessage: ({ t }) => t('User not found'), | |
successMessage: ({ t }) => t('User edited successfully'), | |
outputFields: { | |
user: { | |
type: UserType, | |
resolve: ({ id }, _, context) => UserLoader.load(context, id), | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment