92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
import { UserInputError, AuthenticationError } from 'apollo-server-micro'
|
|
|
|
const createItem = async (parent, { title, text, url, parentId }, { me, models }) => {
|
|
if (!me) {
|
|
throw new AuthenticationError('You must be logged in')
|
|
}
|
|
|
|
const data = {
|
|
title,
|
|
url,
|
|
text,
|
|
user: {
|
|
connect: {
|
|
name: me.name
|
|
}
|
|
}
|
|
}
|
|
|
|
if (parentId) {
|
|
data.parent = {
|
|
connect: {
|
|
id: parseInt(parentId)
|
|
}
|
|
}
|
|
}
|
|
|
|
return await models.item.create({ data })
|
|
}
|
|
|
|
export default {
|
|
Query: {
|
|
items: async (parent, args, { models }) => {
|
|
return await models.$queryRaw(`
|
|
SELECT id, "created_at" as "createdAt", title, url, text, "userId", ltree2text("path") AS "path"
|
|
FROM "Item"
|
|
ORDER BY "path"`)
|
|
}
|
|
},
|
|
|
|
Mutation: {
|
|
createLink: async (parent, { title, url }, { me, models }) => {
|
|
if (!title) {
|
|
throw new UserInputError('Link must have title', { argumentName: 'title' })
|
|
}
|
|
|
|
if (!url) {
|
|
throw new UserInputError('Link must have url', { argumentName: 'url' })
|
|
}
|
|
|
|
return await createItem(parent, { title, url }, { me, models })
|
|
},
|
|
createDiscussion: async (parent, { title, text }, { me, models }) => {
|
|
if (!title) {
|
|
throw new UserInputError('Link must have title', { argumentName: 'title' })
|
|
}
|
|
|
|
return await createItem(parent, { title, text }, { me, models })
|
|
},
|
|
createComment: async (parent, { text, parentId }, { me, models }) => {
|
|
if (!text) {
|
|
throw new UserInputError('Comment must have text', { argumentName: 'text' })
|
|
}
|
|
|
|
if (!parentId) {
|
|
throw new UserInputError('Comment must have text', { argumentName: 'text' })
|
|
}
|
|
|
|
return await createItem(parent, { text, parentId }, { me, models })
|
|
}
|
|
},
|
|
|
|
Item: {
|
|
user: async (item, args, { models }) =>
|
|
await models.user.findUnique({ where: { id: item.userId } }),
|
|
depth: async (item, args, { models }) => {
|
|
if (item.path) {
|
|
return item.path.split('.').length - 1
|
|
}
|
|
|
|
// as the result of a mutation, path is not populated
|
|
const [{ path }] = await models.$queryRaw`
|
|
SELECT ltree2text("path") AS "path"
|
|
FROM "Item"
|
|
WHERE id = ${item.id}`
|
|
|
|
return path.split('.').length - 1
|
|
},
|
|
comments: () => 0,
|
|
sats: () => 0
|
|
}
|
|
}
|