Skip to content

Commit

Permalink
feat(gatsby-source-graphql) Execute functions in header. (#20731)
Browse files Browse the repository at this point in the history
* 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 #20731. Updated README.md to
include new (optional) syntax.
  • Loading branch information
etylermoss authored and GatsbyJS Bot committed Jan 21, 2020
1 parent 4c10a58 commit 99f002b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/gatsby-source-graphql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
},
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-source-graphql/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ exports.sourceNodes = async (
link = createHttpLink({
uri: url,
fetch,
headers,
fetchOptions,
headers: typeof headers === `function` ? await headers() : headers,
})
}

Expand Down

0 comments on commit 99f002b

Please sign in to comment.