Skip to content

Commit f49d0b0

Browse files
author
Jason Kuhrt
authored
chore(docs): update to use new 0.19 api (#667)
1 parent 1551506 commit f49d0b0

12 files changed

+87
-100
lines changed

docs/content/010-getting-started/03-tutorial/03-chapter-2-writing-your-first-schema.mdx

+16-19
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,15 @@ We'll start by letting API clients read the drafts of your blog.
149149
<block>
150150

151151
```ts
152-
// api/graphql/Post.ts // 1
152+
// api/graphql/Post.ts // 1
153153

154154
import { objectType, extendType } from '@nexus/schema'
155155

156156
export const PostQuery = extendType({
157-
type: 'Query', // 2
157+
type: 'Query', // 2
158158
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
163161
})
164162
},
165163
})
@@ -181,19 +179,20 @@ type Query {
181179

182180
1. The Query object is a central place in your schema 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.
183181
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]`.
184185
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 👇
186188

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+
```
188194

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 👇
191195

192-
```ts
193-
definition(t) {
194-
t.list.field('drafts', { ... })
195-
}
196-
```
197196

198197
<div class="NextIs SectionDivider"></div>
199198

@@ -223,9 +222,8 @@ import { extendType } from '@nexus/schema'
223222
export const PostQuery = extendType({
224223
type: 'Query',
225224
definition(t) {
226-
t.field('drafts', {
225+
t.nonNull.list.field('drafts', {
227226
type: 'Post',
228-
list: true,
229227
+ resolve() {
230228
+ return [{ id: 1, title: 'Nexus', body: '...', published: false }]
231229
+ },
@@ -246,9 +244,8 @@ import { extendType } from '@nexus/schema'
246244
export const PostQuery = extendType({
247245
type: 'Query',
248246
definition(t) {
249-
t.field('drafts', {
247+
t.nonNull.list.field('drafts', {
250248
type: 'Post',
251-
list: true,
252249
resolve() {
253250
return [{ id: 1, title: 'Nexus', body: '...', published: false }]
254251
},

docs/content/010-getting-started/03-tutorial/04-chapter-3-adding-mutations-to-your-api.mdx

+10-13
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,8 @@ As before we will take the collocation approach.
241241
export const PostMutation = extendType({
242242
type: 'Mutation',
243243
definition(t) {
244-
t.field('createDraft', {
244+
t.nonNull.field('createDraft', {
245245
type: 'Post',
246-
nullable: false,
247246
resolve(_root, args, ctx) {
248247
ctx.db.posts.push(/*...*/)
249248
return // ...
@@ -282,12 +281,11 @@ import { extendType, stringArg } from '@nexus/schema'
282281
export const PostMutation = extendType({
283282
type: 'Mutation',
284283
definition(t) {
285-
t.field('createDraft', {
284+
t.nonNull.field('createDraft', {
286285
type: 'Post',
287-
nullable: false,
288286
+ args: { // 1
289-
+ title: stringArg({ required: true }), // 2
290-
+ body: stringArg({ required: true }), // 2
287+
+ title: nonNull(stringArg()), // 2
288+
+ body: nonNull(stringArg()), // 2
291289
+ },
292290
resolve(_root, args, ctx) {
293291
+ const draft = {
@@ -317,12 +315,11 @@ import { extendType, stringArg } from '@nexus/schema'
317315
export const PostMutation = extendType({
318316
type: 'Mutation',
319317
definition(t) {
320-
t.field('createDraft', {
318+
t.nonNull.field('createDraft', {
321319
type: 'Post',
322-
nullable: false,
323320
args: { // 1
324-
title: stringArg({ required: true }), // 2
325-
body: stringArg({ required: true }), // 2
321+
title: nonNull(stringArg()), // 2
322+
body: nonNull(stringArg()), // 2
326323
},
327324
resolve(_root, args, ctx) {
328325
const draft = {
@@ -351,7 +348,7 @@ Mutation {
351348
```
352349
353350
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.
355352
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.
356353
357354
## Model the domain: Part 2
@@ -374,7 +371,7 @@ export const PostMutation = extendType({
374371
+ t.field('publish', {
375372
+ type: 'Post',
376373
+ args: {
377-
+ draftId: intArg({ required: true }),
374+
+ draftId: nonNull(intArg()),
378375
+ },
379376
+ resolve(_root, args, ctx) {
380377
+ let draftToPublish = ctx.db.posts.find(p => p.id === args.draftId)
@@ -408,7 +405,7 @@ export const PostMutation = extendType({
408405
t.field('publish', {
409406
type: 'Post',
410407
args: {
411-
draftId: intArg({ required: true }),
408+
draftId: nonNull(intArg()),
412409
},
413410
resolve(_root, args, ctx) {
414411
let draftToPublish = ctx.db.posts.find(p => p.id === args.draftId)

docs/content/010-getting-started/03-tutorial/06-chapter-5-persisting-data-via-prisma.mdx

+11-9
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ Let's now replace all our previous in-memory db interactions with calls to the P
212212

213213
// ...
214214

215-
schema.extendType({
215+
extendType({
216216
type: 'Query',
217217
definition(t) {
218218
t.list.field('drafts', {
@@ -232,14 +232,14 @@ schema.extendType({
232232
},
233233
});
234234

235-
schema.extendType({
235+
extendType({
236236
type: 'Mutation',
237237
definition(t) {
238238
t.field('createDraft', {
239239
type: 'Post',
240240
args: {
241-
title: stringArg({ required: true }),
242-
body: stringArg({ required: true }),
241+
title: nonNull(stringArg()),
242+
body: nonNull(stringArg()),
243243
},
244244
resolve(_root, args, ctx) {
245245
const draft = {
@@ -258,7 +258,7 @@ schema.extendType({
258258
t.field('publish', {
259259
type: 'Post',
260260
args: {
261-
draftId: intArg({ required: true }),
261+
draftId: nonNull(intArg()),
262262
},
263263
resolve(_root, args, ctx) {
264264
- let postToPublish = ctx.db.posts.find((p) => p.id === args.draftId)
@@ -316,8 +316,8 @@ schema.extendType({
316316
t.field('createDraft', {
317317
type: 'Post',
318318
args: {
319-
title: stringArg({ required: true }),
320-
body: stringArg({ required: true }),
319+
title: nonNull(stringArg()),
320+
body: nonNull(stringArg()),
321321
},
322322
resolve(_root, args, ctx) {
323323
const draft = {
@@ -332,11 +332,13 @@ schema.extendType({
332332
t.field('publish', {
333333
type: 'Post',
334334
args: {
335-
draftId: intArg({ required: true }),
335+
draftId: nonNull(intArg()),
336336
},
337337
resolve(_root, args, ctx) {
338338
return ctx.db.post.update({
339-
where: { id: args.draftId },
339+
where: {
340+
id: args.draftId
341+
},
340342
data: {
341343
published: true,
342344
},

docs/content/014-guides/020-schema.mdx

+21-30
Original file line numberDiff line numberDiff line change
@@ -128,15 +128,17 @@ type Beta {
128128

129129
##### Lists and nullability
130130

131+
The following demonstration assumes the Nexus nullability defaults.
132+
131133
```ts
132134
objectType({
133135
name: 'Alpha',
134136
definition(t) {
135-
t.id('a', { nullable: true })
137+
t.id('a')
136138
t.list.id('b')
137-
t.list.id('c', { nullable: true })
138-
t.list.id('c', { list: [false] })
139-
t.list.id('c', { list: [false], nullable: true })
139+
t.nullable.list.id('c')
140+
t.list.nullable.id('d')
141+
t.nullable.list.nullable.id('e')
140142
},
141143
})
142144
```
@@ -146,8 +148,8 @@ type Alpha {
146148
a: ID
147149
b: [ID!]!
148150
c: [ID!]
149-
c: [ID]!
150-
c: [ID]
151+
d: [ID]!
152+
e: [ID]
151153
}
152154
```
153155

@@ -191,13 +193,11 @@ queryType({
191193
return Math.random() > 0.1 : 'Zeta' : 'Yolo'
192194
}
193195
})
194-
t.list.field('alphas', {
195-
type: 'Alpha',
196+
t.field('alphas', {
197+
type: list('Alpha'),
196198
args: {
197-
except: schema.arg({
198-
list: true,
199-
type: "Alpha",
200-
required: true,
199+
except: arg({
200+
type: list(nonNull("Alpha")),
201201
})
202202
},
203203
resolve(_root, args) {
@@ -400,14 +400,11 @@ type Query {
400400
```ts
401401
queryType({
402402
definition(t) {
403-
t.string('echo', {
403+
t.field('echo', {
404+
type: nonNull('String'),
404405
args: {
405-
message: schema.arg({
406-
type: 'String',
407-
nullable: false,
408-
}),
406+
message: nullable(stringArg()),
409407
},
410-
nullable: false,
411408
resolve(_root, args) {
412409
return args.message
413410
},
@@ -431,7 +428,7 @@ It is possible to use type and input/field layers together. This provides flexib
431428

432429

433430
```ts
434-
schema.queryType({
431+
queryType({
435432
// flip the global defaults
436433
nonNullDefaults: {
437434
input: true,
@@ -442,13 +439,9 @@ schema.queryType({
442439
// nullability config ... Except the following,
443440
// which effectively reverts back to what the global
444441
// defaults are:
445-
t.string('echo', {
446-
nullable: false,
442+
t.nonNull.string('echo', {
447443
args: {
448-
message: schema.arg({
449-
type: 'String',
450-
nullable: false,
451-
}),
444+
message: nonNull(stringArg()),
452445
},
453446
resolve(_root, args) {
454447
return args.message
@@ -475,14 +468,12 @@ When an arg has a default you might think that then it should be nullable to the
475468
```ts
476469
queryType({
477470
definition(t) {
478-
t.string('echo', {
471+
t.nonNull.string('echo', {
479472
args: {
480-
message: schema.arg({
481-
type: 'String',
473+
message: stringArg({
482474
default: 'nil via default',
483475
}),
484476
},
485-
nullable: false,
486477
resolve(_root, args) {
487478
const fallback = 'nil via client null'
488479
return args.message ?? fallback
@@ -566,7 +557,7 @@ query({
566557
definition(t) {
567558
t.user({
568559
args: {
569-
id: schema.arg({ type: 'ID', required: true }),
560+
id: nonNull(idArg()),
570561
},
571562
resolve(_, { id }, { db }) {
572563
return db.fetchUser({ where: { id } })

docs/content/015-api/011-subscription-type.mdx

+8-7
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,14 @@ const schema = makeSchema({
4848
type: 'User',
4949
args: {
5050
data: arg({
51-
required: true,
52-
type: inputObjectType({
53-
name: 'CreateOneUserInput',
54-
definition(t) {
55-
t.string('email', { required: true })
56-
},
57-
}),
51+
type: nonNull(
52+
inputObjectType({
53+
name: 'CreateOneUserInput',
54+
definition(t) {
55+
t.nonNull.string('email')
56+
},
57+
})
58+
),
5859
}),
5960
},
6061
async resolve(_, args) {

docs/content/015-api/050-input-object-type.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Defines a complex object which can be passed as an input value.
1313
export const InputType = inputObjectType({
1414
name: 'InputType',
1515
definition(t) {
16-
t.string('key', { required: true })
16+
t.nonNull.string('key')
1717
t.int('answer')
1818
},
1919
})

docs/content/015-api/060-args.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Defines an argument that can be used in any object or interface type. Args can b
1515
import { intArg, core } from '@nexus/schema'
1616

1717
function requiredInt(opts: core.ScalarArgConfig<number>) {
18-
return intArg({ ...opts, required: true })
18+
return nonNull(intArg({ ...opts }))
1919
}
2020
```
2121

0 commit comments

Comments
 (0)