├── .gitignore ├── .prettierrc ├── LICENSE ├── README.md ├── gatsby-browser.js ├── gatsby-config.js ├── gatsby-ssr.js ├── netlify.toml ├── package.json ├── src ├── apollo │ ├── client.js │ └── wrap-root-element.js └── pages │ └── index.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | # Project dependencies 2 | .cache 3 | node_modules 4 | yarn-error.log 5 | 6 | # Build directory 7 | /public 8 | .DS_Store 9 | .netlify -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "singleQuote": true, 3 | "trailingComma": "es5" 4 | } 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 gatsbyjs 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Gatsby 4 | 5 |

6 |

7 | Gatsby with Apollo 8 |

9 | 10 | This is a minimal example of using Gatsby and Apollo together. It’s GraphQL all the way down! 🐢 11 | -------------------------------------------------------------------------------- /gatsby-browser.js: -------------------------------------------------------------------------------- 1 | export { wrapRootElement } from './src/apollo/wrap-root-element'; 2 | -------------------------------------------------------------------------------- /gatsby-config.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | siteMetadata: { 3 | title: 'Gatsby With Apollo', 4 | }, 5 | plugins: [ 6 | { 7 | resolve: 'gatsby-source-graphql', 8 | options: { 9 | typeName: 'RMAPI', 10 | fieldName: 'rickAndMorty', 11 | url: 'https://rickandmortyapi.com/graphql', 12 | }, 13 | }, 14 | ], 15 | }; 16 | -------------------------------------------------------------------------------- /gatsby-ssr.js: -------------------------------------------------------------------------------- 1 | export { wrapRootElement } from './src/apollo/wrap-root-element'; 2 | -------------------------------------------------------------------------------- /netlify.toml: -------------------------------------------------------------------------------- 1 | # example netlify.toml 2 | [build] 3 | command = "yarn build" 4 | functions = "functions" 5 | publish = "public" 6 | 7 | ## Uncomment to use this redirect for Single Page Applications like create-react-app. 8 | ## Not needed for static site generators. 9 | #[[redirects]] 10 | # from = "/*" 11 | # to = "/index.html" 12 | # status = 200 13 | 14 | ## (optional) Settings for Netlify Dev 15 | ## https://github.com/netlify/netlify-dev-plugin#project-detection 16 | #[dev] 17 | # command = "yarn start" # Command to start your dev server 18 | # port = 3000 # Port that the dev server will be listening on 19 | # publish = "dist" # Folder with the static content for _redirect file 20 | 21 | ## more info on configuring this file: https://www.netlify.com/docs/netlify-toml-reference/ 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gatsby-with-apollo", 3 | "description": "Gatsby with Apollo Boost", 4 | "version": "1.0.0", 5 | "author": "Jason Lengstorf ", 6 | "dependencies": { 7 | "@apollo/client": "^3.0.2", 8 | "cross-fetch": "^3.0.5", 9 | "gatsby": "^2.24.10", 10 | "gatsby-source-graphql": "^2.0.6", 11 | "react": "^16.6.0", 12 | "react-dom": "^16.6.0" 13 | }, 14 | "keywords": [ 15 | "gatsby", 16 | "apollo", 17 | "apollo-boost" 18 | ], 19 | "license": "MIT", 20 | "scripts": { 21 | "build": "gatsby build", 22 | "develop": "gatsby develop", 23 | "start": "npm run develop", 24 | "format": "prettier --write \"src/**/*.js\"", 25 | "test": "echo \"Error: no test specified\" && exit 1" 26 | }, 27 | "devDependencies": { 28 | "prettier": "^1.14.2" 29 | }, 30 | "repository": { 31 | "type": "git", 32 | "url": "https://github.com/jlengstorf/gatsby-with-apollo" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/apollo/client.js: -------------------------------------------------------------------------------- 1 | import fetch from 'cross-fetch'; 2 | import { ApolloClient, InMemoryCache, HttpLink } from '@apollo/client'; 3 | 4 | export const client = new ApolloClient({ 5 | link: new HttpLink({ 6 | uri: 'https://api-euwest.graphcms.com/v1/cjke2kn7p00ol01d2pinkptdj/master', 7 | fetch, 8 | }), 9 | cache: new InMemoryCache() 10 | }); 11 | -------------------------------------------------------------------------------- /src/apollo/wrap-root-element.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { ApolloProvider } from '@apollo/client'; 3 | import { client } from './client'; 4 | 5 | export const wrapRootElement = ({ element }) => ( 6 | {element} 7 | ); 8 | -------------------------------------------------------------------------------- /src/pages/index.js: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { graphql } from 'gatsby'; 3 | import { useQuery } from '@apollo/client'; 4 | import gql from 'graphql-tag'; 5 | 6 | // This query is executed at build time by Gatsby. 7 | export const GatsbyQuery = graphql` 8 | { 9 | rickAndMorty { 10 | character(id: 1) { 11 | name 12 | image 13 | } 14 | } 15 | } 16 | `; 17 | 18 | // This query is executed at run time by Apollo. 19 | const APOLLO_QUERY = gql` 20 | { 21 | meme(where: { id: "cjke2xlf9nhd90953khilyzja" }) { 22 | photo { 23 | url( 24 | transformation: { 25 | image: { resize: { width: 600, height: 600, fit: crop } } 26 | } 27 | ) 28 | } 29 | } 30 | } 31 | `; 32 | 33 | export default ({ 34 | data: { 35 | rickAndMorty: { character }, 36 | }, 37 | }) => { 38 | const { loading, error, data } = useQuery(APOLLO_QUERY); 39 | 40 | return ( 41 |
42 |

{character.name} With His Friend Sara

43 |

44 | Rick & Morty API data loads at build time. Sara Vieira’s meme API loads 45 | at runtime. 46 |

47 |
48 | {character.name} 53 | 54 | {loading &&

Loading Sara...

} 55 | {error &&

Error: ${error.message}

} 56 | {data && data.meme && data.meme.photo && ( 57 | Sara Vieira 62 | )} 63 |
64 |
65 | ); 66 | }; 67 | --------------------------------------------------------------------------------