Client-Server Communication with GraphQL
Web Engineering, Guest lecture, 188.951 2VU SS20 Erik Wittern | erikwittern@gmail.com | @erikwittern | wittern.net May 18th 2020
Client-Server Communication with GraphQL Web Engineering , Guest - - PowerPoint PPT Presentation
Client-Server Communication with GraphQL Web Engineering , Guest lecture, 188.951 2VU SS20 Erik Wittern | erikwittern@gmail.com | @erikwittern | wittern.net May 18 th 2020 Learning goals What actually is GraphQL, and how came it about?
Web Engineering, Guest lecture, 188.951 2VU SS20 Erik Wittern | erikwittern@gmail.com | @erikwittern | wittern.net May 18th 2020
Mai 18th 2020 2
Mai 18th 2020 3
Mai 18th 2020 4 Source: 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.
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
https://www.github.com/ErikWittern/graphql-demo
Mai 18th 2020 6
Response: {"data": …}
POST {"introspection": …} {"schema": …}
Server DB API
GraphQL client (here: GraphiQL)
Request: POST {"query": …} Mai 18th 2020 7
Foundation)
Mai 18th 2020 8
Mai 18th 2020 9
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: name description }
Fragment definition Operation type Operation name Variable definition Field selection 2 Arguments Fragment spread Directive Inline fragment Selection set Type condition
More details at: http://spec.graphql.org/draft/#sec-Document-Syntax
Alias
Mai 18th 2020 11
schema { query: Query } type Query { users(limit: Int!): [User] } directive @upperCase on FIELD_DEFINITION enum Status { ACTIVE INACTIVE } 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 Type Name
More details at: http://spec.graphql.org/draft/#sec-Document-Syntax
Enum type definition Name
Mai 18th 2020 12
HTTP server GraphQL middleware GraphQL execution engine Extract request parameters
{"errors": … } {"query": … } {"data": … , "errors": … }
Evaluate selection sets Resolver functions
(contained in schema object)
DB API Parse query string Validate
(against schema)
Coerce variable values Build response
Mai 18th 2020 13
runtime) about the data types and operations a GraphQL server offers
query…
introspection types
Mai 18th 2020 14
query IntrospectionQuery { __schema { queryType { name } mutationType { name } subscriptionType { name } types { ...FullType } directives { name locations args { ...InputValue } } } } ... Directive Definitions ...
https://developer.github.com/v4/explorer/
Mai 18th 2020 15
(or slices) of long lists of data
limit , first, or last) define length of slice to return
Mai 18th 2020 16
query fetchPage2 { user { name friends(last: 5, offset: 5) { name } } }
17 16 15 14 13 12 11 10 9 8 7 6
slice
5 4 3 2 1
page 1 page 2 …
17 16 15 14 13 12 11 10 9 8 7 6
slice
5 4 20 19 18
page 1 page 2 new returned twice
items twice when list updates
15 14 13
pageInfo and edges…
and node, containing the actual object.
Mai 18th 2020 17 More details about cursor connections at: https://relay.dev/graphql/connections.htm (also source of the example above)
query fetchPage2 { user { name friends(last: 5, after: "opaqueCursor") { pageInfo { hasNextPage } edges { cursor node { name } } } } }
…
17 16 15 14 13 12 11 10 9 8 7 6
cursor slice
5 4 20 19 18
page 1 page 2 new
during paginating
news feed, where mostly items are added
Mai 18th 2020 18
In-sync documentation
Predictable responses No over- fetching Fewer roundtrips Static typing (auto- complete, validation) Mai 18th 2020 19
requirements with a single endpoint
data
Mai 18th 2020 20
Server
DB API
gateway caches include:
used to send (large) queries
sooner than others, making it hard to define Cache-Control / Last- modified headers
Mai 18th 2020 21 More details at: https://www.apollographql.com/blog/graphql-caching-the-elephant-in-the-room-11a3df0c23ad
Client-side cache Proxy cache
Server
DB API
Gateway cache Application cache
consumer network provider
(“DataLoaders”) or resolver functions
excessive queries sent by clients
time-interval”
requests
Mai 18th 2020 22
query fetchAllTheData { users (limit: 1000) {
paymentDetails { # calls external API status } } } }
= ~1000s of REST requests!
More details at: https://www.ibm.com/blogs/research/2019/02/graphql-api-management/
Mai 18th 2020 23
API models
Mai 18th 2020 24
(http://people.cs.vt.edu/davisjam/downloads/publications/WitternChaDavisBaudartMandel-EmpiricalGraphQL-ICSOC19.pdf, summary at https://medium.com/swlh/empirical-study-graphql-icsoc19-29038c48da5)
Mai 18th 2020 25
Mai 18th 2020 26