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

Building_a_GraphQL_Client_in_JavaScript_-_Prair...

 Building_a_GraphQL_Client_in_JavaScript_-_Praire_Dev_Con.pdf

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 workshop, attendees will leave with a working GraphQL server that they have built themselves.

My name is Joe Karlsson, and I am a Software Engineer from the frozen tundra known as Minneapolis, Minnesota. I write code, build teams and give talks. You can follow me on:

Twitter - @JoeKarlsson1
Github - @JoeKarlsson
LinkedIn - /in/joekarlsson
Website - joekarlsson.com

Joe Karlsson

October 17, 2018
Tweet

More Decks by Joe Karlsson

Other Decks in Technology

Transcript

  1. schema { query: Query, mutation: Mutation } type Query {

    allPosts: [Post!]! } type Post { id: ID! imageUrl: String! description: Boolean! } @JoeKarlsson1
  2. // server/graphql/schema.js const ToDoMongo = require('../database/Todo') schema { query: Query,

    mutation: Mutation } type Query { allPosts: [Post!]! } type Post { id: ID! imageUrl: String! description: Boolean! resolve: (itemId) => { const foundPosts = new Promise((resolve, reject) => { postMongo.find({ itemId }, (err, posts) => { err ? reject(err) : resolve(posts); }); }); return foundPosts; }, } @JoeKarlsson1
  3. ‣ All GraphiQL features ‣ Prettify, History, HTTP Headers ‣

    Supports realtime
 subscriptions (Apollo) ‣ Standalone App @JoeKarlsson1
  4. DECLARATIVE DATA FETCHING @JoeKarlsson1 const Feed = () => (

    <Query query={GET_DOGS}> {({ loading, error, data }) => { if (error) return <Error /> if (loading || !data) return <Fetching /> return <DogList dogs={data.dogs} /> }} </Query> )
  5. @JoeKarlsson1 ZERO-CONFIG CACHING import ApolloClient from 'apollo-boost' // the Apollo

    cache is set up automatically const client = new ApolloClient()
  6. @JoeKarlsson1 COMBINES LOCAL & REMOTE DATA query GetDogByBreed($breed: String!) {

    dog(breed: $breed) { images { url id isLiked @client } } } `;
  7. VIBRANT ECOSYSTEM @JoeKarlsson1 ‣ Apollo Link community links: Pluggable links

    created by the community ‣ apollo-cache-persist: Simple persistence for your Apollo cache (@jamesreggio) ‣ apollo-storybook-decorator: Wrap your React Storybook stories with Apollo Client (@abhiaiyer91) ‣ AppSync by AWS: Amazon’s real-time GraphQL client uses Apollo Client under the hood
  8. 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(), }) @JoeKarlsson1
  9. // 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'), ); @JoeKarlsson1
  10. import gql from 'graphql-tag' const ALL_POSTS_QUERY = gql` { allPosts(orderBy:

    createdAt_DESC) { id imageUrl description } } ` @JoeKarlsson1
  11. 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 @JoeKarlsson1
  12. 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 } } ` @JoeKarlsson1
  13. 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 @JoeKarlsson1
  14. Resources ‣ https://github.com/JoeKarlsson/graphql-apollo-demo ‣ https://www.graph.cool/ ‣ GraphQL Demo Repo ‣

    GraphQL Official Docs ‣ GraphQL Hub ‣ Apollo Official Docs @JoeKarlsson1