docs/1.2/03-Tutorials/02-GraphQL-Server-Development/02-Resolver-Patterns.md
This tutorial gives an overview about common scenarios you might encounter when implementing your GraphQL server with graphql-yoga and Prisma.
Note that all scenarios in this tutorial are based on the typescript-basic GraphQL boilerplate project.
Adding a new address field to the User type in the database, with the purpose of exposing it in the application API as well.
in database/datamodel.graphql:
type User {
id: ID! @unique
email: String! @unique
password: String!
name: String!
posts: [Post!]!
+ address: String
}
prisma deploy
This will...
database/schema.graphqlIn src/schema.graphql:
type User {
id: ID!
email: String!
name: String!
posts: [Post!]!
+ address: String
}
Suppose we want to add a custom resolver to delete a Post.
Add a new delete field to the Mutation type in src/schema.graphql
type Mutation {
createDraft(title: String!, text: String): Post
publish(id: ID!): Post
+ delete(id: ID!): Post
}
Add a delete resolver to Mutation part of src/index.js
delete(parent, { id }, ctx, info) {
return ctx.db.mutation.deletePost(
{
where: { id }
},
info
);
}
Run yarn start.
Then you can run the following mutation to delete a post:
mutation {
delete(id: "__POST_ID__") {
id
}
}