objectType

1objectType(typeName: string, fn: ObjectDefinitionBlock): NexusObjectType

The most basic components of a GraphQL schema are object types, a type you can fetch from your schema, with fields:

1const User = objectType({
2 name: 'User',
3 definition(t) {
4 t.int('id', { description: 'Id of the user' })
5 t.string('fullName', { description: 'Full name of the user' })
6 t.field('status', { type: 'StatusEnum' })
7 t.list.field('posts', {
8 type: Post, // or "Post"
9 resolve(root, args, ctx) {
10 return ctx.getUser(root.id).posts()
11 },
12 })
13 },
14})
15
16const Post = objectType({
17 name: 'Post',
18 definition(t) {
19 t.int('id')
20 t.string('title')
21 },
22})
23
24const StatusEnum = enumType({
25 name: 'StatusEnum',
26 members: {
27 ACTIVE: 1,
28 DISABLED: 2,
29 },
30})

queryType / mutationType are shorthand for the root types.

Check the type-definitions or the examples for a full illustration of the various options of objectType, or feel free to open a PR on the docs to help document!

Edit this page on Github