Skip to content

Instantly share code, notes, and snippets.

@CosAnca
Forked from monachilada/gatsby-config.js
Created September 26, 2019 09:51
Show Gist options
  • Select an option

  • Save CosAnca/f776edd4d4af16f66e259d504b785b74 to your computer and use it in GitHub Desktop.

Select an option

Save CosAnca/f776edd4d4af16f66e259d504b785b74 to your computer and use it in GitHub Desktop.

Revisions

  1. @monachilada monachilada created this gist Sep 24, 2019.
    40 changes: 40 additions & 0 deletions gatsby-config.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    const { createHttpLink } = require('apollo-link-http');
    const fetch = require('node-fetch');
    const store = require('store');
    const sourceNodes = require('gatsby/dist/utils/source-nodes');
    require('dotenv').config();

    const craftGqlUrl = process.env.CRAFT_GQL_URL;
    const craftGqlToken = process.env.CRAFT_GQL_TOKEN;

    module.exports = {
    plugins: [
    {
    resolve: 'gatsby-source-graphql',
    options: {
    typeName: 'Craft',
    fieldName: 'craft',
    createLink: () =>
    createHttpLink({
    uri: `${craftGqlUrl}`,
    headers: {
    Authorization: `Bearer ${craftGqlToken}`,
    },
    fetch: (uri, options) => {
    const token = store.get('X-Craft-Token');
    return fetch(`${uri}?token=${token}`, options);
    },
    }),
    },
    },
    ],
    developMiddleware: app => {
    app.use('*', (req, res, next) => {
    if (req.query.token) {
    store.set('X-Craft-Token', req.query.token);
    sourceNodes();
    }
    next();
    });
    },
    };