Last active
September 24, 2020 10:04
-
-
Save 2color/71da3fd8bdce7209c9ab5fb2948e8e35 to your computer and use it in GitHub Desktop.
How to add JSDoc
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 { PrismaClient } from '@prisma/client' | |
export const prisma = new PrismaClient() | |
export function createContext() { | |
return { prisma } | |
} |
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 { makeExecutableSchema } from 'apollo-server' | |
const typeDefs = ` | |
type User { | |
email: String! | |
id: ID! | |
name: String | |
posts: [Post!]! | |
} | |
type Post { | |
author: User | |
content: String | |
id: ID! | |
published: Boolean! | |
title: String! | |
} | |
type Query { | |
feed: [Post!]! | |
filterPosts(searchString: String): [Post!]! | |
post(where: PostWhereUniqueInput!): Post | |
} | |
type Mutation { | |
createDraft(authorEmail: String, content: String, title: String!): Post! | |
deleteOnePost(where: PostWhereUniqueInput!): Post | |
publish(id: ID): Post | |
signupUser(data: UserCreateInput!): User! | |
} | |
input PostWhereUniqueInput { | |
id: ID | |
} | |
input UserCreateInput { | |
email: String! | |
id: ID | |
name: String | |
posts: PostCreateManyWithoutPostsInput | |
} | |
input PostCreateManyWithoutPostsInput { | |
connect: [PostWhereUniqueInput!] | |
create: [PostCreateWithoutAuthorInput!] | |
} | |
input PostCreateWithoutAuthorInput { | |
content: String | |
id: ID | |
published: Boolean | |
title: String! | |
} | |
` | |
// From https://www.typescriptlang.org/docs/handbook/jsdoc-supported-types.html | |
/** | |
* @typedef { import("./context.js").prisma } prisma | |
*/ | |
const resolvers = { | |
Query: { | |
/** | |
* @param {any} parent | |
* @param {any} args | |
* @param {{ prisma:prisma }} | |
*/ | |
feed: (parent, args, { prisma }) => { | |
return prisma.post.findMany({ | |
where: { published: true }, | |
}) | |
}, | |
filterPosts: (parent, args, ctx) => { | |
return ctx.prisma.post.findMany({ | |
where: { | |
OR: [ | |
{ title: { contains: args.searchString } }, | |
{ content: { contains: args.searchString } }, | |
], | |
}, | |
}) | |
}, | |
post: (parent, args, ctx) => { | |
return ctx.prisma.post.findOne({ | |
where: { id: Number(args.where.id) }, | |
}) | |
}, | |
}, | |
Mutation: { | |
createDraft: (parent, args, ctx) => { | |
return ctx.prisma.post.create({ | |
data: { | |
title: args.title, | |
content: args.content, | |
published: false, | |
author: { | |
connect: { email: args.authorEmail }, | |
}, | |
}, | |
}) | |
}, | |
deleteOnePost: (parent, args, ctx) => { | |
return ctx.prisma.post.delete({ | |
where: { id: Number(args.where.id) }, | |
}) | |
}, | |
publish: (parent, args, ctx) => { | |
return ctx.prisma.post.update({ | |
where: { id: Number(args.id) }, | |
data: { published: true }, | |
}) | |
}, | |
signupUser: (parent, args, ctx) => { | |
return ctx.prisma.user.create(args) | |
}, | |
}, | |
User: { | |
posts: (parent, args, ctx) => { | |
return ctx.prisma.user | |
.findOne({ | |
where: { id: parent.id }, | |
}) | |
.posts() | |
}, | |
}, | |
Post: { | |
author: (parent, args, ctx) => { | |
return ctx.prisma.post | |
.findOne({ | |
where: { id: parent.id }, | |
}) | |
.author() | |
}, | |
}, | |
} | |
export const schema = makeExecutableSchema({ | |
resolvers, | |
typeDefs, | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment