Notes

Schema definition in GraphQL (example)

Edit on GitHub

GraphQL
2 minutes
  • SDL is Schema Definition Language
  • ! means it’s a required field
  • [] means it’s a list (like an array)
  • ID is automatically generated
  • There are three root types: query, mutation and subscription
    • query lets you read data (the R of CRUD)
    • mutation lets you create, update and delete data (the C, U, and D of CRUD)
    • subscription lets you subscribe to realtime updates and keep receiving data whenever an event occurs (e.g. a mutation)
 1# TYPES
 2# a type called Person for creating user objects
 3type Person {
 4  id: ID!
 5  name: String!
 6  age: Int!
 7  posts: [Post!]!
 8}
 9
10type Post {
11  title: String!
12  author: Person!
13}
14
15# Query type to retrieve a list of Person
16type Query {
17  allPersons(last: Int): [Person!]! # return a list of all users, can accept a `last` parameter to only show last X no. of Persons created
18  allPosts(last: Int): [Post!]!
19}
20
21# Mutation type to create/update/delete a Person
22type Mutation {
23  createPerson(name: String!, age: Int!): Person! # return a single person object, created by this mutation
24  updatePerson(id: ID!, name: String!, age: Int): Person!
25  deletePerson(id: ID!): Person! # delete the Person having the provided `id`
26  # add CUD for posts here..
27}
28
29# Subscription type to subscribe to the events of Person being created/updated/deleted
30type Subscription {
31  newPerson: Person!
32  updatedPerson: Person!
33  deletedPerson: Person!
34  # add events for post here..
35}
 1# QUERIES
 2{
 3  allPersons {
 4    id
 5    name
 6    age
 7  }
 8}
 9
10mutation {
11  createPerson(name: "Henry", age: 32) {
12    id
13  }
14}
15
16subscription {
17  newPerson {
18    id
19    name
20    age
21  }
22}