Slide 1

Slide 1 text

Building a
 GraphQL Client in JavaScript @JOEKARLSSON1

Slide 2

Slide 2 text

I AM
 JOE KARLSSON @JOEKARLSSON1 @JOEKARLSSON

Slide 3

Slide 3 text

What Is GraphQL?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

REST GRAPHQL SQL MongoDB S3 SQL MongoDB S3

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Why Use GraphQL?

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

‣ DECLARATIVE ‣ DE-COUPLED FROM STORAGE ‣ VALIDATED AND STRUCTURED ‣ FACILITATES COLLABORATION ‣ SUPER FAST ‣ NO MORE API VERSIONING

Slide 12

Slide 12 text

GraphQL Timeline

Slide 13

Slide 13 text

2012 GRAPHQL CREATED AT FACEBOOK SUPPORT MOBILE NATIVE TEAMS

Slide 14

Slide 14 text

2015 OPEN SOURCED FIRST SPECIFICATION GRAPHQL.JS

Slide 15

Slide 15 text

2016 GITHUB ANNOUNCES GRAPHQL API NEW GRAPHQL WEBSITE FIRST GRAPHQL SUMMIT

Slide 16

Slide 16 text

2017 APOLLO CLIENT 2.0

Slide 17

Slide 17 text

2018 PRISMA 1.0 APOLLO ENGINE APOLLO CLIENT 2.1

Slide 18

Slide 18 text

Solution Architecture

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

Server Client Graph.Cool GraphQL Apollo Client React

Slide 21

Slide 21 text

GraphQL Schema

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

SCALAR TYPES: ‣ INT ‣ FLOAT ‣ STRING ‣ BOOLEAN ‣ ID Type System ENTRY POINTS: ‣ QUERY ‣ MUTATION

Slide 24

Slide 24 text

schema { query: Query, mutation: Mutation }

Slide 25

Slide 25 text

schema { query: Query, mutation: Mutation } type Query { allPosts(skip: Int, take: Int): [Post!]! }

Slide 26

Slide 26 text

schema { query: Query, mutation: Mutation } type Query { allPosts(skip: Int, take: Int): [Post!]! } type Post { id: ID! imageUrl: String! description: Boolean! }

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

GraphQL Playground

Slide 29

Slide 29 text

‣ All GraphiQL features ‣ Prettify, History, HTTP Headers ‣ Supports realtime
 subscriptions (Apollo) ‣ Standalone App

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

Apollo Client

Slide 33

Slide 33 text

UI COMPONENTS SQL MongoDB S3 GraphQL Query Requests Data Sends Result Updates UI

Slide 34

Slide 34 text

‣ DECLARATIVE DATA FETCHING ‣ ZERO-CONFIG CACHING ‣ COMBINES LOCAL 
 & REMOTE DATA ‣ VIBRANT ECOSYSTEM

Slide 35

Slide 35 text

Setup // client.js import { ApolloClient } from 'apollo-client' import { HttpLink } from 'apollo-link-http' import { InMemoryCache } from 'apollo-cache-inmemory' const httpLink = new HttpLink({ uri: ‘__GRAPHQL_ENDPOINT__', } const client = new ApolloClient({ link: httpLink, cache: new InMemoryCache(), })

Slide 36

Slide 36 text

// client.js import React from 'react' import ReactDOM from 'react-dom' import ListPage from './components/ListPage' import { ApolloProvider } from 'react-apollo' import { ApolloClient } from 'apollo-client' import { HttpLink } from 'apollo-link-http' import { InMemoryCache } from 'apollo-cache-inmemory' const httpLink = new HttpLink({ uri: '__SIMPLE_API_ENDPOINT__', }) const client = new ApolloClient({ link: httpLink, cache: new InMemoryCache(), }) ReactDOM.render( , document.getElementById('root'), );

Slide 37

Slide 37 text

Efficient Queries

Slide 38

Slide 38 text

import gql from 'graphql-tag' const ALL_POSTS_QUERY = gql` { allPosts(orderBy: createdAt_DESC) { id imageUrl description } } `

Slide 39

Slide 39 text

import React from 'react' import ListPage from './ListPage' import { Query } from 'react-apollo' import { ALL_POSTS_QUERY } from './graphql' import Loading from './Loading' const ListPageContainer = props => ( {({ loading, error, posts }) => { if (loading) return if (error) return `Error! ${error.message}` return }} ) export default ListPageContainer

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Effective Mutations

Slide 43

Slide 43 text

import gql from 'graphql-tag' export const CREATE_POST_MUTATION = gql` mutation CreatePostMutation($description: String!, $imageUrl: String!) { createPost(description: $description, imageUrl: $imageUrl) { id description imageUrl } } `

Slide 44

Slide 44 text

import React from 'react' import { Query, Mutation } from 'react-apollo' import { CREATE_POST_MUTATION, ALL_POSTS_QUERY } from './graphql' import CreatePage from './CreatePage' import Loading from './Loading' const CreatePageContainer = props => ( {({ loading, error, data }) => { if (loading) return if (error) return

`Error! ${error.message}`

return ( {createPost => ( )} ) }} ) export default CreatePageContainer

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

1) REST v GraphQL 2) Graph.Cool 3) Apollo 3) Queries 4) Mutations

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

THANK YOU!

Slide 50

Slide 50 text

Resources ‣ https://github.com/JoeKarlsson/graphql-apollo-demo ‣ https://www.graph.cool/ ‣ GraphQL Demo Repo ‣ GraphQL Official Docs ‣ GraphQL Hub ‣ Apollo Official Docs

Slide 51

Slide 51 text

LET’S STAY IN TOUCH @JOEKARLSSON1 @JOEKARLSSON