Grap aphQL hQL – forget (the) REST?
Christian Schwendtner
Grap aphQL hQL forget (the) REST? Christian Schwendtner - - PowerPoint PPT Presentation
Grap aphQL hQL forget (the) REST? Christian Schwendtner https://www.pexels.com/photo/man-person-people-emotions-1990/ https://www.pexels.com/photo/man-person-people-emotions-1990/ REST Feelings Im lovin it Well, What else?
Christian Schwendtner
https://www.pexels.com/photo/man-person-people-emotions-1990/
https://www.pexels.com/photo/man-person-people-emotions-1990/
REST – Feelings
I’m lovin’ it Well, … What else?
Agenda
Let’s talk about REST Challenges Why What How GraphQL in Action GraphQL Features GraphQL on the Server GraphQL on the Client
https://www.pexels.com/photo/adult-casual-collection-fashion-296881/
Richardson Maturity Model
https://martinfowler.com/articles/richardsonMaturityModel.html
Level 0: The Swamp of POX Level 1: Resources Level 2: HTTP Verbs Level 3: Hypermedia Controls Glory y of REST ST
The BookStore aka “ Amazon (very) light”
Books
Algorithms to Live By Brian Christian Cracking the Coding Interview Gayle Laakmann McDowell Life 3.0 Max T egmark
Book Author
*
1
Interaction
Books
Algorithms to Live By Brian Christian Cracking the Coding Interview Gayle Laakmann McDowell Life 3.0 Max T egmark GET /books
[ book: { title, …, authorURL } ]
GET /authors/1
{ firstName, lastName, … }
GET /authors/14
{ firstName, lastName, … }
GET /authors/98
{ firstName, lastName, … }
Server
https://www.pexels.com/photo/adult-alcohol-blur-celebrate-290316/ https://www.pexels.com/photo/time-lapse-photography-of-water-bobbling-beside-lemon-fruit-161425/
“Custom Endpoints”?
GET /books_overview GET /books?dataset=books_with_authors GET /books?dataset=books_with_authors_with_ratings GET /books?dataset=books_with_authors_with_all_i_will_ever_need GET /books?dataset=books_with_authors_with_all_i_will_ever_need2
“Custom Endpoints”
Multiple views? Multiple client types? Multiple client versions? Private / Public API GET /books?include=book.title,author.lastName,author.firstName
A query language for your API
Just st for once, ce, let t me me look k on you with th my my own n eyes es. . An Anakin kin Skywal ywalker er
https://pixabay.com/en/star-wars-warrior-space-cinema-2473201/
GraphiQL
GraphQL is …
Specification Reference implementation Schema (domain specific) Independent of the data store Independent of the transport protocol
GraphQL Operations
Query Read Mutation Write Subscription Notification (“Event”)
Fields
query { books { title author { firstName lastName } } }
Arguments
query { books(first: 2) { title author { firstName lastName } } }
Aliases
query { books { title author { firstName lastName smallPic: profilePic(size: SMALL) largePic: profilePic(size: LARGE) } } }
Fragments
fragment authorFragment on Author { firstName lastName } query { books { title author { ...authorFragment } } }
Variables
query($limit: Int) { books(first: $limit) { title author { firstName lastName } } }
Validation / Errors
Introspection
Mutations
mutation { voteBook(bookId: 1) { id votes } }
https://pixabay.com/en/star-wars-lightsaber-laser-sword-2369317/
Schema
type Book { id: Int! title: String ... author: Author } type Author { id: Int! firstName: String lastName: String ... books: [Book] }
type Query { books(first: Int): [Book] authors: [Author] } type Mutation { voteBook(bookId: Int!): Book }
Resolvers
books() title() title() author
} author() author() “Algorithms …” firstName() lastName() query { books { title author { firstName lastName } } } “Cracking the ...” author
} “Brian”
“Gayle”
“Laak akmann mann” “Christian” [ b book1 k1 { } }, book2 k2 { } }, … ] firstName() lastName()
Execution
query { books { title author { firstName lastName } } } db.getAllBooks() db.getAuthorForBook(1) db.getAuthorForBook(2) db.getAuthorForBook(3) db.getAuthorForBook(4) db.getAuthorForBook(5) db.getAuthorForBook(6) Caching Batching DataLoader
https://github.com/facebook/dataloader
Client
GET / POS OST Relay Client
https://facebook.github.io/relay https://www.apollographql.com/client
UI - Structure
Books
Algorithms to Live By Brian Christian Cracking the Coding Interview Gayle Laakmann McDowell Life 3.0 Max T egmark
<book-list> <book-info> <book-info> <author-info> <book-info> <author-info>
<author-info>
Version 1
<book-list> query { books { title author { firstName lastName profilePic } } } <book-info> <author-info>
Version 2
<book-list> query bookList { books { ...book } } <book-info> <author-info> fragment book
title author { ...author } } fragment author
firstName lastName profilePic }
<author-info>
fragment author
firstName lastName profilePic } export interface authorFragment = { firstName: string | null, lastName: string | null, profilePic: string | null, };
TypeScript
codegen
https://www.pexels.com/photo/man-couple-people-woman-343/
One last note: GraphQL as Gateway
Service A GraphQL Client Service B Service C
GraphQL – REST in peace
https://www.pexels.com/photo/photo-of-a-hippie-woman-736603/
Christian Schwendtner PROGRAMMIERFABRIK GmbH
@CSchwendtner
https://www.pexels.com/photo/adult-chill-computer-connection-450271/