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

Getting Started with GraphQL and Atlas | MongoD...

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.
Avatar for Joe Karlsson Joe Karlsson
January 22, 2020

Getting Started with GraphQL and Atlas | MongoDB .Local SF 2020

GraphQL's principles of simply describing the data you want and getting predictable results have led to an explosion in popularity – replacing REST APIs more and more. With Atlas' new GraphQL service it's simple to generate a schema and API on top of your data, helping you connect your data and application quicker than ever. Today we'll show you the process step-by-step and give you a sneak peak at what's coming next!

Avatar for Joe Karlsson

Joe Karlsson

January 22, 2020
Tweet

More Decks by Joe Karlsson

Other Decks in Programming

Transcript

  1. { name: “Joe Karlsson”, company: “MongoDB”, title: [ “Developer Advocate”,

    “Software Engineer” ], } twitter: “@JoeKarlsson1”, twitch: “joe_karlsson”, website: “joekarlsson.com”, opinions: “my own”, links: “bit.ly/GraphQLAtlas” bit.ly/ GraphQLAtlas
  2. @JoeKarlsson1 Control the shape and content of your documents {


    "properties": {
 "_id": {
 "bsonType": "objectId"
 },
 "description": {
 "bsonType": "string"
 },
 "imageUrl": {
 "bsonType": "string"
 }
 }
 }
  3. @JoeKarlsson1 WHAT I NEEDED { "name": "Joe Karlsson", "company": "MongoDB",

    "title": [ "Developer Advocate", "Software Engineer" ] } GET /api/user/JoeKarlsson
  4. @JoeKarlsson1 ‣ What is GraphQL? ‣ REST vs GraphQL ‣

    Why use GraphQL? ‣ Declarative Getting Started with GraphQL
  5. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Declarative Getting Started with

    GraphQL ‣ Why use GraphQL? ‣ Validated and structured ‣ What is GraphQL? {
 "properties": {
 "_id": {
 "bsonType": "objectId"
 },
 "description": {
 "bsonType": "string"
 },
 "imageUrl": {
 "bsonType": "string"
 }
 }
 }
  6. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Validated and structured ‣

    Facilitates Collaboration Getting Started with GraphQL ‣ Why use GraphQL? ‣ Declarative ‣ UNIFIES ALL API ENDPOINTS ‣ What is GraphQL?
  7. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Validated and structured ‣

    Facilitates Collaboration Getting Started with GraphQL ‣ Why use GraphQL? ‣ Declarative ‣ UNIFIES ALL API ENDPOINTS ‣ What is GraphQL? ‣ NO MORE WRITING DOCUMENTATION
  8. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Validated and structured ‣

    Facilitates Collaboration Getting Started with GraphQL ‣ Why use GraphQL? ‣ Declarative ‣ UNIFIES ALL API ENDPOINTS ‣ What is GraphQL? ‣ NO MORE WRITING DOCUMENTATION ‣ NO MORE API VERSIONING
  9. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Validated and structured ‣

    Facilitates Collaboration Getting Started with GraphQL ‣ Why use GraphQL? ‣ Declarative ‣ What is GraphQL? ‣ Makes working with data easy
  10. import { HttpLink, InMemoryCache, ApolloClient } from 'apollo-boost'; import {

    setContext } from 'apollo-link-context'; import CONFIG from '../config'; // Add an Authorization header to each GraphQL request const authLink = setContext((_, { headers }) => ({ headers: { ...headers, Authorization: `Bearer ${CONFIG.accessToken}`, }, })); // Connect Apollo to the GraphQL Endpoint const GRAPHQL_URL = `https://stitch.mongodb.com/api/client/v2.0/app/${STICH_APP_ID}/graphql`; const httpLink = new HttpLink({ uri: GRAPHQL_URL }); // Instantiate the Apollo Client const client = new ApolloClient({ link: authLink.concat(httpLink), cache: new InMemoryCache(), }); Setup Apollo with GraphQL and Atlas
  11. Connect Apollo to React Provider function InstaPostApp() { return (

    <ApolloProvider client={client}> <App /> </ApolloProvider> ); } import { HttpLink, InMemoryCache, ApolloClient } from 'apollo-boost'; import { setContext } from 'apollo-link-context'; import CONFIG from '../config'; // Add an Authorization header to each GraphQL request const authLink = setContext((_, { headers }) => ({ headers: { ...headers, Authorization: `Bearer ${CONFIG.accessToken}`, }, })); // Connect Apollo to the GraphQL Endpoint const GRAPHQL_URL = `https://stitch.mongodb.com/api/client/v2.0/app/${STICH_APP_ID}/graphql`; const httpLink = new HttpLink({ uri: GRAPHQL_URL }); // Instantiate the Apollo Client const client = new ApolloClient({ link: authLink.concat(httpLink), cache: new InMemoryCache(), }); import React, { Fragment } from 'react'; import { ApolloProvider } from '@apollo/react-hooks';
  12. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Introduced InstaPosts + +
  13. @JoeKarlsson1 ‣ What is GraphQL? ‣ What is Apollo? ‣

    Introduced InstaPosts ‣ How does Apollo work? Getting Started with GraphQL
  14. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Introduced InstaPosts ‣ How does Apollo work? ‣ Setup Apollo with GraphQL and Atlas // Connect Apollo to the GraphQL Endpoint const GRAPHQL_URL = `https://stitch.mongodb.com/api/client/v2.0/app/${STICH_APP_ID}/graphql`; const httpLink = new HttpLink({ uri: GRAPHQL_URL }); // Instantiate the Apollo Client const client = new ApolloClient({ link: authLink.concat(httpLink), cache: new InMemoryCache(), });
  15. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Introduced InstaPosts ‣ How does Apollo work? ‣ Setup Apollo with GraphQL and Atlas function InstaPostApp() { return ( <ApolloProvider client={client}> <App /> </ApolloProvider> ); } ‣ Connected Apollo to React
  16. @JoeKarlsson1 CONSTRUCTING A GRAPHQL QUERY query { } instaposts {

    } 3) What data do you want? _id description imageUrl
  17. Querying GraphQL with Apollo and React import React, { Fragment

    } from 'react'; import { useQuery } from '@apollo/react-hooks'; import gql from 'graphql-tag'; import ListPage from './ListPage'; const FIND_ALL_INSTAPOSTS = gql` query { instaposts { _id description imageUrl } } `; function ListPageContainer(props) { const { loading, error, data } = useQuery(FIND_ALL_INSTAPOSTS, { pollInterval: 500, }); } export default ListPageContainer; return ( <Fragment> {loading && <div>Loading…<div/>} {error && <div>{`encountered an error: ${error}`}</div>} {data && <ListPage {...data} {...props} />} </Fragment> );
  18. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries
  19. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries ‣ MongoDB automatically generates the queries
  20. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries ‣ MongoDB automatically generates the queries ‣ How to construct a GraphQL query query { instaposts { _id description imageUrl } } Query or a Mutation? What do you want to query? What data do you want?
  21. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries ‣ MongoDB automatically generates the queries ‣ How to construct a GraphQL query ‣ Querying GraphQL with Apollo and React function ListPageContainer(props) { const { loading, error, data } = useQuery(FIND_ALL_INSTAPOSTS, { pollInterval: 500, }); return ( <Fragment> {loading && <div>Loading…<div/>} {error && <div>{`encountered an error: ${error}`}</div>} {data && <ListPage {...data} {...props} />} </Fragment> ); }
  22. mutation ( ) { } @JoeKarlsson1 CONSTRUCTING A GRAPHQL MUTATION

    Operation name describes the GraphQL String CreatePostMutation
  23. @JoeKarlsson1 CONSTRUCTING A GRAPHQL MUTATION mutation CreatePostMutation( ) { }

    Arguments allow you to pass data to fields $data: InstapostInsertInput! Argument Type. This is defined by the GraphQl server
  24. @JoeKarlsson1 CONSTRUCTING A GRAPHQL MUTATION mutation CreatePostMutation( ) { }

    $data: InstapostInsertInput! Bang - This means the argument is required.
  25. mutation CreatePostMutation( ) { } insertOneInstapost (data: $data) { }

    @JoeKarlsson1 CONSTRUCTING A GRAPHQL MUTATION Mutation type. This is defined by the GraphQl server $data: InstapostInsertInput! Arguments
  26. insertOneInstapost (data: $data) { } @JoeKarlsson1 CONSTRUCTING A GRAPHQL MUTATION

    mutation CreatePostMutation( ) { } What data do you returned, once the data has been mutated? $data: InstapostInsertInput! _id description imageUrl
  27. @JoeKarlsson1 CONSTRUCTING A GRAPHQL MUTATION mutation CreatePostMutation($data: InstapostInsertInput!) { insertOneInstapost(data:

    $data) { _id description imageUrl } } Query or a Mutation? Operation name Arguments Mutation Type What data do you want?
  28. GraphQL Mutations with Apollo and React function CreatePage(props) { let

    imageUrl = { value: '' }; let description = ''; const [addPost] = useMutation(ADD_INSTAPOST); return ( ); } import React from 'react'; import { useMutation } from '@apollo/react-hooks'; import gql from 'graphql-tag'; // CreatePage.js const ADD_INSTAPOST = gql` mutation CreatePostMutation($data: InstapostInsertInput!) { insertOneInstapost(data: $data) { _id description imageUrl } } `; <form onSubmit={e => { e.preventDefault(); // Invoke the mutation method // and pass in the required variables addPost({ variables: { data: { description: description.value, imageUrl: imageUrl.value, }, }, }); }} > <input id=“imageUrl" ref={url => { imageUrl = url; }} /> <input id="description" ref={str => { description = str; }} /> <button type=“submit">Post</button> </form>
  29. @JoeKarlsson1 ‣ What is GraphQL? ‣ REST vs GraphQL ‣

    Why use GraphQL? ‣ Declarative Getting Started with GraphQL
  30. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Declarative Getting Started with

    GraphQL ‣ Why use GraphQL? ‣ Validated and structured ‣ What is GraphQL? {
 "properties": {
 "_id": {
 "bsonType": "objectId"
 },
 "description": {
 "bsonType": "string"
 },
 "imageUrl": {
 "bsonType": "string"
 }
 }
 }
  31. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Validated and structured ‣

    Facilitates Collaboration Getting Started with GraphQL ‣ Why use GraphQL? ‣ Declarative ‣ UNIFIES ALL API ENDPOINTS ‣ What is GraphQL?
  32. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Validated and structured ‣

    Facilitates Collaboration Getting Started with GraphQL ‣ Why use GraphQL? ‣ Declarative ‣ UNIFIES ALL API ENDPOINTS ‣ What is GraphQL? ‣ NO MORE WRITING DOCUMENTATION
  33. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Validated and structured ‣

    Facilitates Collaboration Getting Started with GraphQL ‣ Why use GraphQL? ‣ Declarative ‣ UNIFIES ALL API ENDPOINTS ‣ What is GraphQL? ‣ NO MORE WRITING DOCUMENTATION ‣ NO MORE API VERSIONING
  34. @JoeKarlsson1 ‣ REST vs GraphQL ‣ Validated and structured ‣

    Facilitates Collaboration Getting Started with GraphQL ‣ Why use GraphQL? ‣ Declarative ‣ What is GraphQL? ‣ Makes working with data easy
  35. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Introduced InstaPosts + +
  36. @JoeKarlsson1 ‣ What is GraphQL? ‣ What is Apollo? ‣

    Introduced InstaPosts ‣ How does Apollo work? Getting Started with GraphQL
  37. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Introduced InstaPosts ‣ How does Apollo work? ‣ Setup Apollo with GraphQL and Atlas // Connect Apollo to the GraphQL Endpoint const GRAPHQL_URL = `https://stitch.mongodb.com/api/client/v2.0/app/${STICH_APP_ID}/graphql`; const httpLink = new HttpLink({ uri: GRAPHQL_URL }); // Instantiate the Apollo Client const client = new ApolloClient({ link: authLink.concat(httpLink), cache: new InMemoryCache(), });
  38. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Introduced InstaPosts ‣ How does Apollo work? ‣ Setup Apollo with GraphQL and Atlas function InstaPostApp() { return ( <ApolloProvider client={client}> <App /> </ApolloProvider> ); } ‣ Connected Apollo to React
  39. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries
  40. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries ‣ MongoDB automatically generates the queries
  41. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries ‣ MongoDB automatically generates the queries ‣ How to construct a GraphQL query query { instaposts { _id description imageUrl } } Query or a Mutation? What do you want to query? What data do you want?
  42. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries ‣ MongoDB automatically generates the queries ‣ How to construct a GraphQL query ‣ Querying GraphQL with Apollo and React function ListPageContainer(props) { const { loading, error, data } = useQuery(FIND_ALL_INSTAPOSTS, { pollInterval: 500, }); return ( <Fragment> {loading && <div>Loading…<div/>} {error && <div>{`encountered an error: ${error}`}</div>} {data && <ListPage {...data} {...props} />} </Fragment> ); }
  43. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries ‣ Mutating data with GraphQL
  44. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries ‣ Mutating data with GraphQL ‣ Constructing a GraphQL Mutation mutation CreatePostMutation($data: InstapostInsertInput!) { insertOneInstapost(data: $data) { _id description imageUrl } } Query or a Mutation? Operation name Arguments Mutation Type What data do you want?
  45. Getting Started with GraphQL @JoeKarlsson1 ‣ What is GraphQL? ‣

    What is Apollo? ‣ Making effective queries ‣ Mutating data with GraphQL ‣ Constructing a GraphQL Mutation ‣ GraphQL mutations with Apollo and React function CreatePage(props) { let imageUrl = { value: '' }; let description = ''; const [addPost] = useMutation(ADD_INSTAPOST); return ( <form onSubmit={e => { e.preventDefault(); addPost({ variables: { data: { description: description.value, imageUrl: imageUrl.value, }, }, }); }} > <input id="imageUrl" ref={url => { imageUrl = url; }} /> <input id="description" ref={str => { description = str; }} /> <button type="submit">Post</button> </form> ); }
  46. All resources and links used in this talk can be

    found here: @JoeKarlsson1 bit.ly/GraphQLAtlas
  47. { name: “Joe Karlsson”, company: “MongoDB”, title: [ “Developer Advocate”,

    “Software Engineer” ], twitter: “@JoeKarlsson1”, twitch: “joe_karlsson”, website: “joekarlsson.com”, links: “bit.ly/GraphQLAtlas” } bit.ly/ GraphQLAtlas
  48. Addtional Resources ‣Link to all resources: bit.ly/GraphQLAtlas ‣Source Code: github.com/JoeKarlsson/mongodb-graphql-demo

    ‣GraphQL Official Docs: graphql.org ‣GraphQL Hub: graphqlhub.com ‣Apollo Official Docs: apollographql.com @JoeKarlsson1 bit.ly/GraphQLAtlas