Skip to content

Instantly share code, notes, and snippets.

Created February 10, 2017 10:14
What would you like to do?
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...');
configOptions: { cwd: path.resolve('../') },
targets: [{
type: 'schemaJSON',
}, {
type: 'schemaFlow',
callback(err, result) {
const [ schemaJSON, schemaFlow ] = result;
const fileContent = [
'/* eslint-disable */',
`export const json = ${schemaJSON};`,
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) ');
"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