पाठ 4 / 25

Object Types, Scalars and Nullability

Building blocks of SDL.

Fields, lists and non-null

An object type has fields, each with a type. Built-in scalars are Int, Float, String, Boolean and ID; custom scalars such as DateTime can be added. ! marks a field as non-null and [Type] is a list, so [Order!]! is a non-null list of non-null orders. Fields can take arguments, and descriptions in triple quotes become documentation. Design types around the client's domain language, not your database tables.

The contract between client and server

The Schema Definition Language (SDL) describes types, fields and operations clients can use.

Three ideas: object types and scalars, root operation types, abstract and input types.
Figure 2.1 — Types, operations and abstract types.

A shop schema

GraphQL SDL.

"""A registered customer."""
type User {
  id: ID!
  name: String!
  email: String!
  orders(first: Int = 10, after: String): OrderConnection!
}

type Order {
  id: ID!
  placedAt: DateTime!
  status: OrderStatus!
  total: Money!
  items: [OrderItem!]!
}

type OrderItem {
  product: Product!
  quantity: Int!
}

scalar DateTime

Be deliberate with non-null

Non-null fields are a promise; if a resolver fails, the null bubbles up to the nearest nullable parent, possibly wiping out more data than expected.

त्वरित जाँच: What does [Order!]! mean?

  • An optional list of optional orders
  • A non-null list whose items are non-null orders
  • Exactly one order
  • A map of orders
Answer

A non-null list whose items are non-null orders — Both the list and its items are non-null.