Refactor CreateComment resolver

- extract out the validations for a valid comment
This commit is contained in:
Matt Rider 2019-07-12 08:07:07 -03:00
parent bd77c8e450
commit ba7e44a762
2 changed files with 31 additions and 25 deletions

View File

@ -1,6 +1,9 @@
import { UserInputError } from 'apollo-server' import { UserInputError } from 'apollo-server'
import Joi from '@hapi/joi' import Joi from '@hapi/joi'
const COMMENT_MIN_LENGTH = 1
const NO_POST_ERR_MESSAGE = 'Comment cannot be created without a post!'
const validate = schema => { const validate = schema => {
return async (resolve, root, args, context, info) => { return async (resolve, root, args, context, info) => {
const validation = schema.validate(args) const validation = schema.validate(args)
@ -15,8 +18,36 @@ const socialMediaSchema = Joi.object().keys({
.required(), .required(),
}) })
const validateCommentCreation = async (resolve, root, args, context, info) => {
const content = args.content.replace(/<(?:.|\n)*?>/gm, '').trim()
const { postId } = args
if (!args.content || content.length < COMMENT_MIN_LENGTH) {
throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} character long!`)
}
const session = context.driver.session()
const postQueryRes = await session.run(
`
MATCH (post:Post {id: $postId})
RETURN post`,
{
postId,
},
)
const [post] = postQueryRes.records.map(record => {
return record.get('post')
})
if (!post) {
throw new UserInputError(NO_POST_ERR_MESSAGE)
} else {
return resolve(root, args, context, info)
}
}
export default { export default {
Mutation: { Mutation: {
CreateSocialMedia: validate(socialMediaSchema), CreateSocialMedia: validate(socialMediaSchema),
CreateComment: validateCommentCreation,
}, },
} }

View File

@ -1,40 +1,15 @@
import { neo4jgraphql } from 'neo4j-graphql-js' import { neo4jgraphql } from 'neo4j-graphql-js'
import { UserInputError } from 'apollo-server'
const COMMENT_MIN_LENGTH = 1
const NO_POST_ERR_MESSAGE = 'Comment cannot be created without a post!'
export default { export default {
Mutation: { Mutation: {
CreateComment: async (object, params, context, resolveInfo) => { CreateComment: async (object, params, context, resolveInfo) => {
const content = params.content.replace(/<(?:.|\n)*?>/gm, '').trim()
const { postId } = params const { postId } = params
// Adding relationship from comment to post by passing in the postId, // Adding relationship from comment to post by passing in the postId,
// but we do not want to create the comment with postId as an attribute // but we do not want to create the comment with postId as an attribute
// because we use relationships for this. So, we are deleting it from params // because we use relationships for this. So, we are deleting it from params
// before comment creation. // before comment creation.
delete params.postId delete params.postId
if (!params.content || content.length < COMMENT_MIN_LENGTH) {
throw new UserInputError(`Comment must be at least ${COMMENT_MIN_LENGTH} character long!`)
}
const session = context.driver.session() const session = context.driver.session()
const postQueryRes = await session.run(
`
MATCH (post:Post {id: $postId})
RETURN post`,
{
postId,
},
)
const [post] = postQueryRes.records.map(record => {
return record.get('post')
})
if (!post) {
throw new UserInputError(NO_POST_ERR_MESSAGE)
}
const commentWithoutRelationships = await neo4jgraphql( const commentWithoutRelationships = await neo4jgraphql(
object, object,
params, params,