GraphQL Types system

GraphQL Type System
Image from graphql.org

Type systems in GraphQL

Schema type and Root types

Scalars and Enumeration types

Scalars

{
user {
name
book: {
title
pages
}
}
}
//Would result in following JSON data{
“data”: {
“user”: {
“name”: “Jay”,
“book”: {
“title”: “Some book title”,
“pages”: 200
}
}
}
}

Enumeration types

enum Gender {
MASCULINE
FEMININE
COMMON
NEUTER
}
input createUserInput { # later on this input object types
name: String!
email: String!
gender: Gender!
}
query {
createUser(input: createUserInput!): User!
}

Object types and Input object

type Book {
title: String!
pages: Int!
}
input createBookInput {
title: String!
pages: Int!
}
mutation createBook(input: createBookInput!): Book!

Interfaces and Unions

Interfaces

interface Book {
title: String
author: Author
}
type TextBook implements Book {
title: String # Must be present
author: Author # Must be present
course: Course
}
type Comic implements Book {
title: String # Must be present
author: Author # Must be present
genre: String
}
type Query {
myBooks: [Book] # list of Book
}
query {
myBooks {
title # always present part of Book interface
author # always present part of Book interface
...on TextBook { # inline fragments
course: { # only present on type TextBook
name
}
}
...on Comic {
genre # only present on type Comic
}
}
}

Unions

type Movie {
name: String
director: String
}
union SearchResult = Book | Moviequery {
search(contains: String!): [SearchResult]
}
query {
search(contains: "Godfather") {
... on Book {
title
}
... on Movie {
name
}
}
}

Modifiers

List

type Query {
myBooks: [Book] # list of Book
}

Non-Null

type Book {
title: String!
pages: Int!
}
input createBookInput {
title: String!
pages: Int!
}
mutation createBook(input: createBookInput!): Book!

Composing Modifiers

# 1. List of Non-Null types
nickNames: [String!]
# This means the list nickNames can itself be null but cant have null members i.e
nickNames: [] # valid
nickNames: null # valid
nickNames: ["Bruno", "B" ] # valid
nickNames: ["Bruno", null] # not valid error
# -----------# 2. Non-Null List of types
nickNames: [String]!
# This means the list nickNames cannot be null but can have null members and can be empty i.e
nickNames: null # not valid error
nickNames: [] # valid
nickNames: ["Bruno", "B" ] # valid
nickNames: ["Bruno", null] # valid
# -----------# 3. Non-Null List of Non-Null types
nickNames: [String!]!
# This means the list as well as it's members cannot be null i.e
nickNames: null # not valid error
nickNames: [] # valid
nickNames: ["Bruno", "B" ] # valid
nickNames: ["Bruno", null] # not valid error

Get in touch

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store