Slide 1

Slide 1 text

Introduction to GraphQL Software Developer, Newlogic @arnellebalane

Slide 2

Slide 2 text

What is GraphQL?

Slide 3

Slide 3 text

a query language for our APIs, and a runtime for fulfilling these queries

Slide 4

Slide 4 text

a query language for our APIs, and a runtime for fulfilling these queries

Slide 5

Slide 5 text

GraphQL is a query language It gives clients the ability to ask for the exact data that they need, and nothing more runtime It provides a complete and understandable description of the data available in the API, and defines how to get those data

Slide 6

Slide 6 text

query { post(id: 1) { id content comments { content } } } { "data": { "post": { "id": "abc123", "content": "Hello GraphQL!", "comments": [{ "content": "Hey!" }, { "content": "Yoooo!" }] } } }

Slide 7

Slide 7 text

it’s just a new way to send data over HTTP

Slide 8

Slide 8 text

it’s just a new way to send data over HTTP ( ehem... REST )

Slide 9

Slide 9 text

GraphQL vs REST ( part one )

Slide 10

Slide 10 text

Both have the idea of a resource, which is the piece of data in our API. Usually these resources have their own IDs They’re similar! Both can fetch resources via HTTP with a URL Both can return a JSON representation of the resource being fetched

Slide 11

Slide 11 text

REST But also different! GraphQL The server determines what fields are returned to the client when fetching a resource The server returns only the fields that were queried by the client

Slide 12

Slide 12 text

The available resources are described in a “schema” ● A schema is a typed representation of the resources, their fields, and their relationships with each other Each resource is identified by a URL REST But also different! GraphQL example.com/api/posts example.com/api/posts/1 example.com/api/posts/1/comments

Slide 13

Slide 13 text

type Query { posts: [Post] post(id: ID!): Post } type Post { id: ID! content: String comments: [Comment] } type Comment { id: ID! content: String }

Slide 14

Slide 14 text

Both need to differentiate if the request is meant to read or write data They’re similar! Both provide a way to interact with our API data

Slide 15

Slide 15 text

REST But also different! Interact with API data using a list of available root queries ● “Which query should I use?” Interact with API data using a list of available endpoints ● “Which endpoint should I use?” GraphQL

Slide 16

Slide 16 text

REST But also different! Interact with API data using a list of available root queries ● “Which query should I use?” Interact with API data using a list of available endpoints ● “Which endpoint should I use?” GraphQL Use HTTP methods (i.e. GET, POST, etc.) to differentiate read/write operations Use query to read data, use mutation to write data

Slide 17

Slide 17 text

They’re similar! Both eventually call a function in the server when a request is received

Slide 18

Slide 18 text

Route handlers are called when requests are received REST But also different! GraphQL app.get('/books/:id', (req, res) => { res.json(book); }) Resolver functions are called corresponding to the fields requested ● Each field in the GraphQL schema has a corresponding “resolver” function which returns the value for that field Each request can call multiple field resolvers from different resources

Slide 19

Slide 19 text

GraphQL syntax

Slide 20

Slide 20 text

bit.ly/graphql-playground graphql.org/learn/queries

Slide 21

Slide 21 text

Queries ● Fields ● Arguments ● Aliases Summary Mutations Subscriptions ● Fragments ● Variables ● Operation Names

Slide 22

Slide 22 text

Get the content, number of likes, and author email for posts with ID of 1 and 3

Slide 23

Slide 23 text

Add yourself as a new user, then create a new post as that user

Slide 24

Slide 24 text

Subscribe to the latest posts, showing their content and creation date

Slide 25

Slide 25 text

GraphQL schema and resolvers

Slide 26

Slide 26 text

bit.ly/fdc-graphql-workshop graphql.org/learn/schema

Slide 27

Slide 27 text

Add a new root query field that will return all users

Slide 28

Slide 28 text

Add a new mutation for creating a new comment for a given post using a given user

Slide 29

Slide 29 text

GraphQL in our apps

Slide 30

Slide 30 text

● Apollo Client ● Relay ● … and many more Server libraries/services graphql.org/code ● Apollo Server ● Express GraphQL ● Hasura Client libraries

Slide 31

Slide 31 text

bit.ly/fdc-apollo-react apollographql.com/docs/react/get-started/

Slide 32

Slide 32 text

GraphQL vs REST ( part two )

Slide 33

Slide 33 text

REST is years ahead of GraphQL Tools and integrations, e.g. for monitoring, alerting, logging, etc. Versioning, authentication, authorization, file uploads, caching, etc. GraphQL requests can’t be cached as easily as REST requests

Slide 34

Slide 34 text

GraphQL Introspection GraphQL Subscriptions Apollo Graph Manager

Slide 35

Slide 35 text

Should you use GraphQL?

Slide 36

Slide 36 text

Thank you! Software Developer, Newlogic @arnellebalane