Skip to content

Instantly share code, notes, and snippets.

@kumarharsh
Created February 10, 2017 10:14
Show Gist options
  • Save kumarharsh/4a4ee5d8f9c9b8153adad2a7ab21cb71 to your computer and use it in GitHub Desktop.
Save kumarharsh/4a4ee5d8f9c9b8153adad2a7ab21cb71 to your computer and use it in GitHub Desktop.
Generate schema.js from .gql files
/* @flow */
import generate from '@playlyfe/gql/lib/tools/generate';
import path from 'path';
import fs from 'fs';
// This script reads in the schema files based off the configOptions.cwd folder
// and generates a schema.js (instead of schema.json) file and
// outputs it to the specified output folder
// Usually, you'd place this script just before running webpack,
// so that it picks up the compiled schema file.
console.log('\n[schema] generating...');
console.time('time');
generate({
configOptions: { cwd: path.resolve('../') },
targets: [{
type: 'schemaJSON',
}, {
type: 'schemaFlow',
}],
callback(err, result) {
const [ schemaJSON, schemaFlow ] = result;
const fileContent = [
'/* eslint-disable */',
schemaFlow,
`export const json = ${schemaJSON};`,
].join('\n\n');
fs.writeFileSync('src/schema.js', fileContent); // output the built schema to be used in build-step by webpack
process.stdout.write('[schema] (`src/schema.js` file generated) ');
console.timeEnd('time');
console.log('');
},
});
{
"name": "your-project",
"scripts": {
"build": "cross-env NODE_ENV=production yarn run build-dev",
"build-dev": "yarn run _pre-build && yarn run _webpack",
"build-watch": "yarn run _pre-build && yarn run _webpack -- --watch",
"gen-schema": "yarn run _babel-node -- ./scripts/graphql-relay/gen-schema.js",
"_pre-build": "yarn run gen-schema",
"_webpack": "yarn run _babel-node -- node_modules/webpack/bin/webpack.js --config webpack.config.js",
"_babel-node": "cross-env BABEL_CACHE_PATH=./.tmp/babel-node-cache.json babel-node"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment