GraphQL - A query language for your API
Bartlomiej Skwira
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

August 2017, source: https://jaxenter.com/state-of-api-integration-report-136342.html
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*
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
What is GraphQL?
1. Define types and fields:
How GraphQL works?

2. Provide function for each field on each type
How GraphQL works?

3. Run a graph service:
How GraphQL works?
- usually a web service at a URL
- single endpoint
- has many differnt server implementations: C#, Go, Java, Python, Ruby, Clojure, JavaScript, PHP and more
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
How GraphQL works?
4. Run a query:
How GraphQL works?
{ me { name } }
{ "me": { "name": "Luke Skywalker" } }
JSON result
Example query
Ask for a specific field -> get the same shape as result
GraphQL queries

Query objects
GraphQL queries

Arguments
GraphQL queries

Aliases to rename the result
GraphQL queries

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

Fragments - reusable sets of query fields
GraphQL queries

GraphQL queries
Explicit operation name

Operation == query or mutation or subscription
Variables for dynamic queries
GraphQL queries

Directives
GraphQL queries

Mutations
GraphQL queries

GraphQL schemas and types
Object types
type Starship {
id: ID!
name: String!
length(unit: LengthUnit = METER): Float
appearsIn: [Episode!]! }
GraphQL schemas and types
Scalar types:
- Int
- Float
- String
- Boolean
- ID - object/cache
- can implement custom scalar types*
GraphQL schemas and types
Enum Types:

GraphQL schemas and types
Interfaces


GraphQL schemas and types
Union


Authorization -> business logic layer
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?
GraphQL

and more https://graphql.org/users/
GraphQL Editor
GraphQL - A query language for your API
By Bartlomiej Skwira
GraphQL - A query language for your API
- 230