Summary:
-
Configure Husky work with lint-staged and share your githooks in your repository
-
Set up lint-staged to watch for changes to schema.graphql file
-
use the slash-graphql-cli to push your schema to Slash with pre-push verification to prevent any bad schema pushes.
Detailed configuration:
- Husky configuration in
package.json
…
"husky": {
"hooks": {
"pre-commit": "node ./.githooks/preCommit.js",
"pre-push": "prepush-if-changed"
}
},
"prepush-if-changed": {
"src/schema.graphql": "node ./.githooks/updateSlash.js"
},
- Pre-commit hook
/.githooks/preCommit.js
const lintStaged = require('lint-staged')
const runHooks = async () => {
try {
const success = await lintStaged({
allowEmpty: false,
concurrent: true,
config: {
'schema.graphql': [
// include whatever command line functions you want to run here, in the order you want them to run, in this array
// This example makes 2 files and then adds them to the git commit
'node ./src/contexts/Fragments/makePrettySchema.js',
'node ./src/contexts/Fragments/generateFragments.js',
'git add ./src/schemaPretty.md ./src/contexts/Fragments/codeGenFragments.js',
]
},
cwd: process.cwd(),
debug: false,
maxArgLength: null,
quiet: false,
relative: false,
shell: false,
stash: true,
verbose: false
})
console.log(success ? 'Linting was successful!' : 'Linting failed!')
if (!success) return process.exit(22)
} catch (e) {
// Failed to load configuration
console.error(e)
throw 2
}
}
runHooks()
- Push schema changes to Slash.
./.githooks/updateSlash.js
:
const { exec } = require("child_process");
exec("slash-graphql update-schema -e https://YOUR_ENDPOINT HERE.dgraph.io/graphql -t YOUR_API_KEY_HERE ./src/schema.graphql", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
// return;
throw 1
}
if (stderr) {
console.log(`stderr: ${stderr}`);
// return;
throw 1
}
console.log(`stdout: ${stdout}`);
})
That’s it! Now you have version controlled schema and can rest easier.
P.s. the generateFragments.js
script that is part of our pre-commit hook is explained here.