Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection of common scenarios #35

Open
marktani opened this issue Dec 16, 2017 · 0 comments
Open

Collection of common scenarios #35

marktani opened this issue Dec 16, 2017 · 0 comments
Assignees

Comments

@marktani
Copy link
Contributor

marktani commented Dec 16, 2017

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

Other scenarios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants