From 99f002baf627f24262e151674aeafa29e0b387d4 Mon Sep 17 00:00:00 2001 From: Eden Tyler-Moss Date: Tue, 21 Jan 2020 14:03:10 +0000 Subject: [PATCH] feat(gatsby-source-graphql) Execute functions in header. (#20731) * feat(gatsby-source-graphql) Execute functions in headers. Resolves issue #20690, allowing the use of async functions to define headers for gatsby-source-graphql. To use, functions can be passed in the headers object, i.e: headers { authorization: asyncFunc }. * Changes whole headers parameter to accept async function. See https://github.com/gatsbyjs/gatsby/pull/20731. Updated README.md to include new (optional) syntax. --- packages/gatsby-source-graphql/README.md | 6 ++++++ packages/gatsby-source-graphql/src/gatsby-node.js | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-source-graphql/README.md b/packages/gatsby-source-graphql/README.md index 941597a1ec88d..0993f30e01cd7 100644 --- a/packages/gatsby-source-graphql/README.md +++ b/packages/gatsby-source-graphql/README.md @@ -42,6 +42,12 @@ module.exports = { // Learn about environment variables: https://gatsby.dev/env-vars Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, }, + // HTTP headers alternatively accepts a function (allows async) + headers: async () => { + return { + Authorization: await getAuthorizationToken(), + } + }, // Additional options to pass to node-fetch fetchOptions: {}, }, diff --git a/packages/gatsby-source-graphql/src/gatsby-node.js b/packages/gatsby-source-graphql/src/gatsby-node.js index b1988b7dec867..a02d79a8f42dc 100644 --- a/packages/gatsby-source-graphql/src/gatsby-node.js +++ b/packages/gatsby-source-graphql/src/gatsby-node.js @@ -51,8 +51,8 @@ exports.sourceNodes = async ( link = createHttpLink({ uri: url, fetch, - headers, fetchOptions, + headers: typeof headers === `function` ? await headers() : headers, }) }