You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: docs/content/010-getting-started/03-tutorial/03-chapter-2-writing-your-first-schema.mdx
+16-19
Original file line number
Diff line number
Diff line change
@@ -149,17 +149,15 @@ We'll start by letting API clients read the drafts of your blog.
149
149
<block>
150
150
151
151
```ts
152
-
// api/graphql/Post.ts // 1
152
+
// api/graphql/Post.ts // 1
153
153
154
154
import { objectType, extendType } from '@nexus/schema'
155
155
156
156
export const PostQuery = extendType({
157
-
type: 'Query', // 2
157
+
type: 'Query', // 2
158
158
definition(t) {
159
-
t.field('drafts', { // 3
160
-
nullable: false, // 4
161
-
type: 'Post', // 5
162
-
list: true, // 6
159
+
t.nonNull.list.field('drafts', { // 3, 4, 5
160
+
type: 'Post', // 6, 7
163
161
})
164
162
},
165
163
})
@@ -181,19 +179,20 @@ type Query {
181
179
182
180
1. TheQueryobjectisacentralplaceinyourschema where many other types will appear. Like before with the modular GraphQL types decision we again can decide to be modular here. We could either create a new `api/graphql/Query.ts` module (not modular), or we could _collocate_ the exposure of Post object with its definition in `api/graphql/Post.ts` (modular). Staying consistent with before, we'll take the modular way.
183
181
1. To achieve collocation in Nexus we'll use `schema.extendType`. Its API is _very_ similar to `schema.objectType` with the difference that the defined fields are merged into the _targeted_ type.
182
+
1. `.nullable` specifies that clients will always get a value for this field. By default, in Nexus, all "output types" (types returned by fields) are nullable. This is for [best practice reasons](https://graphql.org/learn/best-practices/#nullability). In this case though we indeed want to guarantee that a list will always be returned, never `null`.
183
+
If you're ever dissatisfied with Nexus' defaults, not to worry, [you can change them](https://www.nexusjs.org/#/api/modules/main/exports/settings?id=schemanullableinputs).
184
+
1. `.list` augments the field's type spec, making it wrapped by a List type. Here, a `[Post]`.
184
185
1. The first parameter specifies the field's name, here `drafts`
185
-
1. `nullable: false` specifies that clients will always get a value for this field. By default, in Nexus, all "output types" (types returned by fields) are nullable. This is for [best practice reasons](https://graphql.org/learn/best-practices/#nullability). In this case though we indeed want to guarantee that a list will always be returned, never `null`.
186
+
1. `type: 'Post'` specifies what the field's type should be. Here, a `Post`
187
+
1. Nexus also allows you to specifiy lists and nullability on the type field. This example could be rewritten like so 👇
186
188
187
-
If you're ever dissatisfied with Nexus' defaults, not to worry, [you can change them](https://www.nexusjs.org/#/api/modules/main/exports/settings?id=schemanullableinputs).
189
+
```ts
190
+
t.field('drafts', {
191
+
type: nonNull(list('Post')),
192
+
})
193
+
```
188
194
189
-
1. `type: 'Post'` specifies what the field's type should be. Here, a `Post`
190
-
1. `list: true` augments the field's type spec, making it wrapped by a List type. Here, a `[Post]`. Nexus also provides the following shorthand for this 👇
191
195
192
-
```ts
193
-
definition(t) {
194
-
t.list.field('drafts', { ... })
195
-
}
196
-
```
197
196
198
197
<divclass="NextIs SectionDivider"></div>
199
198
@@ -223,9 +222,8 @@ import { extendType } from '@nexus/schema'
1. Add an `args` property to the field definition to define its args. Keys are arg names and values are type specifications.
354
-
2. Use the Nexus helpers for defining an arg type. There is one such helper for every GraphQL scalar such as `intArg` and `booleanArg`. If you want to reference a type like some InputObject then use `arg({ type: "..." })`.
351
+
2. Use the Nexus helpers for defining an arg type. There is one such helper for every GraphQL scalar such as `intArg` and `booleanArg`. If you want to reference a type like some InputObject then use `arg({ type: "..." })`. You can use the helpers `nonNull` and `nullable` to adjust the nullability type of the arg. You can use the functional helper `list` to turn the arg into a list type too.
355
352
3. In our resolver, access the args we specified above and pass them through to our custom logic. If you hover over the `args` parameter you'll see that Nexus has properly typed them including the fact that they cannot be undefined.
0 commit comments