makeSchema

Defines the GraphQL schema, by combining the GraphQL types defined by the GraphQL Nexus layer or any manually defined GraphQL named types (Scalars, ObjectTypes, Enums, etc).

We require at least one type be named "Query", which will be used as the root query type. The makeSchema takes several options which all should be specified by the user, detailed below:

types

The types property is required, and should contain all of the possible Nexus/GraphQL types that make up a schema (at least the root-level ones), imported into one place. The type is opaque, it can be an array or object, we recursively walk through the object looking for types, so the following will all have the same result, and non-GraphQL objects will be ignored.

  • { typeA, typeB, typeC, typeD }
  • [[typeA], [{ typeB: typeB }], typeC, { typeD }]
  • [typeA, typeB, typeC, typeD]
1import { makeSchema } from '@nexus/schema'
2import * as types from './allNexusTypes'
3
4export const schema = makeSchema({
5 types,
6})

plugins

The plugins property is an array for adding "Plugins", or ways of extending/changing the runtime behavior of Nexus and GraphQL. Unlike the types property, this must be an array, and the order of the plugins matters because this influences the order of any resolver "middleware" the plugin may optionally provide.

1import { makeSchema, nullabilityGuard, fieldAuthorizePlugin } from '@nexus/schema'
2import * as types from './allNexusTypes'
3
4export const schema = makeSchema({
5 types,
6 plugins: [
7 fieldAuthorizePlugin({
8 /* ... */
9 }),
10 nullabilityGuard({
11 /* ... */
12 }),
13 ],
14})

shouldGenerateArtifacts, outputs, typegenAutoConfig

The shouldGenerateArtifacts is a boolean value which determines whether artifact files (graphql and TypeScript) are emitted when the code for makeSchema

outputs is an object which specifies the absolute path for where the emitted files are generated. If you do not wish to generate one of these types

typegenAutoConfig is an object which gives nexus more information about how to properly generate the type definition file. An example of the sources is provided below:

1makeSchema({
2 types,
3 shouldGenerateArtifacts: process.env.NODE_ENV === 'development',
4 outputs: {
5 // I tend to use `.gen` to denote "auto-generated" files, but this is not a requirement.
6 schema: path.join(__dirname, 'generated/schema.gen.graphql'),
7 typegen: path.join(__dirname, 'generated/nexusTypes.gen.ts'),
8 },
9 typegenAutoConfig: {
10 headers: [
11 'import { ConnectionFieldOpts } from "@packages/api-graphql/src/extensions/connectionType"',
12 ],
13 sources: [
14 // Automatically finds any interface/type/class named similarly to the and infers it
15 // the "source" type of that resolver.
16 {
17 source: '@packages/types/src/db.ts',
18 alias: 'dbt',
19 typeMatch: name => new RegExp(`(?:interface|type|class)\\s+(${name}s?)\\W`, 'g'),
20 },
21 // We also need to import this source in order to provide it as the `contextType` below.
22 {
23 source: '@packages/data-context/src/DataContext.ts',
24 alias: 'ctx',
25 },
26 ],
27 // Typing from the source
28 contextType: 'ctx.DataContext',
29 backingTypeMap: {
30 Date: 'Date',
31 DateTime: 'Date',
32 UUID: 'string',
33 },
34 debug: false,
35 },
36})

The Ghost Example is the best place to look for an example of how we're able to capture the types from existing runtime objects or definitions and merge them with our schema.

shouldExitAfterGenerateArtifacts

If you are not checking in your artifacts and wish to run them, this will allow you to exit right after the artifacts have been generated. There is no default behavior for this, but you could do something like the following, to be able to run a script which will exit if --nexus-exit is provided:

1makeSchema({
2 // ... options like above
3 shouldExitAfterGenerateArtifacts: process.argv.includes('--nexus-exit'),
4})
1ts-node -T ./path/to/my/schema.ts --nexus-exit

prettierConfig

Either an absolute path to a .prettierrc file, or an object with a valid "prettier" config options.

1makeSchema({
2 // ... options like above
3 prettierConfig: path.join(__dirname, '../../../.prettierrc'),
4})

nonNullDefaults

Controls the nullability of the input / output types emitted by nexus. The current Nexus default is { output: true, input: false }, though the graphql-js / spec default is { output: false, input: false }.

You should make a decision on this and supply the option yourself, it may be changed / required in the future.

Read more on this in the getting-started guide.

typegenConfig, formatTypegen

Escape hatches for more advanced cases which need further control over. You typically won't need these.

customPrintSchemaFn

Optional, allows you to override the printSchema when outputting the generated .graphql file:

1makeSchema({
2 // ...
3 customPrintSchemaFn: schema => {
4 return printSchema(schema, { commentDescriptions: true })
5 },
6})

Footnotes: Annotated config option for typegenAutoConfig:

1export interface TypegenAutoConfigOptions {
2 /**
3 * Any headers to prefix on the generated type file
4 */
5 headers?: string[]
6 /**
7 * Array of files to match for a type
8 *
9 * sources: [
10 * { source: 'typescript', alias: 'ts' },
11 * { source: path.join(__dirname, '../backingTypes'), alias: 'b' },
12 * ]
13 */
14 sources: TypegenConfigSourceModule[]
15 /**
16 * Typing for the context, referencing a type defined in the aliased module
17 * provided in sources e.g. 'alias.Context'
18 */
19 contextType?: string
20 /**
21 * Types that should not be matched for a backing type,
22 *
23 * By default this is set to ['Query', 'Mutation', 'Subscription']
24 *
25 * skipTypes: ['Query', 'Mutation', /(.*?)Edge/, /(.*?)Connection/]
26 */
27 skipTypes?: (string | RegExp)[]
28 /**
29 * If debug is set to true, this will log out info about all types
30 * found, skipped, etc. for the type generation files.
31 */
32 debug?: boolean
33 /**
34 * If provided this will be used for the backing types rather than the auto-resolve
35 * mechanism above. Useful as an override for one-off cases, or for scalar
36 * backing types.
37 */
38 backingTypeMap?: Record<string, string>
39}
40
41export interface TypegenConfigSourceModule {
42 /**
43 * The module for where to look for the types.
44 * This uses the node resolution algorithm via require.resolve,
45 * so if this lives in node_modules, you can just provide the module name
46 * otherwise you should provide the absolute path to the file.
47 */
48 source: string
49 /**
50 * When we import the module, we use 'import * as ____' to prevent
51 * conflicts. This alias should be a name that doesn't conflict with any other
52 * types, usually a short lowercase name.
53 */
54 alias: string
55 /**
56 * Provides a custom approach to matching for the type
57 *
58 * If not provided, the default implementation is:
59 *
60 * (type) => [
61 * new RegExp(`(?:interface|type|class|enum)\\s+(${type.name})\\W`, "g")
62 * ]
63 *
64 */
65 typeMatch?: (type: GraphQLNamedType, defaultRegex: RegExp) => RegExp | RegExp[]
66 /**
67 * A list of typesNames or regular expressions matching type names
68 * that should be resolved by this import. Provide an empty array if you
69 * wish to use the file for context and ensure no other types are matched.
70 */
71 onlyTypes?: (string | RegExp)[]
72 /**
73 * By default the import is configured 'import * as alias from', setting glob to false
74 * will change this to 'import alias from'
75 */
76 glob?: false
77}
Edit this page on Github