Git Product home page Git Product logo

gatsby-plugin-apollo's Introduction

gatsby-plugin-apollo

This plugin sets up your Gatsby project to use Apollo Client. It wraps your app in an ApolloProvider so that you can use GraphQL queries and mutations in your components.

import {useQuery} from '@apollo/client';

function Cupcake() {
  const {data, loading, error} = useQuery(GET_CUPCAKE); // <-- just works
}

To learn all about GraphQL and Apollo, check out the Lift-off course on Odyssey, Apollo's learning platform.

Installation

To use this plugin, you'll also need to install your own copy of @apollo/client. This package exports all of the tools you'll need to make GraphQL queries in your app.

npm install gatsby-plugin-apollo @apollo/client graphql

Once installed, add the plugin to your Gatsby config.

// gatsby-config.js
module.exports = {
  plugins: ['gatsby-plugin-apollo']
};

Configuration

This plugin supports two methods of configuration:

Plugin options

For simple configurations, you can supply many of the ApolloClient constructor options directly to the plugin in your Gatsby config.

// gatsby-config.js
module.exports = {
  plugins: [
    {
      resolve: 'gatsby-plugin-apollo',
      options: {
        uri: 'https://example.com/graphql'
      }
    }
  ]
};

Due to a limitation on the types of options that can be passed to a Gatsby plugin, only certain constructor options can be passed as plugin options. Options that require an instance of a JavaScript class, like cache or link can't be serialized as JSON, so they can't be configured in this way.

For more complex configurations, the client shadowing method allows you to define your own custom Apollo Client instance to be used by the plugin.

Client shadowing

You can configure this plugin to use your own custom Apollo Client instance by creating a client.js file in src/gatsby-plugin-apollo that exports your client. You can create your client however you like, as long as you remember these important details:

  1. You must provide an isomorphic fetch implementation (such as isomorphic-fetch) as an option to HttpLink
  2. Your client instance must be the default export
// src/gatsby-plugin-apollo/client.js
import fetch from 'isomorphic-fetch';
import {ApolloClient, HttpLink, InMemoryCache} from '@apollo/client';

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: new HttpLink({
    uri: 'https://api.spacex.land/graphql/',
    fetch
  })
});

export default client;

This configuration method should be used if you need to customize your cache or use a complex link, such as sending a JWT along with every request or enabling subscriptions using a WebSocketLink.

License

MIT

gatsby-plugin-apollo's People

Contributors

dependabot[bot] avatar mckelveygreg avatar trevorblades avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

gatsby-plugin-apollo's Issues

Unable to Install

I am unable to get the plugin to install. I have created a new Gatsby site using the CLI which gives me the following package.json file:

{ "name": "apollo-test", "version": "1.0.0", "private": true, "description": "apollo-test", "author": "@chrisdavies71", "keywords": [ "gatsby" ], "scripts": { "develop": "gatsby develop", "start": "gatsby develop", "build": "gatsby build", "serve": "gatsby serve", "clean": "gatsby clean" }, "dependencies": { "gatsby": "^4.12.1", "gatsby-plugin-sass": "^5.12.1", "react": "^17.0.1", "react-dom": "^17.0.1", "sass": "^1.51.0" } }

When I try to add Apollo using the following command:

npm install gatsby-plugin-apollo @apollo/client graphql

I get the following error in my console:

`chrisdavies@Chriss-MacBook-Pro apollo-test % npm install gatsby-plugin-apollo @apollo/client graphql
npm ERR! code ERESOLVE
npm ERR! ERESOLVE could not resolve
npm ERR!
npm ERR! While resolving: [email protected]
npm ERR! Found: [email protected]
npm ERR! node_modules/graphql-ws
npm ERR! graphql-ws@"^4.4.1" from @graphql-tools/[email protected]
npm ERR! node_modules/@graphql-tools/url-loader
npm ERR! @graphql-tools/url-loader@"^6.0.0" from [email protected]
npm ERR! node_modules/graphql-config
npm ERR! graphql-config@"^3.0.2" from [email protected]
npm ERR! node_modules/eslint-plugin-graphql
npm ERR! eslint-plugin-graphql@"^4.0.0" from [email protected]
npm ERR! node_modules/gatsby
npm ERR!
npm ERR! Could not resolve dependency:
npm ERR! @apollo/client@"" from the root project
npm ERR!
npm ERR! Conflicting peer dependency: [email protected]
npm ERR! node_modules/graphql-ws
npm ERR! peerOptional graphql-ws@"^5.5.5" from @apollo/[email protected]
npm ERR! node_modules/@apollo/client
npm ERR! @apollo/client@"
" from the root project
npm ERR!
npm ERR! Fix the upstream dependency conflict, or retry
npm ERR! this command with --force, or --legacy-peer-deps
npm ERR! to accept an incorrect (and potentially broken) dependency resolution.
npm ERR!
npm ERR! See /Users/chrisdavies/.npm/eresolve-report.txt for a full report.

npm ERR! A complete log of this run can be found in:
npm ERR! /Users/chrisdavies/.npm/_logs/2022-04-26T05_46_18_213Z-debug-0.log
chrisdavies@Chriss-MacBook-Pro apollo-test %`

Node version is: 16.11.1 npm version is: 8.3.2

Any help much appreciated.

Chris

[Question] Best practices with Gatsby Cloud (Unsafe method warning when building)

Hi @trevorblades, while I don't use this plugin directly, my configuration is more or less identical to it.

Recently in Gatsby v3, we started seeing "Unsafe built-in method was used" warnings as documented in the discussion thread I created here:
gatsbyjs/gatsby#31753

TL;DR: It seems that any use of node-fetch throws these warnings that could potentially have impact on users who are on Gatsby Cloud (e.g. causes a full build vs incremental build).

All documentation from both Gatsby and Apollo to date has been pushing towards this configuration:

  1. using an isomorphic fetch library
  2. wrapping the Apollo Provider in both gatsby-browser.js AND gatsby-ssr.js

This seems to be problematic for Gatsby now. I haven't heard back from the Gatsby team on how to proceed, but I've also created a "Apollo Provider on Client-Side Only" test on my minimal repro here:
ardiewen/gatsby-with-apollo-warning#1

I wanted to get your thoughts on this. The PR on my minimal repro no longer produces the "Unsafe built-in method" warning on build, but it DOES force components with hooks such as useQuery to be tested against client-side only logic. Otherwise, it will throw an SSR invariant violation error.

A sample gasby project for this plugin

Hello

Could you provide a sample project/page about using your plugin?

The README gives some info but I'd like to have a more detailed example

Thanks
Matteo

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.