-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-plugin-schema-snapshot): First version (#16561)
- Loading branch information
1 parent
442ae0c
commit 9dd1070
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}, | ||
], | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// noop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters