Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building a GraphQL Client in JavaScript

Building a GraphQL Client in JavaScript

We will answer the following questions. What is the philosophy behind GraphQL? How do you architect a scalable schema? How can GraphQL boost productivity? How can you avoid common pitfalls?

We will then get a GraphQL server up and running together, while focusing on exploring real world patterns for architecting our schema. We will discuss and implement practical steps to improve query performance, error handling and caching.

For this talk, attendees will leave with a working GraphQL server that they have built themselves.

Joe Karlsson

August 09, 2018
Tweet

More Decks by Joe Karlsson

Other Decks in Programming

Transcript

  1. ‣ DECLARATIVE ‣ DE-COUPLED FROM STORAGE ‣ VALIDATED AND STRUCTURED

    ‣ FACILITATES COLLABORATION ‣ SUPER FAST ‣ NO MORE API VERSIONING
  2. SCALAR TYPES: ‣ INT ‣ FLOAT ‣ STRING ‣ BOOLEAN

    ‣ ID Type System ENTRY POINTS: ‣ QUERY ‣ MUTATION
  3. schema { query: Query, mutation: Mutation } type Query {

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

    allPosts(skip: Int, take: Int): [Post!]! } type Post { id: ID! imageUrl: String! description: Boolean! }
  5. ‣ All GraphiQL features ‣ Prettify, History, HTTP Headers ‣

    Supports realtime
 subscriptions (Apollo) ‣ Standalone App
  6. 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(), })
  7. // 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( <ApolloProvider client={client}> <ListPage /> </ApolloProvider>, document.getElementById('root'), );
  8. 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 => ( <Query query={ALL_POSTS_QUERY} pollInterval={500}> {({ loading, error, posts }) => { if (loading) return <Loading /> if (error) return `Error! ${error.message}` return <ListPage {... posts} {...props} /> }} </Query> ) export default ListPageContainer
  9. 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 } } `
  10. 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 => ( <Query query={ALL_POSTS_QUERY}> {({ loading, error, data }) => { if (loading) return <Loading /> if (error) return <p>`Error! ${error.message}`</p> return ( <Mutation mutation={CREATE_POST_MUTATION}> {createPost => ( <CreatePage {…data} {…props} createPost={createPost} /> )} </Mutation> ) }} </Query> ) export default CreatePageContainer