Skip to content

Commit

Permalink
feat(gatsby-plugin-schema-snapshot): First version (#16561)
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanprobst authored and sidharthachatterjee committed Aug 12, 2019
1 parent 442ae0c commit 9dd1070
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 0 deletions.
53 changes: 53 additions & 0 deletions packages/gatsby-plugin-schema-snapshot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# gatsby-plugin-schema-snapshot

Create a snapshot of the GraphQL schema.

Saves a minimal schema to file, adds the `@dontInfer` directive to all top-level types, and re-creates the schema from the saved type definitions during bootstrap. Use this plugin if you intend to lock-down a project's GraphQL schema.

## Options

All configuration options are optional.

```js
{
// Path where the type definitions will be saved to
path: `schema.gql`,
// include types by name, or all types owned by a plugin
include: {
types: [],
plugins: [],
},
// exclude types by name, or all types owned by a plugin
// by default, internal and built-in types are excluded
exclude: {
types: [],
plugins: [],
},
// ensure all field types are included
// dont't turn this off unless you have a very good reason to
withFieldTypes: true,
// manually control if a saved schema snapshot should be replaced with an
// updated version
update: false,
}
```

## Example

```js
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-schema-snapshot`,
options: {
path: `schema.gql`,
exclude: {
plugins: [`gatsby-source-npm-package-search`],
},
update: process.env.GATSBY_UPDATE_SCHEMA_SNAPSHOT,
},
},
],
}
```
35 changes: 35 additions & 0 deletions packages/gatsby-plugin-schema-snapshot/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const fs = require(`fs`)
const path = require(`path`)

exports.createSchemaCustomization = ({ actions, reporter }, options = {}) => {
const { createTypes, printTypeDefinitions } = actions

if (!printTypeDefinitions) {
reporter.error(
`\`gatsby-plugin-schema-snapshot\` needs Gatsby v2.13.55 or above.`
)
return
}

const filePath = path.resolve(options.path || `schema.gql`)

try {
if (fs.existsSync(filePath)) {
reporter.info(`Reading GraphQL type definitions from ${filePath}`)
const schema = fs.readFileSync(filePath, { encoding: `utf-8` })

createTypes(schema, { name: `default-site-plugin` })

if (options.update) {
fs.unlinkSync(filePath)
printTypeDefinitions(options)
}
} else {
printTypeDefinitions(options)
}
} catch (error) {
reporter.error(
`The plugin \`gatsby-plugin-schema-snapshot\` encountered an error`, error
)
}
}
1 change: 1 addition & 0 deletions packages/gatsby-plugin-schema-snapshot/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// noop
22 changes: 22 additions & 0 deletions packages/gatsby-plugin-schema-snapshot/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "gatsby-plugin-schema-snapshot",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"keywords": [
"gatsby",
"gatsby-plugin"
],
"files": [
"gatsby-node.js"
],
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby.git",
"directory": "packages/gatsby-plugin-schema-snapshot"
},
"homepage": "https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-schema-snapshot#readme",
"peerDependencies": {
"gatsby": "^2.13.55"
}
}
1 change: 1 addition & 0 deletions packages/gatsby/src/schema/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ const printTypeDefinitions = ({ config, schemaComposer }) => {

try {
typeDefs.forEach(tc => printedTypeDefs.push(printType(tc)))
report.info(`Writing GraphQL type definitions to ${path}`)
return fs.writeFile(path, printedTypeDefs.join(`\n\n`))
} catch (error) {
report.error(`Failed writing type definitions to \`${path}\`.`, error)
Expand Down

0 comments on commit 9dd1070

Please sign in to comment.