2023-07-23 15:08:43 +00:00
|
|
|
import { GraphQLError } from 'graphql'
|
2021-03-25 19:29:24 +00:00
|
|
|
|
|
|
|
export default {
|
|
|
|
Query: {
|
|
|
|
messages: async (parent, args, { models }) =>
|
|
|
|
await models.message.findMany(),
|
|
|
|
message: async (parent, { id }, { models }) =>
|
|
|
|
await models.message.findUnique({ where: { id } })
|
|
|
|
},
|
|
|
|
|
|
|
|
Mutation: {
|
|
|
|
createMessage: async (parent, { text }, { me, models }) => {
|
|
|
|
if (!text) {
|
2023-07-23 15:08:43 +00:00
|
|
|
throw new GraphQLError('Must have text', { extensions: { code: 'BAD_INPUT' } })
|
2021-03-25 19:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return await models.message.create({
|
|
|
|
data: { text, userId: me.id }
|
|
|
|
})
|
|
|
|
},
|
|
|
|
deleteMessage: async (parent, { id }, { models }) =>
|
|
|
|
await models.message.delete({ where: { id } })
|
|
|
|
},
|
|
|
|
|
|
|
|
Message: {
|
|
|
|
user: async (message, args, { models }) =>
|
|
|
|
await models.user.findUnique({ where: { id: message.userId } })
|
|
|
|
}
|
|
|
|
}
|