Understanding GraphQL Schema

What is schema?

A schema is the structure behind data organization

Types

  • Query and Mutation Types
  • Scalar Type
  • Enumeration
  • List and Not-null
  • Interfaces
  • Union Types
  • Input types

Query & Mutation

type Query {
  hero(episode: Episode): Character
  droid(id: ID!): Droid
}
mutation CreateReviewForEpisode($ep: Episode!, $review: ReviewInput!) {
  createReview(episode: $ep, review: $review) {
    stars
    commentary
  }
}

Scalar Type

  • IntĀ 
  • StringĀ 
  • Float
  • Boolean
  • ID

Enumeration

enum Episode {
  NEWHOPE
  EMPIRE
  JEDI
}

List and Not-null

type Character {
  name: String!
  appearsIn: [Episode]!
}

Interfaces

interface Character {
  id: ID!
  name: String!
}
type Human implements Character {
  id: ID!
  name: String!
  starships: [Starship]
  totalCredits: Int
}

type Droid implements Character {
  id: ID!
  name: String!
  primaryFunction: String
}

Union Types

union SearchResult = Human | Droid | Starship
{
  search(text: "an") {
    ... on Human {
      name
      height
    }
    ... on Droid {
      name
      primaryFunction
    }
    ... on Starship {
      name
      length
    }
  }
}

Input Types

input ReviewInput {
  stars: Int!
  commentary: String
}

Schema

  • There are two special types in GraphQL
schema {
  query: Query
  mutation: Mutation
}

Fields

  • Each types property are know as field
type Character {
  name: String!
  appearsIn: [Episode]!
}

Resolver

  • Each types property are know as field
type Query {
  hero(episode: Episode): Character
  droid(id: ID!): Droid
}
const resolvers = {
  Query: {
    hero(root, args, context, info) {
      return find(heroes, { id: args.id });
    },
  },
  Character: {
    appearsIn {
      return filter(characterAppearsIn, { author: author.name });
    },
  },
};

Summary

Q & A

Understanding GraphQL Schema

By Pankaj Parkar

Understanding GraphQL Schema

Understanding GraphQL Schema

  • 1,418