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: Save generated query fragments #108

Merged
merged 3 commits into from
Aug 21, 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
18 changes: 18 additions & 0 deletions demo/graphcms-fragments/Asset.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
fragment Asset on Asset {
stage
locale
remoteId: id
handle
fileName
height
width
size
mimeType
productImages {
... on Product {
remoteTypeName: __typename
remoteId: id
}
}
url
}
30 changes: 30 additions & 0 deletions demo/graphcms-fragments/Product.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
fragment Product on Product {
stage
remoteId: id
createdAt
updatedAt
publishedAt
name
slug
description {
... on RichText {
raw
html
markdown
text
}
}
price
images {
... on Asset {
remoteTypeName: __typename
remoteId: id
}
}
reviews {
... on Review {
remoteTypeName: __typename
remoteId: id
}
}
}
15 changes: 15 additions & 0 deletions demo/graphcms-fragments/Review.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
fragment Review on Review {
stage
remoteId: id
createdAt
updatedAt
publishedAt
email
name
product {
... on Product {
remoteTypeName: __typename
remoteId: id
}
}
}
4 changes: 4 additions & 0 deletions gatsby-source-graphcms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ module.exports = {

- Build markdown nodes for all [`RichText`](https://graphcms.com/docs/reference/fields/rich-text) fields in your GraphCMS schema. [Learn more](#using-markdown-nodes).

- `fragmentsPath` _String_ (default value: `graphcms-fragments`)

- The local project path where generated query fragments are saved. This is relative to your current working directory.

## Downloading local image assets

This source plugin provides the option to download and cache GraphCMS assets in your Gatsby project. This enables you to use [`gatsby-image`](https://www.gatsbyjs.org/packages/gatsby-image), for image loading optimizations, with your GraphCMS image assets.
Expand Down
17 changes: 14 additions & 3 deletions gatsby-source-graphcms/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const crypto = require('crypto')
const fs = require('fs')
const {
wrapQueryExecutorWithQueue,
loadSchema,
generateDefaultFragments,
readOrGenerateDefaultFragments,
compileNodeQueries,
buildNodeDefinitions,
createSchemaCustomization,
Expand All @@ -20,7 +21,10 @@ exports.onPreBootstrap = ({ reporter }, pluginOptions) => {
)
}

const createSourcingConfig = async (gatsbyApi, { endpoint, token }) => {
const createSourcingConfig = async (
gatsbyApi,
{ endpoint, fragmentsPath = 'graphcms-fragments', token }
) => {
const execute = async ({ operationName, query, variables = {} }) => {
return await fetch(endpoint, {
method: 'POST',
Expand Down Expand Up @@ -61,7 +65,14 @@ const createSourcingConfig = async (gatsbyApi, { endpoint, token }) => {
nodeQueryVariables: ({ id }) => ({ where: { id } }),
}))

const fragments = generateDefaultFragments({ schema, gatsbyNodeTypes })
const fragmentsDir = `${process.cwd()}/${fragmentsPath}`

if (!fs.existsSync(fragmentsDir)) fs.mkdirSync(fragmentsDir)

const fragments = await readOrGenerateDefaultFragments(fragmentsDir, {
schema,
gatsbyNodeTypes,
})

const documents = compileNodeQueries({
schema,
Expand Down