GRAPHQL

by Gian Marco Toso   -   @gianmarcotoso

APIConf 2018

starting at 11:30

What is GraphQL?

  • GraphQL is a query language for APIs
  • GraphQL is a runtime to fulfill those queries
  • It allows the client to ask exactly what it needs to the API endpoint
  • It allows to fully describe the API's data by using a strongly typed language

What does it look like?

/* Define the schema */

type Product {
    id: ID!
    name: String!
    price: Float!
}

type Customer {
    id: ID!
    email: String!
}

type Order {
    products: [Product]!
    customer: Customer!
    comment: String
}
/* Call the /graphql endpoint */

{
    orders(email: 'frank@example.com') {
        products {
            name
        }
    }
}
/* Get a JSON response! */

{
  orders: [
    { products: ['PS4', 'A Game'] },
    { products: ['Washing Machine'] },
    { products: ['Dishwasher'] }
  ]
}

Main Advantages

  • No overfetching (get just what you need)
  • Multiple resources in a single request
  • Type system helps to document the API
  • APIs can evolve without versioning
  • Great dev experience with GraphiQL

Main Drawbacks

  • N+1 requests to datastores due to multiple resolvers
  • It does not always replace a REST API
  • File upload is not supported
  • Adds an extra layer of complexity and potential technical debt
  • It might solve a problem you don't have

Open discussion

  • Should I use GraphQL instead of REST?
  • Should I use GraphQL alongside REST?
  • Is GraphQL appropriate for my use case?

GraphQL

By Gabriele Mittica

GraphQL

  • 744