Top Banner
GraphQL
44

Graphql Intro (Tutorial and Example)

Apr 16, 2017

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Graphql Intro (Tutorial and Example)

GraphQL

Page 2: Graphql Intro (Tutorial and Example)

GRAPHQL

WHAT IS GRAPHQL?

GraphQL is an application layer query language from Facebook.

GraphQL is a specification

Page 3: Graphql Intro (Tutorial and Example)

GRAPHQL

MOTIVATION

2012

Page 4: Graphql Intro (Tutorial and Example)

GRAPHQL

MOTIVATION

Lee Byron is an Engineer at Facebook working on GraphQL. He’s been making things at Facebook since 2008, including Immutable.js, Mobile & JavaScript.

Page 5: Graphql Intro (Tutorial and Example)

GRAPHQL

MOTIVATION

UP UNTIL 2012, NEWS FEED COULD ONLY BE REQUESTED AND DELIVERED AS HTML FROM OUR SERVERS. DURING THE EFFORT TO REBUILD NEWS FEED AS A NATIVE IOS VIEW WE HAD TO REVISIT THIS ARCHITECTURE TO GET RAW DATA

Lee Byron

Page 6: Graphql Intro (Tutorial and Example)

GRAPHQL

WHAT IS GRAPHQL? http://facebook.github.io/graphql

Page 7: Graphql Intro (Tutorial and Example)

GRAPHQL

Hello GraphQL

WHAT IS GRAPHQL?

Page 8: Graphql Intro (Tutorial and Example)

GRAPHQL

WHAT IS GRAPHQL?

Page 9: Graphql Intro (Tutorial and Example)

GRAPHQL

With GraphQL, you can define your backend as a well-defined graph-based

schema. Then client applications can query your dataset as they are needed.

WHAT IS GRAPHQL?

Page 10: Graphql Intro (Tutorial and Example)

GRAPHQL

So, you don't need to change your backend for data requirement changes in client apps. This simply solves one of the biggest problems in managing REST API.

WHAT IS GRAPHQL?

Page 11: Graphql Intro (Tutorial and Example)

GRAPHQL

WHY GRAPHQL?

Path Management

Hell

Page 12: Graphql Intro (Tutorial and Example)

GRAPHQL

GraphQL also allows client applications to batch and fetch data very efficiently. For an example, have a look at the following

GraphQL query:

WHAT IS GRAPHQL?

Page 13: Graphql Intro (Tutorial and Example)

GRAPHQL

WHAT IS GRAPHQL?

This is a GraphQL query to fetch data for a blog post with comments and author information

Page 14: Graphql Intro (Tutorial and Example)

GRAPHQL

WHAT IS GRAPHQL? Here's the result of the above query:

Page 15: Graphql Intro (Tutorial and Example)

GRAPHQL

WHAT IS GRAPHQL? GraphiQL

Page 16: Graphql Intro (Tutorial and Example)

GRAPHQL

▸ Declarative Query Language

▸ Hierarchical

▸ Product-centric

▸ Strong-typing

▸ Client-specified queries

WHAT IS GRAPHQL?

Page 17: Graphql Intro (Tutorial and Example)

GRAPHQL

WHAT IS GRAPHQL?

Page 18: Graphql Intro (Tutorial and Example)

GRAPHQL

WHAT IS GRAPHQL?

GraphQL is a query language for your API, and a server-side runtime for executing

queries by using a type system you define for your data

Page 19: Graphql Intro (Tutorial and Example)

GRAPHQL

WHAT IS GRAPHQL?

▸ Query (GET)

▸ Mutation (POST/PUT/DELETE)

GraphQL Operations

Page 20: Graphql Intro (Tutorial and Example)

GRAPHQL

STORY

A quick story before

Page 21: Graphql Intro (Tutorial and Example)

GRAPHQL

STORY

A Frontend Dev will start a new App and integrate with this

REST API

Page 22: Graphql Intro (Tutorial and Example)

GRAPHQL

STORY

Page 23: Graphql Intro (Tutorial and Example)

GRAPHQL

STORY

Design send the first layout for our Frontend Dev

Page 24: Graphql Intro (Tutorial and Example)

GRAPHQL

STORY

Page 25: Graphql Intro (Tutorial and Example)

GRAPHQL

STORY http://swapi.co/api/films/

Page 26: Graphql Intro (Tutorial and Example)

GRAPHQL

STORY

Page 27: Graphql Intro (Tutorial and Example)

GRAPHQL

STORY

Page 28: Graphql Intro (Tutorial and Example)

GRAPHQL

Let's write our first GraphQL query

QUERYING GRAPHQL

Page 29: Graphql Intro (Tutorial and Example)

GRAPHQL

QUERYING GRAPHQL

Page 30: Graphql Intro (Tutorial and Example)

GRAPHQL

Nested Querying

QUERYING GRAPHQL

Page 31: Graphql Intro (Tutorial and Example)

GRAPHQL

Arguments

QUERYING GRAPHQL

Page 32: Graphql Intro (Tutorial and Example)

GRAPHQL

Multiple fields

QUERYING GRAPHQL

Page 33: Graphql Intro (Tutorial and Example)

GRAPHQL

Assigning a result to a variable

QUERYING GRAPHQL

Page 34: Graphql Intro (Tutorial and Example)

GRAPHQL

Assigning a result to a variableQUERYING GRAPHQL

Page 35: Graphql Intro (Tutorial and Example)

GRAPHQL

Assigning a result to a variable

QUERYING GRAPHQL

Page 36: Graphql Intro (Tutorial and Example)

GRAPHQL

Mutations are the way to change the dataset behind GraphQL. A mutation is very similar to a

field in a GraphQL query, but GraphQL assumes a mutation has side effects and changes the dataset behind the schema.

INVOKING MUTATIONS

Page 37: Graphql Intro (Tutorial and Example)

GRAPHQL

First mutation

INVOKING MUTATIONS

Page 38: Graphql Intro (Tutorial and Example)

GRAPHQL

First mutation

INVOKING MUTATIONS

Page 39: Graphql Intro (Tutorial and Example)

GRAPHQL

Multiple mutations

INVOKING MUTATIONS

Page 40: Graphql Intro (Tutorial and Example)

GRAPHQL

Fragments are the way to group commonly used fields and reuse them.

FRAGMENTS

Page 41: Graphql Intro (Tutorial and Example)

GRAPHQL

So check this query. It's the same as above, but with fragments:

FRAGMENTS

Page 42: Graphql Intro (Tutorial and Example)

GRAPHQL

Fragments with nested fragmentsFRAGMENTS

Page 43: Graphql Intro (Tutorial and Example)

GRAPHQL

Using query variables

QUERY VARIABLES

Page 44: Graphql Intro (Tutorial and Example)

GRAPHQL

Using query variablesQUERY VARIABLES