Collection of common scenarios
marktani opened this issue · comments
Scenarios
The following is a collection of commons scenarios, with the goal of providing step-by-step explanations of how to go about them.
All scenarios are based on typescript-basic
.
Adding fields to an existing type
Example & Instructions
Example
Adding a new address field to the user type in the database, with the purpose of exposing it in the application API as well.
Instructions
1. Adding the field to the data model
in database/datamodel.graphql
:
type User {
id: ID! @unique
email: String! @unique
password: String!
name: String!
posts: [Post!]! @relation(name: "UserPosts")
+ address: String
}
2. Deploying the updated data model
graphcool deploy
This will
- deploy the new database structure to the local service
- download the new GraphQL schema for the database to
database/schema.graphql
3. Adding the field to the application schema
in src/schema.graphql
:
type User {
id: ID!
email: String!
name: String!
posts: [Post!]!
+ address: String
}
Adding a new resolver to the GraphQL server
Example & Instructions
Example
Suppose we want to add a custom resolver to delete a post.
Instructions
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 we can run the following mutation to delete a post:
mutation {
delete(id: "post-id") {
id
}
}
Further resolver-centric scenarios
- prisma-labs/graphql-prisma-typescript#49
- https://www.graph.cool/forum/t/querying-specific-fields-in-db-from-localservice-in-graphcool-1-0/2075?u=nilan
- https://www.graph.cool/forum/t/querying-specific-fields-in-db-from-localservice-in-graphcool-1-0/2075/4?u=nilan
- prisma-labs/prisma-binding#61