Skip to content

Commit

Permalink
feat: Delta sourcing (#79)
Browse files Browse the repository at this point in the history
* chore: Add ENABLE_GATSBY_REFRESH_ENDPOINT flag for rebuilds

* feat: Add query for individual node fetching

* fix: Correct individual node query

* feat: Handle webhooks for node deletion

* feat: Handle webhooks for node creation

* fix: Use correct input type, variable for NODE_ query

* feat: Handle webhooks for node updates

* feat: Handle webhooks for node publish, unpublish
  • Loading branch information
Jonathan Steele authored and ynnoj committed Jul 23, 2020
1 parent 994dbc4 commit bb306cc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"build": "gatsby build",
"clean": "gatsby clean",
"dev": "gatsby develop"
"dev": "ENABLE_GATSBY_REFRESH_ENDPOINT=true gatsby develop"
},
"dependencies": {
"gatsby": "2.23.22",
Expand Down
42 changes: 39 additions & 3 deletions gatsby-source-graphcms/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
buildNodeDefinitions,
createSchemaCustomization,
sourceAllNodes,
sourceNodeChanges,
} = require('gatsby-graphql-source-toolkit')
const { createRemoteFileNode } = require('gatsby-source-filesystem')
const fetch = require('node-fetch')
Expand All @@ -30,9 +31,14 @@ const createSourcingConfig = async (gatsbyApi, { endpoint, token }) => {
const gatsbyNodeTypes = possibleTypes.map((type) => ({
remoteTypeName: type.name,
remoteIdFields: ['__typename', 'id'],
queries: `query LIST_${pluralize(type.name).toUpperCase()} { ${pluralize(
queries: `
query LIST_${pluralize(type.name).toUpperCase()} { ${pluralize(
type.name.toLowerCase()
)}(first: $limit, skip: $offset) }`,
)}(first: $limit, skip: $offset) }
query NODE_${type.name.toUpperCase()}($where: ${
type.name
}WhereUniqueInput!) { ${type.name.toLowerCase()}(where: $where) }`,
nodeQueryVariables: ({ id }) => ({ where: { id } }),
}))

const fragments = generateDefaultFragments({ schema, gatsbyNodeTypes })
Expand All @@ -53,11 +59,41 @@ const createSourcingConfig = async (gatsbyApi, { endpoint, token }) => {
}

exports.sourceNodes = async (gatsbyApi, pluginOptions) => {
const { webhookBody } = gatsbyApi

const config = await createSourcingConfig(gatsbyApi, pluginOptions)

await createSchemaCustomization(config)

await sourceAllNodes(config)
if (webhookBody && Object.keys(webhookBody).length) {
const { operation, data } = webhookBody

const nodeEvent = (operation, { __typename, id }) => {
switch (operation) {
case 'delete':
case 'unpublish':
return {
eventName: 'DELETE',
remoteTypeName: __typename,
remoteId: { __typename, id },
}
case 'create':
case 'publish':
case 'update':
return {
eventName: 'UPDATE',
remoteTypeName: __typename,
remoteId: { __typename, id },
}
}
}

await sourceNodeChanges(config, {
nodeEvents: [nodeEvent(operation, data)],
})
} else {
await sourceAllNodes(config)
}
}

exports.onCreateNode = async (
Expand Down

0 comments on commit bb306cc

Please sign in to comment.