Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Build Gatsby nodes #76

Merged
merged 7 commits into from
Jul 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions demo/gatsby-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
plugins: [
{
resolve: 'gatsby-source-graphcms',
options: {
endpoint:
'https://api-eu-central-1.graphcms.com/v2/ck8sn5tnf01gc01z89dbc7s0o/master',
},
},
],
}
2 changes: 1 addition & 1 deletion demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"dependencies": {
"gatsby": "2.23.22",
"gatsby-source-graphcms": "*",
"gatsby-source-graphcms": "2.0.0",
"react": "16.13.1",
"react-dom": "16.13.1"
}
Expand Down
50 changes: 50 additions & 0 deletions gatsby-source-graphcms/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const {
createDefaultQueryExecutor,
loadSchema,
generateDefaultFragments,
compileNodeQueries,
buildNodeDefinitions,
createSchemaCustomization,
sourceAllNodes,
} = require('gatsby-graphql-source-toolkit')
const pluralize = require('pluralize')

const createSourcingConfig = async (gatsbyApi, { endpoint, token }) => {
const execute = createDefaultQueryExecutor(endpoint)
const schema = await loadSchema(execute)

const nodeInterface = schema.getType('Node')
const possibleTypes = schema.getPossibleTypes(nodeInterface)

const gatsbyNodeTypes = possibleTypes.map((type) => ({
remoteTypeName: type.name,
remoteIdFields: ['__typename', 'id'],
queries: `query LIST_${pluralize(type.name).toUpperCase()} { ${pluralize(
type.name.toLowerCase()
)}(first: $limit, skip: $offset) }`,
}))

const fragments = generateDefaultFragments({ schema, gatsbyNodeTypes })

const documents = compileNodeQueries({
schema,
gatsbyNodeTypes,
customFragments: fragments,
})

return {
gatsbyApi,
schema,
execute,
gatsbyTypePrefix: `GraphCMS_`,
gatsbyNodeDefs: buildNodeDefinitions({ gatsbyNodeTypes, documents }),
}
}

exports.sourceNodes = async (gatsbyApi, pluginOptions) => {
const config = await createSourcingConfig(gatsbyApi, pluginOptions)

await createSchemaCustomization(config)

await sourceAllNodes(config)
}
5 changes: 3 additions & 2 deletions gatsby-source-graphcms/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-source-graphcms",
"version": "1.0.0-alpha.3",
"version": "2.0.0",
"description": "Gatsby source plugin for building websites using the GraphCMS as a data source.",
"main": "index.js",
"keywords": [
Expand All @@ -13,6 +13,7 @@
],
"license": "MIT",
"dependencies": {
"gatsby-graphql-source-toolkit": "0.2.0"
"gatsby-graphql-source-toolkit": "0.2.2",
"pluralize": "8.0.0"
}
}
25 changes: 15 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1283,9 +1283,9 @@
form-data "^3.0.0"

"@types/node@*":
version "14.0.18"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.18.tgz#5111b2285659442f9f95697386a2b42b875bd7e9"
integrity sha512-0Z3nS5acM0cIV4JPzrj9g/GH0Et5vmADWtip3YOXOp1NpOLU8V3KoZDc8ny9c1pe/YSYYzQkAWob6dyV/EWg4g==
version "14.0.19"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.0.19.tgz#994d99708822bca643a2364f8aeed04a16e0f5a1"
integrity sha512-yf3BP/NIXF37BjrK5klu//asUWitOEoUP5xE1mhSUjazotwJ/eJDgEmMQNlOeWOVv72j24QQ+3bqXHE++CFGag==

"@types/node@^8.5.7":
version "8.10.61"
Expand Down Expand Up @@ -3882,9 +3882,9 @@ [email protected]:
integrity sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=

electron-to-chromium@^1.3.488:
version "1.3.489"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.489.tgz#6e87ed17744c85629ca6f567e5bc7ebe67652f57"
integrity sha512-1FfqjT+IXbs/H9bGJwRIyQQqOXIo7mOVUnv8s7nb2ff01fD0Z8dn1YjKovLDCFe4900Dal2uutR0YzXN5ldqEQ==
version "1.3.490"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.490.tgz#12aa776c493e66ba21536512fc317bdda6d04cd4"
integrity sha512-jKJF1mKXrQkT0ZiuJ/oV63Q02hAeWz0GGt/z6ryc518uCHtMyS9+wYAysZtBQ8rsjqFPAYXV4TIz5GQ8xyubPA==

elliptic@^6.0.0, elliptic@^6.5.2:
version "6.5.3"
Expand Down Expand Up @@ -4882,10 +4882,10 @@ gatsby-graphiql-explorer@^0.4.10:
dependencies:
"@babel/runtime" "^7.10.3"

[email protected].0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/gatsby-graphql-source-toolkit/-/gatsby-graphql-source-toolkit-0.2.0.tgz#a40c7685d579a862e67c2ed30c42ac4dd0ebd2c2"
integrity sha512-9S26sev19KfeoZB/vp7vUamonLz/w9HlEa15Q5+fXsmKtpNREzxJt+qsTvBbkDqTxl+3CCG0bD8sxnj0kbzIng==
[email protected].2:
version "0.2.2"
resolved "https://registry.yarnpkg.com/gatsby-graphql-source-toolkit/-/gatsby-graphql-source-toolkit-0.2.2.tgz#07c37eb23c330099c9e9d3d59b3981a999a1ab2f"
integrity sha512-gx98EGdzpq3LDd5YNTSdciZDExhOQRLW2kzrc9tLf61N2oV3pnQ1zIrQ5RJHNjKwLUndyfDRmK85bYqYLLcPuA==
dependencies:
"@types/node-fetch" "^2.5.7"
lodash "^4.17.15"
Expand Down Expand Up @@ -8282,6 +8282,11 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

[email protected]:
version "8.0.0"
resolved "https://registry.yarnpkg.com/pluralize/-/pluralize-8.0.0.tgz#1a6fa16a38d12a1901e0320fa017051c539ce3b1"
integrity sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==

pnp-webpack-plugin@^1.6.4:
version "1.6.4"
resolved "https://registry.yarnpkg.com/pnp-webpack-plugin/-/pnp-webpack-plugin-1.6.4.tgz#c9711ac4dc48a685dabafc86f8b6dd9f8df84149"
Expand Down