GitHub image
nexus
⌘K
Exports
Readme
Functions
20
makeSchema
Docs
Import
Tests
3
References
2

Defines the GraphQL schema, by combining the GraphQL types defined by the GraphQL Nexus layer or any manually defined GraphQLType objects.

Requires at least one type be named "Query", which will be used as the root query type.

function makeSchema(config: SchemaConfig): NexusGraphQLSchema
arg
Docs
Import
Tests
3
References
2

API Docs | GraphQL.org Docs | GraphQL 2018 Spec

Define an argument. Arguments can be used with the args config of any field.

function arg<T'>(

Configuration for the argument like its type and description. See jsdoc on each config field for details.

config: NexusArgConfig<T'>
): NexusArgDef<any>
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
export const Mutation = mutationType({
definition(t) {
t.field('createDraft', {
type: 'Post',
args: {
title: arg({
type: 'String',
default: 'Untitled',
description: 'The title of this draft post.',
}),
body: nonNull(
arg({
type: 'String',
description: 'The content of this draft post.',
})
),
},
// ...
})
},
})
booleanArg
Docs
Import
Tests
3
References
2

API Docs | GraphQL.org Docs | GraphQL 2018 Spec

Define a string argument. Arguments can be used with the args config of any field.

This is a shorthand equivalent to:

arg({ type: 'Boolean', ... })

function booleanArg(

Configuration for the argument like its description. See jsdoc on each config field for details.

config?: ScalarArgConfig<boolean>
): NexusArgDef<any>
Example
1
2
3
4
5
6
7
8
9
10
11
export const Query = queryType({
definition(t) {
t.list.field('users', {
type: 'User',
args: {
active: booleanArg(),
},
// ...
})
},
})
floatArg
Docs
Import
Tests
3
References
2

API Docs | GraphQL.org Docs | GraphQL 2018 Spec

Define a string argument. Arguments can be used with the args config of any field.

This is a shorthand equivalent to:

arg({ type: 'Float', ... })

function floatArg(

Configuration for the argument like its description. See jsdoc on each config field for details.

config?: ScalarArgConfig<number>
): NexusArgDef<any>
Example
1
2
3
4
5
6
7
8
9
10
11
export const Query = queryType({
definition(t) {
t.field('search', {
type: 'SearchResult',
args: {
ratingAbove: floatArg(),
},
// ...
})
},
})
idArg
Docs
Import
Tests
3
References
2

API Docs | GraphQL.org Docs | GraphQL 2018 Spec

Define a string argument. Arguments can be used with the args config of any field.

This is a shorthand equivalent to:

arg({ type: 'ID', ... })

function idArg(

Configuration for the argument like its description. See jsdoc on each config field for details.

config?: ScalarArgConfig<string>
): NexusArgDef<any>
Example
1
2
3
4
5
6
7
8
9
10
11
export const Query = queryType({
definition(t) {
t.field('user', {
type: 'User',
args: {
id: idArg(),
},
// ...
})
},
})
intArg
Docs
Import
Tests
3
References
2

API Docs | GraphQL.org Docs | GraphQL 2018 Spec

Define a string argument. Arguments can be used with the args config of any field.

This is a shorthand equivalent to:

arg({ type: 'Int', ... })

function intArg(

Configuration for the argument like its description. See jsdoc on each config field for details.

config?: ScalarArgConfig<number>
): NexusArgDef<any>
Example
1
2
3
4
5
6
7
8
9
10
11
export const Query = queryType({
definition(t) {
t.field('search', {
type: 'SearchResult',
args: {
maxResults: intArg(),
},
// ...
})
},
})
stringArg
Docs
Import
Tests
3
References
2

API Docs | GraphQL.org Docs | GraphQL 2018 Spec

Define a string argument. Arguments can be used with the args config of any field.

This is a shorthand equivalent to:

arg({ type: 'String', ... })

function stringArg(

Configuration for the argument like its description. See jsdoc on each config field for details.

config?: ScalarArgConfig<string>
): NexusArgDef<any>
Example
1
2
3
4
5
6
7
8
9
10
11
12
export const Mutation = mutationType({
definition(t) {
t.field('createDraft', {
type: 'Post',
args: {
title: stringArg(),
body: stringArg(),
},
// ...
})
},
})
decorateType
Docs
Import
Tests
3
References
2
function decorateType<T'>(type: T', config: TypeExtensionConfig): T'
enumType
Docs
Import
Tests
3
References
2
function enumType<TypeName'>(
config: NexusEnumTypeConfig<TypeName'>
): NexusEnumTypeDef<TypeName'>
extendInputType
Docs
Import
Tests
3
References
2

Adds new fields to an existing inputObjectType in the schema. Useful when splitting your schema across several domains.

function extendInputType<TypeName'>(
config: NexusExtendInputTypeConfig<TypeName'>
): NexusExtendInputTypeDef<TypeName'>
extendType
Docs
Import
Tests
3
References
2

API Docs

Add new fields to an existing objectType.

This is useful when splitting your schema's type definitions across modules wherein each module is concerned with its own domain (User, Post, Comment, etc.). You may discover that some types are shared across domains and you want to co-locate the definition of the field contributions to where the domains they relate to are.

A classic example is contributing fields to root types Query, Mutation, or Subscription. Note that this use-case is so common Nexus ships dedicated functions for it: queryField, mutationField, subscriptionField.

You can extend types before defining them strictly with objectType or the root field functions (queryType etc.). The typing for "type" property will appear to suggest that you cannot, however once Nexus reflection has run you'll see that the type you "extended" exists in the schema and that your static typing error has been resolved. This behaviour is a convenience especially when extending root types which you might never define in your schema directly.

function extendType<TypeName'>(

The specification of which type to extend and how. This is basically a subset of the configuration object passed to the objectType function.

config: NexusExtendTypeConfig<TypeName'>
): NexusExtendTypeDef<any>
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// types/User.ts
export const User = objectType({
name: 'User',
// ...
})
// Remember: It does not matter if you have
// used queryType(...) elsewhere or not.
export const UserQuery = extendType({
type: 'Query',
definition(t) {
t.list.nonNull.field('users', {
type: 'User',
resolve() {
return // ...
},
})
},
})
inputObjectType
Docs
Import
Tests
3
References
2
function inputObjectType<TypeName'>(
config: NexusInputObjectTypeConfig<TypeName'>
): NexusInputObjectTypeDef<TypeName'>
interfaceType
Docs
Import
Tests
3
References
2

API Docs | Abstract Types Guide | 2018 GraphQL Spec

Defines an Interface type.

Interface types are one of the two abstract type in GraphQL. They let you express polymorphic fields wherein the field may return a number of different object types but they all share some subset of fields. Interface types in Nexus also serve as a way to share a set of fields amongst different object types.

function interfaceType<TypeName'>(

Specify your interface's name, its fields, and more. See each config property's jsDoc for more detail.

config: NexusInterfaceTypeConfig<TypeName'>
): NexusInterfaceTypeDef<TypeName'>
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
export const Media = interfaceType({
name: 'Media',
resolveType(source) {
return 'director' in source ? 'Movie' : 'Song'
},
definition(t) {
t.string('url')
},
})
export const Movie = objectType({
name: 'Movie',
definition(t) {
t.implements('Media')
t.string('director')
},
})
export const Song = objectType({
name: 'Song',
definition(t) {
t.implements('Media')
t.string('album')
},
})
// GraphQL SDL
// -----------
//
// interface Media {
// url: String
// }
//
// type Movie implements Media {
// director: String
// url: String
// }
//
// type Song implements Media {
// album: String
// url: String
// }
list
Docs
Import
Tests
3
References
2
function list<TypeName'>(type: TypeName'): NexusListDef<TypeName'>
mutationField
Docs
Import
Tests
3
References
2

API Docs | 2018 GraphQL Spec

Define one or more fields on the Mutation type.

Use this if you are going to modularize your schema and thus be wanting to contribute fields to Mutation type from multiple modules. You do not have to have previously defined a Mutation type before using this. If you haven't Nexus will create one automatically for you.

This is shorthand for:

extendType({ type: 'Mutation' })

If you need to leverage plugins or define multiple fields then use the typeBuilder overload variant of this function. Otherwise you may prefer to the field name/config variant.

function mutationField(

The same as the "definition" method you define on object type configurations.

typeBuilder: (t: OutputDefinitionBlock<"Mutation">) => void
): NexusExtendTypeDef<"Mutation">+1
Examples
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// User.ts
// Overload 1: Type Builder
mutationField((t) => {
t.field('signup', {
type: 'User',
args: {
email: stringArg(),
},
// ...
})
t.field('deactivate', {
type: 'User',
args: {
userId: idArg(),
},
// ...
})
})
1
2
3
4
5
6
7
8
9
10
// User.ts
// Overload 2: Field Name/Config
mutationField('signup', {
type: 'User',
args: {
email: stringArg(),
},
// ...
})
mutationType
Docs
Import
Tests
3
References
2

2018 GraphQL Spec

Define a Mutation type.

The Mutation type is one of three root types in GraphQL and its fields represent API operations your clients can run that may have side-effects.

You can only have one of these in your schema. If you are going to modularize your schema and thus be wanting to contribute fields to Mutation type from multiple modules then use mutationField intead.

This is a shorthand for:

objectType({ name: 'Mutation' })

function mutationType(

Specify your Mutation type's fields, description, and more. See each config property's jsDoc for more detail.

config: Omit<NexusObjectTypeConfig<"Mutation">, "name">
): NexusObjectTypeDef<"Mutation">
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
mutationType({
definition(t) {
t.field('signup', {
type: 'User',
args: {
email: stringArg(),
},
// ...
})
t.field('buy', {
type: 'Recipet',
args: {
productId: idArg(),
},
// ...
})
},
})
nonNull
Docs
Import
Tests
3
References
2

API Docs | Nullability Guide | 2018 GraphQL Spec

Modify a type to be Non-Null.

In Nexus input and output position types are nullable by default so use this to modify them so long as you've not changed the non-null defaults for one or both positions.

If you find yourself using this a large majority of the time then consider changing your nullability defaults.

function nonNull<TypeName'>(

The type to wrap in Non-Null. This may be expressed in one of three ways:

  1. As string literals matching the name of a builtin scalar. E.g.: 'ID', 'String', ...
  2. As string literals matching the name of another type. E.g.: 'User', 'Location', ... Thanks to Nexus' reflection system this is typesafe and autocompletable. This is the idiomatic approach in Nexus because it avoids excessive importing and circular references.
  3. As references to other enums or object type definitions. E.g.: User, Location You may also use other type modifier helpers like list() which in turn accept one of the three
type: TypeName'
): NexusNonNullDef<any>
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
objectType({
name: 'User',
definition(t) {
t.field('id', {
type: nonNull('ID'),
})
t.field('bio', {
args: {
format: nonNull(booleanArg()),
maxWords: intArg(),
},
type: 'String',
})
},
})
// GraphQL SDL
// -----------
//
// type User {
// id: ID!
// bio(maxWords: Int, format: Boolean!): String
// }
nullable
Docs
Import
Tests
3
References
2

API Docs | Nullability Guide | 2018 GraphQL Spec

Remove the Non-Null wrapper from a type, if present.

In Nexus input and output position types are nullable by default so this has no use until you've changed the non-null defaults for one or both positions.

If you find yourself using this a large majority of the time then consider changing your nullability defaults.

function nullable<TypeName'>(

The type to wrap in Non-Null. This may be expressed in one of three ways:

  1. As string literals matching the name of a builtin scalar. E.g.: 'ID', 'String', ...
  2. As string literals matching the name of another type. E.g.: 'User', 'Location', ... Thanks to Nexus' reflection system this is typesafe and autocompletable. This is the idiomatic approach in Nexus because it avoids excessive importing and circular references.
  3. As references to other enums or object type definitions. E.g.: User, Location You may also use other type modifier helpers like list() which in turn accept one of the three
type: TypeName'
): NexusNullDef<any>
Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
objectType({
name: 'User',
nonNullDefaults: {
inputs: true,
outputs: true,
},
definition(t) {
t.field('id', {
type: 'ID',
})
t.field('bio', {
args: {
format: booleanArg(),
maxWords: nullable(intArg()),
},
type: nullable('String'),
})
},
})
// GraphQL SDL
// -----------
//
// type User {
// id: ID!
// bio(maxWords: Int, format: Boolean!): String
// }
objectType
Docs
Import
Tests
3
References
2

API Docs | GraphQL.org Docs | GraphQL 2018 Spec

Define a GraphQL Object Type.

Object types are typically the most common kind of type present in a GraphQL schema. You give them a name and fields that model your domain. Fields are typed and can point to yet another object type you've defined.

function objectType<TypeName'>(

Specify your object's name, its fields, and more. See each config property's jsDoc for more detail.

config: NexusObjectTypeConfig<TypeName'>
): NexusObjectTypeDef<TypeName'>
Example
1
2
3
4
5
6
7
const Post = objectType({
name: 'Post',
definition(t) {
t.int('id')
t.string('title')
},
})
queryField
Docs
Import
Tests
3
References
2

API Docs | 2018 GraphQL Spec

Define one or more fields on the Query type.

The Query type is one of three root types in GraphQL and its fields represent API operations your clients can run that must not have side-effects.

Use this instead of queryType if you are going to modularize your schema and thus be wanting to contribute fields to Query type from multiple modules. You do not have to have previously defined a Query type before using this. If you haven't Nexus will create one automatically for you.

This is shorthand for:

extendType({ type: 'Query' })

If you need to leverage plugins or define multiple fields then use the typeBuilder overload variant of this function. Otherwise you may prefer to the field name/config variant.

function queryField(

The same as the "definition" method you define on object type configurations.

typeBuilder: (t: OutputDefinitionBlock<"Query">) => void
): NexusExtendTypeDef<"Query">+1
Examples
1
2
3
4
5
6
7
8
9
10
11
12
// User.ts
// Overload 1: Type Builder
queryField((t) => {
t.field('user', {
type: 'User',
args: {
id: idArg(),
},
// ...
})
})
1
2
3
4
5
6
7
8
9
10
// User.ts
// Overload 2: Field Name/Config
queryField('user', {
type: 'User',
args: {
id: idArg(),
},
// ...
})
Description
Scalable, strongly typed GraphQL schema development
Install
yarn add nexus
Copy
Details
MIT License
2 Dependencies
Native TypeScript Package
Metrics
Weekly Downloads