API

list / nonNull

list

GraphQL Docs on List Type

Types (including arguments) can be passed into the list() function to wrap them in a list type.

1import { queryType, stringArg, list } from 'nexus'
2
3queryType({
4 definition(t) {
5 t.field('tags', {
6 type: list('String'), // -> [String]
7 args: {
8 ids: list(stringArg()) // or list('String') -> [String]
9 },
10 resolve() {
11 // ...
12 }
13 })
14 }
15})

⚠️ If nonNullDefaults.input or nonNullDefaults.output is true, list('String') will resolve to nonNull(list(nonNull('String'))), producing the following type: [String!]!

Below are some usage examples of list():

ExampleGraphQL Type (nonNullDefaults = false)GraphQL Type (nonNullDefaults = true)
list(nonNull('String'))[String!][String!]!
list(list('String'))[[String]][[String!]!]!
nonNull(list('String'))[String]![String!]!
nonNull(list(nonNull('String')))[String!]![String!]!

nonNull

GraphQL Docs on List Type

Types (including arguments) can be passed into the nonNull() function to mark them as non-null.

1import { queryType, stringArg, nonNull } from 'nexus'
2
3queryType({
4 definition(t) {
5 t.field('tags', {
6 type: nonNull('String') // => String!
7 args: {
8 id: nonNull(stringArg()) // or nonNull('String') => String!
9 },
10 resolve() {
11 // ...
12 }
13 })
14 }
15})

In case nonNullDefaults.output or nonNullDefaults.input is true, nonNull will behave as a no-op.

Below are some more usage examples of nonNull:

ExampleProduced GraphQL Type (nonNullDefaults = true)Produced GraphQL Type (nonNullDefaults = false)
nonNull('String')String!String!
nonNull(list('String'))[String]![String]!
list(nonNull('String'))[String!][String!]
nonNull(list(nonNull('String')))[String!]![String!]!
nonNull(nonNull('String'))String!String!
nonNull(nullable('String'))String!String!

nullable

The nullable() helper is intended to be used when nonNullDefaults.output or nonNullDefaults.input is true.

While input and output types are nullable by default, you can set them to be non-nullable globally, or per-type, thanks to the nonNullDefaults configuration.

When types are non-nullable by default:

  • String will resolve to nonNull('String'), producing the type String!
  • list('String') will resolve to nonNull(list(nonNull('String'))), producing the type [String!]!

To revert the non-nullability, we can use the nullable() helper to mark Types and Arguments as nullable.

1import { queryType, stringArg, nonNull } from 'nexus'
2
3queryType({
4 nonNullDefaults: {
5 input: true // input types are non-nullable by default
6 output: true // output types are non-nullable by default
7 },
8 definition(t) {
9 t.field('tags', {
10 type: list(nullable('String')) // => [String]!
11 args: {
12 id: nullable(stringArg()) // or nullable('String') => String
13 },
14 resolve() {
15 // ...
16 }
17 })
18 }
19})

In case nonNullDefaults.output or nonNullDefaults.input is false, nullable will behave as a no-op.

Below are some more usage examples of nullable():

ExampleGraphQL Type (nonNullDefaults = false)GraphQL Type (nonNullDefaults = true)
nullable('String')StringString
nullable(list('String'))[String][String!]
list(nullable('String'))[String][String]!
nullable(list(nullable('String')))[String][String]
nullable(nonNull('String'))StringString
nonNull(nullable('String'))String!String!
nullable(nullable('String'))StringString
Edit this page on Github