Nexus: Is it possible to have a script to generate the schema?

Created on 28 May 2019  路  2Comments  路  Source: graphql-nexus/nexus

It would be awesome if there was a script to generate the schema instead of being strictly limited to generating as a side effect of running in development.

It could be hooked into CI or pre-commit scripts to ensure that the schema never falls out of sync with the code.

Most helpful comment

It's really easy to set this up yourself, in fact we do exactly this, even in development mode the generation of the schema is separated from our server restart.

First we collect all our types:

// schema.js
import * as QueryTypes from "./Query";
// more type imports...

export const SchemaTypes = [
  QueryTypes
];

Second, we define a generateSchema script:

// generateSchema.js
import { makeSchema } from "nexus";
import { SchemaTypes } from "./schema";

makeSchema({
  types: SchemaTypes,
  outputs: {
    // outputs configs...
  },
  // anything else you need to successfully make the schema.
});

Third is our server.js:

// server.js 
const server = new ApolloServer({
  schema: makeSchema({
    tyupes: SchemaTypes,
    outputs: false // it will already be generated so let's not waste resources on this.
  })
});

Now everything is divided into re-usable parts, we can either use them stand-alone (for example generate the schema alone on a commit hook or during CI) or we can use them all combined. The following are a couple of example scripts: (package.json)

{ 
  "scripts": {
    "generate-schema": "node ./src/generateSchema.js",
    "start-dev": "node ./src/server.js",
    "generate-and-start": "yarn generate-schema && yarn start-dev",
    "dev": "nodemon yarn generate-and-start"
  }
}

All 2 comments

It's really easy to set this up yourself, in fact we do exactly this, even in development mode the generation of the schema is separated from our server restart.

First we collect all our types:

// schema.js
import * as QueryTypes from "./Query";
// more type imports...

export const SchemaTypes = [
  QueryTypes
];

Second, we define a generateSchema script:

// generateSchema.js
import { makeSchema } from "nexus";
import { SchemaTypes } from "./schema";

makeSchema({
  types: SchemaTypes,
  outputs: {
    // outputs configs...
  },
  // anything else you need to successfully make the schema.
});

Third is our server.js:

// server.js 
const server = new ApolloServer({
  schema: makeSchema({
    tyupes: SchemaTypes,
    outputs: false // it will already be generated so let's not waste resources on this.
  })
});

Now everything is divided into re-usable parts, we can either use them stand-alone (for example generate the schema alone on a commit hook or during CI) or we can use them all combined. The following are a couple of example scripts: (package.json)

{ 
  "scripts": {
    "generate-schema": "node ./src/generateSchema.js",
    "start-dev": "node ./src/server.js",
    "generate-and-start": "yarn generate-schema && yarn start-dev",
    "dev": "nodemon yarn generate-and-start"
  }
}

This is very clever! Thank you :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

StevenLangbroek picture StevenLangbroek  路  6Comments

MathiasKandelborg picture MathiasKandelborg  路  5Comments

tonyfromundefined picture tonyfromundefined  路  3Comments

santialbo picture santialbo  路  5Comments

israelglar picture israelglar  路  3Comments