Skip to content

Commit

Permalink
feat: Add custom query executor to pass in Authorization header (#78)
Browse files Browse the repository at this point in the history
* chore: Add node-fetch dependency

* feat: Add custom query executor to pass in Authorization header

* refactor: Use dotenv
  • Loading branch information
Jonathan Steele authored Jul 10, 2020
1 parent c88cee3 commit 33cdb8d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
6 changes: 4 additions & 2 deletions demo/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
require('dotenv').config()

module.exports = {
plugins: [
{
resolve: 'gatsby-source-graphcms',
options: {
downloadLocalImages: true,
endpoint:
'https://api-eu-central-1.graphcms.com/v2/ck8sn5tnf01gc01z89dbc7s0o/master',
endpoint: process.env.GRAPHCMS_ENDPOINT,
token: process.env.GRAPHCMS_TOKEN,
},
},
],
Expand Down
16 changes: 13 additions & 3 deletions gatsby-source-graphcms/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const {
createDefaultQueryExecutor,
wrapQueryExecutorWithQueue,
loadSchema,
generateDefaultFragments,
compileNodeQueries,
Expand All @@ -8,10 +8,20 @@ const {
sourceAllNodes,
} = require('gatsby-graphql-source-toolkit')
const { createRemoteFileNode } = require('gatsby-source-filesystem')
const fetch = require('node-fetch')
const pluralize = require('pluralize')

const createSourcingConfig = async (gatsbyApi, { endpoint, token }) => {
const execute = createDefaultQueryExecutor(endpoint)
const execute = async ({ operationName, query, variables = {} }) => {
return await fetch(endpoint, {
method: 'POST',
body: JSON.stringify({ query, variables, operationName }),
headers: {
'Content-Type': 'application/json',
...(token && { Authorization: `Bearer ${token}` }),
},
}).then((res) => res.json())
}
const schema = await loadSchema(execute)

const nodeInterface = schema.getType('Node')
Expand All @@ -36,7 +46,7 @@ const createSourcingConfig = async (gatsbyApi, { endpoint, token }) => {
return {
gatsbyApi,
schema,
execute,
execute: wrapQueryExecutorWithQueue(execute, { concurrency: 10 }),
gatsbyTypePrefix: `GraphCMS_`,
gatsbyNodeDefs: buildNodeDefinitions({ gatsbyNodeTypes, documents }),
}
Expand Down
1 change: 1 addition & 0 deletions gatsby-source-graphcms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"dependencies": {
"gatsby-graphql-source-toolkit": "0.2.2",
"gatsby-source-filesystem": "2.3.18",
"node-fetch": "2.6.0",
"pluralize": "8.0.0"
}
}

0 comments on commit 33cdb8d

Please sign in to comment.