Created
December 15, 2017 06:21
-
-
Save Khangeldy/0de9363bdfe2626bb3f5cc6bca14a0b2 to your computer and use it in GitHub Desktop.
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 { | |
GraphQLNonNull, | |
GraphQLObjectType, | |
GraphQLInt, | |
GraphQLString, | |
GraphQLID, | |
} from 'graphql'; | |
import { mutationWithClientMutationId, fromGlobalId } from 'graphql-relay'; | |
import validator from 'validator'; | |
import db from '../../db'; | |
import answerType from '../types/answerType'; | |
import ValidationError from '../ValidationError'; | |
import { deletedOutput } from '../types/outputTypes'; | |
import { joinIfExist } from '../../utils'; | |
const inputFields = { | |
author_id: { | |
type: new GraphQLNonNull(GraphQLID), | |
}, | |
question_id: { | |
type: new GraphQLNonNull(GraphQLID), | |
}, | |
body: { | |
type: new GraphQLNonNull(GraphQLString), | |
}, | |
}; | |
function validate(input, { t, user }) { | |
const errors = []; | |
const data = {}; | |
if (typeof input.body === 'undefined') { | |
errors.push({ | |
key: 'body', | |
message: t('The body field cannot be empty'), | |
}); | |
} else if (input.body.length < 3 || input.body.trim() === '') { | |
errors.push({ | |
key: 'body', | |
message: t('The body field must longer 3'), | |
}); | |
} else { | |
data.body = input.body; | |
} | |
return { data, errors }; | |
} | |
export const createAnswer = mutationWithClientMutationId({ | |
name: 'CreateAnswer', | |
inputFields, | |
outputFields: { | |
answer: { | |
type: answerType, | |
resolve({ answer }) { | |
return answer; | |
}, | |
}, | |
}, | |
async mutateAndGetPayload(input, context) { | |
const { data, errors } = validate(input, context); | |
const user = fromGlobalId(input.author_id); | |
if (user.type !== 'userType') { | |
throw new Error('The user id is not valid'); | |
} | |
const question = fromGlobalId(input.question_id); | |
if (question.type !== 'questionType') { | |
throw new Error('The question id is not valid'); | |
} | |
if (errors.length) { | |
throw new ValidationError(errors); | |
} | |
data.author_id = user.id; | |
data.question_id = question.id; | |
const rows = await db | |
.table('answers') | |
.insert(data) | |
.returning('id'); | |
console.log('rows = ', rows); | |
return context.answerById.load(rows[0]).then(answer => ({ answer })); | |
}, | |
}); | |
export const updateAnswer = mutationWithClientMutationId({ | |
name: 'UpdateAnswer', | |
inputFields: { | |
id: { type: new GraphQLNonNull(GraphQLID) }, | |
body: { | |
type: new GraphQLNonNull(GraphQLString), | |
}, | |
}, | |
outputFields: { | |
answer: { | |
type: answerType, | |
resolve({ answer }) { | |
return answer; | |
}, | |
}, | |
}, | |
async mutateAndGetPayload(input, context) { | |
const { t, user } = context; | |
const { data, errors } = validate(input, context); | |
const answer = await db | |
.table('answers') | |
.where('id', '=', input.id) | |
.first('*'); | |
if (!answer) { | |
errors.push({ | |
key: '', | |
message: 'Failed to save the answer. Please make sure that it exists.', | |
}); | |
} | |
data.updated_at = db.raw('CURRENT_TIMESTAMP'); | |
await db | |
.table('answers') | |
.where('id', '=', input.id) | |
.update(data); | |
return context.answerById.load(input.id).then(x => ({ answer: x })); | |
}, | |
}); | |
export const deleteAnswer = mutationWithClientMutationId({ | |
name: 'DeleteAnswer', | |
inputFields: { | |
id: { type: new GraphQLNonNull(GraphQLID) }, | |
}, | |
outputFields: deletedOutput, | |
async mutateAndGetPayload(input, context) { | |
const { t } = context; | |
const deletedRows = await db | |
.table('answers') | |
.where('id', '=', input.id) | |
.first('*') | |
.del() | |
.then(rows => ({ | |
deletedRows: rows, | |
})); | |
return deletedRows; | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment