GraphQL Schema Design

Design First

Client First

Naming

The names are the API talking back to you, so listen to them

type Query {
  products(ids: [ID!]): [Product!]!
  findPosts(ids: [ID!]): [Post!]!
}

type Mutation {
  addProduct(input: AddProductInput): AddProductPayload
  createPost(input: CreatePostInput): CreatePostPayload
}

Expressive Schemas

# creditCardというprefixが複数のフィールドで定義されている
type Payment {
  creditCardNumber: CreditCardNumber
  creditCardExpiration: CreditCardExpiration
  giftCardCode: String
}

# creditCardXxxを別のtypeに切り出した場合 (p.39)
type Payment {
  creditCard: CreditCard
  giftCardCode: String
}

type CreditCard {
  number: CreditCardNumber!
  expiration: CreditCardExpiration!
}
union Payment = CreditCard | GiftCardCode

type CreditCard {
  number: CreditCardNumber!
  expiration: CreditCardExpiration!
}

type GiftCardCode {
  code: String!
}