client server communication with graphql
play

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?


  1. 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

  2. Learning goals • What actually is GraphQL, and how came it about? • How does GraphQL work? • What are some benefits vs. challenges for GraphQL? Mai 18 th 2020 2

  3. Background & Overview Mai 18th 2020 3

  4. In 2012, Facebook faced a problem 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. Mai 18 th 2020 4 Source: https://reactjs.org/blog/2015/05/01/graphql-introduction.html

  5. GraphQL shifts control over what data is returned (or mutated) to clients Providers define their data Clients send queries Servers respond with data types at design time at runtime at runtime { "data" : { query { type User { "me" : { me { name: String "name" : "Erik" name age: Int } } } } } } type Query { me: User } Mai 18 th 2020 5

  6. Demo https://www.github.com/ErikWittern/graphql-demo Mai 18th 2020 6

  7. So, what is GraphQL? • A query language for networked APIs… • …and a runtime for servers to fulfill queries • Specification + reference implementation in JavaScript • Clients send type-checked queries, servers respond with requested data: Request: POST {"query": …} DB POST {"introspection": …} API Server {"schema": …} GraphQL client (here: Graph i QL) Response: {"data": …} Mai 18 th 2020 7

  8. 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

  9. Language & Runtime Mai 18th 2020 9

  10. Anatomy of a GraphQL query (selected concepts) Operation type Operation name Variable definition query fetchGraphQLData ($details: Boolean!) { Field selection 2 Arguments repository(owner: "graphql", name: "graphql-js") { Fragment spread Directive ...repoDetails @include(if: $details) issueOrPullRequest(number: 10) { Selection set Type condition ...on Issue { Inline updatedAt fragment } } } } fragment repoDetails on Repository { repoName: name Alias Fragment description definition } Mai 18th 2020 10 More details at: http://spec.graphql.org/draft/#sec-Document-Syntax

  11. Defining schemas with the schema definition language (SDL) schema { Schema Root operation query: Query definition type definition } type Query { Name Argument definition Type Object type definition users(limit: Int!): [User] Field definition } Name Directive locations Directive directive @upperCase on FIELD_DEFINITION definition enum Status { ACTIVE Enum type INACTIVE definition } Directive type User { name: String @upperCase """Description of field User.status""" status: Status } Mai 18th 2020 11 More details at: http://spec.graphql.org/draft/#sec-Document-Syntax

  12. Query exeuction on a (HTTP) server HTTP server DB GraphQL execution engine Resolver functions API (contained in schema object) GraphQL middleware Coerce variable Extract request Build response selection sets Parse query (against schema) parameters Evaluate Validate values {"query": … } string {"errors": … } {"data": … , "errors": … } Mai 18th 2020 12

  13. Advanced Query Concepts Mai 18th 2020 13

  14. Introspection query IntrospectionQuery { • Introspection is a mechanism for clients to learn (at __schema { runtime) about the data types and operations a queryType { name } mutationType { name } GraphQL server offers subscriptionType { name } types { ...FullType • An introspection query is a plain-old GraphQL } query… directives { name • …that happens to select meta-fields provided by locations introspection types args { ...InputValue } } • Client-tools like GraphiQL rely on introspection for: } Showing documentation about types & operations } • Client-side query validation • ... Directive Definitions ... Auto-completion when typing queries • Etc. • Mai 18th 2020 14

  15. Demo https://developer.github.com/v4/explorer/ Mai 18th 2020 15

  16. Pagination with slicing arguments and offset query fetchPage2 { • Pagination aims to return different parts user { (or slices ) of long lists of data name friends(last: 5, offset: 5) { name • Slicing arguments (often named max , } } } limit , first , or last ) define length of page 1 page 2 slice to return • Often combined with an “offset” 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 offset slice • One problem: this approach may return new returned twice items twice when list updates page 1 page 2 … 20 19 18 17 16 15 15 14 14 13 13 12 11 10 9 8 7 6 5 4 offset slice Mai 18th 2020 16

  17. Pagination with Cursor Connections query fetchPage2 { • Cursor Connections rely on… user { name • Fields using slicing arguments… friends(last: 5, after: "opaqueCursor") { pageInfo { • …return a Connection with fields hasNextPage pageInfo and edges … } edges { • …where each Edge has fields cursor cursor node { and node , containing the actual object. name } } } } } • Robust to list-updates outside the slice during paginating new page 1 page 2 • Think of a common Facebook’s use-case: … 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 news feed , where mostly items are added cursor slice Mai 18th 2020 17 More details about cursor connections at: https://relay.dev/graphql/connections.htm (also source of the example above)

  18. Pros & Cons Mai 18th 2020 18

  19. GraphQL benefits for clients In-sync documentation of type system Predictable responses Static typing (auto- complete, validation) Fewer roundtrips No over- fetching Mai 18 th 2020 19

  20. GraphQL benefits for providers • Happy API consumers (!) • Simplified maintenance DB • Serve clients with diverse, changing requirements with a single endpoint API Server • 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

  21. Challenge: HTTP caching of GraphQL requests Application • Problems with typical HTTP proxy / cache DB gateway caches include: • Often, non-safe & non-idempotent POST is API Server Gateway used to send (large) queries cache • Some queried fields may become stale Proxy sooner than others, making it hard to cache define Cache-Control / Last- provider modified headers network Client-side • Alternatives include: cache • Cache persisted queries in proxy or gateway consumer • Client-side caching based on ID field • Application caches in the data-layer (“DataLoaders”) or resolver functions Mai 18th 2020 21 More details at: https://www.apollographql.com/blog/graphql-caching-the-elephant-in-the-room-11a3df0c23ad

  22. Challenge: rate-limiting & threat prevention query fetchAllTheData { • Servers may need to deal with users (limit: 1000) { excessive queries sent by clients orders (first: 1000) { paymentDetails { # calls external API • Rate-limiting - and not “x requests per status time-interval” } • Pricing requests } } • Blocking (inadvertently) threatening } requests = ~1000s of REST requests! • Options include: • Timeouts against threatening requests • Dynamic analysis • Static analysis • Query “depth” or “nesting” • Query “cost” or “complexity” Mai 18th 2020 22 More details at: https://www.ibm.com/blogs/research/2019/02/graphql-api-management/

  23. Wrap-up Mai 18th 2020 23

  24. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend