The Schema

The schema is a description of the API. It lists every type the API has, the fields on each type, and every query and mutation the API offers. The schema is a text file on the server, written in GraphQL’s own syntax.

Here are the types for the shop’s GraphQL API:

type Product {
  id: ID!
  name: String!
  description: String!
  priceCents: Int!
  quantity: Int!
}

type Cart {
  id: ID!
  status: CartStatus!
  items: [CartItem!]!
  totalCents: Int!
}

type CartItem {
  productId: ID!
  quantity: Int!
  product: Product
}

enum CartStatus {
  OPEN
  ORDERED
}

type Order {
  id: ID!
  cartId: ID!
  items: [OrderItem!]!
  totalCents: Int!
  placedAt: String!
}

type OrderItem {
  productId: ID!
  productName: String!
  unitPriceCents: Int!
  quantity: Int!
}

A field is written as a name, a colon, and a type. GraphQL has five built-in types: ID, String, Int, Float, and Boolean. ID is the type for identifiers. placedAt is a String holding an ISO 8601 time in UTC.

A field can hold another type. Order.items holds a list of OrderItem, written [OrderItem!].

The ! after a type means the field is non-null: it always has a value. A field whose type has no ! may be null. A list type can have two ! marks. In [OrderItem!]!, the outer ! says the list itself is never null, and the inner ! says no order item in it is null.

The fields of a product, an order, and an order item are all non-null, because those records always have every field. CartItem.product is nullable.

An enum declares a type with a fixed set of values, such as enum CartStatus { OPEN ORDERED }.