GraphQL - A query language for your API

Bartlomiej Skwira

Disclaimer:

 

All examples taken from the official documentation at https://graphql.org/learn/

What is an API?

API - Application Program Interface

What is a web API?

Web API

the part of the server that receives requests and sends responses

Most APIs are RESTful

Title Text

Most APIs are RESTful

REST API example

REST APIs - what's wrong with them?

  • Multiple Round Trips To Fetch Related Resources

  • Over Fetching / Under Fetching

GraphQL the hype

  • 2012 - developed internally by Facebook
  • 2015 - publicly released
  • 2018 - moved to GraphQL Foundation*

 

 

 

 

1

GraphQL history

*hosted by the non-profit Linux Foundation

 

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. *

 

 

 

*official definition from https://graphql.org

1

What is GraphQL?

 

1. Define types and fields:

 

 

 

1

How GraphQL works?

 

 

2. Provide function for each field on each type

 

1

How GraphQL works?

 

 

3. Run a graph service:

1

How GraphQL works?

 

 

4. Run a query:

 

The server:

  • checks to ensure it only refers to the types and fields defined
  • runs the provided functions to produce a result
  • query == read or write operation
1

How GraphQL works?

 

 

4. Run a query:

 

 

 

 

1

How GraphQL works?

 
{
  me {
    name
  }
}
{
  "me": {
    "name": "Luke Skywalker"
  }
}

JSON result

Example query

 

Ask for a specific field -> get the same shape as result

 

 

 

1

GraphQL queries 

 

 

Query objects

 

 

 

1

GraphQL queries 

 

 

Arguments

 

 

 

1

GraphQL queries 

 

 

Aliases to rename the result

 

 

 

1

GraphQL queries 

 

 

1. Ask for a specific field -> get the same shape as result

 

 

 

1

GraphQL queries 

 

 

Fragments - reusable sets of query fields

 

 

 

1

GraphQL queries 

 

GraphQL queries 

 

 

Explicit operation name

 

 

1

Operation == query or mutation or subscription 

 

Variables for dynamic queries

 

 

 

1

GraphQL queries 

 

 

Directives

 

 

 

1

GraphQL queries 

 

 

Mutations

 

 

 

1

GraphQL queries 

 

GraphQL schemas and types 

 

 

Object types

type Starship {
  id: ID!
  name: String!
  length(unit: LengthUnit = METER): Float
  appearsIn: [Episode!]!
}

 

1

GraphQL schemas and types 

 

 

Scalar types:

  • Int
  • Float
  • String
  • Boolean
  • ID - object/cache
  • can implement custom scalar types*

 

 

1

GraphQL schemas and types 

 

 

Enum Types:

 

 

1

GraphQL schemas and types 

 

 

Interfaces

 

1

GraphQL schemas and types 

 

 

Union

 

 

1

 

 

Authorization -> business logic layer

 

 

 

1

GraphQL

 

GraphQL pros 

 
  • no need to do multiple round trips to fetch data
  • a request language: less client-server dependency, no hardcoded data size and shape
  • express data requirements in a declarative way

GraphQL cons

 
  • introducing another dependency to the project
  • resource exhaustion attack (DoS attack)
  • authentication and authorization - do we handle them before, after, or during a GraphQL resolve process?
  • RESTful APIs are easier to cache
  • N+1 SQL queries

 

 

Who is using GraphQL?

 

1

GraphQL 

 

GraphQL Editor 

 
Made with Slides.com