Skip to content

Instantly share code, notes, and snippets.

@svx
Forked from jsjaspreet/generateSchema.js
Created March 31, 2021 09:07
Show Gist options
  • Save svx/ced46d8a576ecdd657524014c60e5430 to your computer and use it in GitHub Desktop.
Save svx/ced46d8a576ecdd657524014c60e5430 to your computer and use it in GitHub Desktop.
Generate GraphQL Schema and Docs programmatically (adopted from Relay github)
import fs from 'fs';
import path from 'path';
import { graphql } from 'graphql';
import { introspectionQuery } from 'graphql/utilities';
import schema from './src/schema/index';
(async () => {
const result = await graphql(schema, introspectionQuery);
if (result.errors) {
console.error(
'ERROR introspecting schema: ',
JSON.stringify(result.errors, null, 2)
);
} else {
fs.writeFileSync(
path.join(__dirname, './schema.json'),
JSON.stringify(result, null, 2)
);
}
})();
{
"scripts": {
"generate-schema": "babel-node ./generateSchema.js",
"generate-schema-doc": "npm run clean && npm run generate-schema && graphdoc -s ./schema.json -o ./doc"
},
"dependencies": {
"graphql": "~0.11.7"
},
"devDependencies": {
"@2fd/graphdoc": "^2.4.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment