26
Client-Server Communication with GraphQL Web Engineering, Guest lecture, 188.951 2VU SS20 Erik Wittern | [email protected] | @erikwittern | wittern.net May 18 th 2020

Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Client-ServerCommunication with GraphQLWeb Engineering, Guest lecture, 188.951 2VU SS20

Erik Wittern | [email protected] | @erikwittern | wittern.netMay 18th 2020

Page 2: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Learning goals

• What actually is GraphQL, and how came it about?• How does GraphQL work?• What are some benefits vs. challenges for GraphQL?

Mai 18th 2020 2

Page 3: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Background & Overview

Mai 18th 2020 3

Page 4: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

In 2012, Facebook faced a problem

Mai 18th 2020 4Source: https://reactjs.org/blog/2015/05/01/graphql-introduction.html

An increasing number of ever-evolving (mobile, native) clients…

…led to the creation of (and hence maintenance burden for) more and

more, increasingly complex (ad hoc) API endpoints.

Page 5: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

GraphQL shifts control over what data is returned (or mutated) to clients

type User {

name: String

age: Int

}

type Query {

me: User

}

Providers define their data types at design time

query {me {name

}}

Clients send queries at runtime

{"data" : {"me" : {"name" : "Erik"

}}

}

Servers respond with data at runtime

Mai 18th 2020 5

Page 6: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Demohttps://www.github.com/ErikWittern/graphql-demo

Mai 18th 2020 6

Page 7: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

So, what is GraphQL?

• A query language for networked APIs…• …and a runtime for servers to fulfill queries

Response: {"data": …}

POST {"introspection": …}

{"schema": …}

Serv

er

DB

API

• Clients send type-checked queries, servers respond with requested data:

GraphQL client (here: GraphiQL)

• Specification + reference implementation in JavaScript

Request: POST {"query": …}

Mai 18th 2020 7

Page 8: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

History of GraphQL• 2012 – Originally developed and used by Facebook• …to serve increasing numbers of diverse clients

• Sep 2015 – Open sourcing

• Sep 2016 – Move from “technical preview” to “working draft”

• Nov 2018 – Announcement of GraphQL Foundation (part of The Linux Foundation)

Mai 18th 2020 8

Page 9: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Language & Runtime

Mai 18th 2020 9

Page 10: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Anatomy of a GraphQL query (selected concepts)

Mai 18th 2020 10

query fetchGraphQLData ($details: Boolean!) {

repository(owner: "graphql", name: "graphql-js") {

...repoDetails @include(if: $details)issueOrPullRequest(number: 10) {

...on Issue {updatedAt

}}

}}fragment repoDetails on Repository {repoName: namedescription

}

Fragment definition

Operation type Operation name Variable definition

Field selection 2 Arguments

Fragment spread Directive

Inline fragment

Selection setType condition

More details at: http://spec.graphql.org/draft/#sec-Document-Syntax

Alias

Page 11: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Defining schemas with the schema definition language (SDL)

Mai 18th 2020 11

schema {query: Query

}

type Query {

users(limit: Int!): [User]}

directive @upperCase on FIELD_DEFINITION

enum Status {ACTIVEINACTIVE

}

type User {name: String @upperCase"""Description of field User.status"""status: Status

}

Schema definition

Root operation type definition

Object type definition

Argument definition

Field definition

Directive definition

Directive locations

Directive

TypeName

More details at: http://spec.graphql.org/draft/#sec-Document-Syntax

Enum type definition

Name

Page 12: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Query exeuction on a (HTTP) server

Mai 18th 2020 12

HTTP server

GraphQL middleware

GraphQL execution engine

Extra

ct re

ques

t pa

ram

eter

s

{"errors": … }

{"query": … }

{"data": … , "errors": … }

Eval

uate

se

lect

ion

sets

Resolver functions(contained in schema object)

DB

API

Pars

e qu

ery

strin

g

Valid

ate

(aga

inst

sch

ema)

Coe

rce

varia

ble

valu

es

Build

resp

onse

Page 13: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Advanced Query Concepts

Mai 18th 2020 13

Page 14: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Introspection• Introspection is a mechanism for clients to learn (at

runtime) about the data types and operations a GraphQL server offers

• An introspection query is a plain-old GraphQL query…

• …that happens to select meta-fields provided by introspection types

Mai 18th 2020 14

query IntrospectionQuery {__schema {queryType { name }mutationType { name }subscriptionType { name }types {...FullType

}directives {namelocationsargs {...InputValue

}}

}}

... Directive Definitions ...

• Client-tools like GraphiQL rely on introspection for:• Showing documentation about types & operations• Client-side query validation• Auto-completion when typing queries• Etc.

Page 15: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Demohttps://developer.github.com/v4/explorer/

Mai 18th 2020 15

Page 16: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Pagination with slicing arguments and offset• Pagination aims to return different parts

(or slices) of long lists of data• Slicing arguments (often named max, limit , first, or last) define length of slice to return

• Often combined with an “offset”

Mai 18th 2020 16

query fetchPage2 {user {namefriends(last: 5, offset: 5) {name

} } }

17 16 15 14 13 12 11 10 9 8 7 6

offset slice

5 4 3 2 1

page 1 page 2

…17 16 15 14 13 12 11 10 9 8 7 6

offset slice

5 420 19 18

page 1 page 2

new returned twice• One problem: this approach may return items twice when list updates

15 14 13

Page 17: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Pagination with Cursor Connections

• Cursor Connections rely on…• Fields using slicing arguments…• …return a Connection with fields pageInfo and edges…

• …where each Edge has fields cursorand node, containing the actual object.

Mai 18th 2020 17More details about cursor connections at: https://relay.dev/graphql/connections.htm (also source of the example above)

query fetchPage2 {user {namefriends(last: 5, after: "opaqueCursor") {pageInfo {hasNextPage

}edges {cursornode {name

}} } } }

…17 16 15 14 13 12 11 10 9 8 7 6

cursor slice

5 420 19 18

page 1 page 2new

• Robust to list-updates outside the slice during paginating

• Think of a common Facebook’s use-case: news feed, where mostly items are added

Page 18: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Pros & Cons

Mai 18th 2020 18

Page 19: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

GraphQL benefits for clientsIn-sync

documentation

of type systemPredictable responses

No over-fetching

Fewer roundtrips

Static typing (auto-complete, validation)

Mai 18th 2020 19

Page 20: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

GraphQL benefits for providers

• Happy API consumers (!)• Simplified maintenance• Serve clients with diverse, changing

requirements with a single endpoint• GraphQL API self-documents types & operations

• Improved performance and operations• Avoid loading / caching / exposing unneeded

data• Understand data-use on a per-field level

• Compose heterogenous backend resources

Mai 18th 2020 20

Serv

er

DB

API

Page 21: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Challenge: HTTP caching of GraphQL requests

• Problems with typical HTTP proxy / gateway caches include:• Often, non-safe & non-idempotent POST is

used to send (large) queries• Some queried fields may become stale

sooner than others, making it hard to define Cache-Control / Last-modified headers

Mai 18th 2020 21More details at: https://www.apollographql.com/blog/graphql-caching-the-elephant-in-the-room-11a3df0c23ad

Client-side cache

Proxycache

Server

DB

APIGateway

cache

Applicationcache

consumer

network

provider

• Alternatives include:• Cache persisted queries in proxy or gateway• Client-side caching based on ID field• Application caches in the data-layer

(“DataLoaders”) or resolver functions

Page 22: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Challenge: rate-limiting & threat prevention

• Servers may need to deal with excessive queries sent by clients• Rate-limiting - and not “x requests per

time-interval”• Pricing requests• Blocking (inadvertently) threatening

requests

Mai 18th 2020 22

query fetchAllTheData {users (limit: 1000) {orders (first: 1000) {paymentDetails { # calls external API status

}}

}}

= ~1000s of REST requests!• Options include:• Timeouts against threatening requests• Dynamic analysis• Static analysis

• Query “depth” or “nesting”• Query “cost” or “complexity”

More details at: https://www.ibm.com/blogs/research/2019/02/graphql-api-management/

Page 23: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Wrap-up

Mai 18th 2020 23

Page 24: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Summary• Remember: GraphQL was created to address specific problems with other

API models• Using GraphQL may or may not be beneficial

• Who are API clients? Internal, external, both?• How is an API used?• How will the API (likely) evolve?• à consider the trade-offs (as with most technology choices)

• There is much more to learn about GraphQL !!• Mutation and subscription operations• (Automatic) mappings to REST APIs or databases• Schema stitching and federation• And more!

Mai 18th 2020 24

Page 25: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Additional resources• Web resources

• Official GraphQL website, incl. documentation (https://graphql.org/)• GraphQL specification (http://spec.graphql.org/)• Principled GraphQL (https://principledgraphql.com/)

• Libraries• GraphQL-js reference implementation (https://github.com/graphql/graphql-js)• OpenAPI-to-GraphQL (https://github.com/IBM/openapi-to-graphql)• Apollo Client (https://www.apollographql.com/client/)• …and many many more!!!

• Videos• “GraphQL – The Documentary” (https://www.youtube.com/watch?v=783ccP__No8)• “Zero to GraphQL in 30 Minutes” by Steven Luscher (https://www.youtube.com/watch?v=UBGzsb2UkeY)

• Research papers & books• “Semantics and Complexity of GraphQL” by Hartig and Perez (http://olafhartig.de/files/HartigPerez_WWW2018_Preprint.pdf)• “An Empirical Study of GraphQL Schemas” by Wittern et al.

(http://people.cs.vt.edu/davisjam/downloads/publications/WitternChaDavisBaudartMandel-EmpiricalGraphQL-ICSOC19.pdf, summary at https://medium.com/swlh/empirical-study-graphql-icsoc19-29038c48da5)

• ”Production ready GraphQL” by Marc-Andre Giroux (from GitHub, @__xuorig__) (https://book.productionreadygraphql.com/)

Mai 18th 2020 25

Page 26: Client-Server Communication with GraphQL · Introspection •Introspection is a mechanism for clients to learn (at runtime) about the data types and operations a GraphQL server offers

Thank you!

Mai 18th 2020 26