user-avatar

Kyle J. Roux

jstacoder

Developer Program Member

a python/javascript/php/linux guru ready to take on the world...

CasePeer
orange, CA, USA
https://jstacoder.github.io

Hi
Home

GraphQL schema definition 101

1 min read

Graphql schema 101


Why might you need to define a graphql schema?

There are a few reasons this could happen:

    Lets look at a simple type to represent a customer

    type Customer {
    firstName: String!
    lastName: String!
    age: Int!
    }

    Here we are defining a Customer type

    It has a first name, a last name (strings) and an age (an int). The exclamation points following the type names signify that the fields are required.

    Simple enough, but things start to get more complex when you want to model something on our Customer that doesn't map to a primitive like a String or an Int, Like lets say we want to also have types for Product and Order.

    type Customer {
    ...
    orders: [Order]!
    }
    type Product {
    name: String!
    }
    type Order {
    products: [Product!]!
    customer: Customer!
    }

    So now we have a Customer and we can track their orders of given products.

    Home