-
Notifications
You must be signed in to change notification settings - Fork 275
/
definitionBlocks.ts
557 lines (515 loc) · 20.2 KB
/
definitionBlocks.ts
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
import { GraphQLFieldConfig, GraphQLFieldResolver, GraphQLInputFieldConfig } from 'graphql'
import { messages } from '../messages'
import { AllInputTypes, FieldResolver, GetGen, GetGen3, HasGen3, NeedsResolver } from '../typegenTypeHelpers'
import { ArgsRecord } from './args'
import { AllNexusInputTypeDefs, AllNexusOutputTypeDefs, NexusWrapKind } from './wrapping'
import { BaseScalars } from './_types'
export interface CommonFieldConfig {
/** The description to annotate the GraphQL SDL */
description?: string | null
/**
* Info about a field deprecation. Formatted as a string and provided with the deprecated directive on
* field/enum types and as a comment on input fields.
*/
deprecation?: string // | DeprecationInfo;
}
export type CommonOutputFieldConfig<TypeName extends string, FieldName extends string> = CommonFieldConfig & {
/** Arguments for the field */
args?: ArgsRecord
/**
* Custom extensions, as supported in graphql-js
*
* @see https://github.com/graphql/graphql-js/issues/1527
*/
extensions?: GraphQLFieldConfig<any, any>['extensions']
} & NexusGenPluginFieldConfig<TypeName, FieldName>
export type CommonInputFieldConfig<TypeName extends string, FieldName extends string> = CommonFieldConfig & {
/** The default value for the field, if any */
default?: GetGen3<'inputTypes', TypeName, FieldName>
/**
* Custom extensions, as supported in graphql-js
*
* @see https://github.com/graphql/graphql-js/issues/1527
*/
extensions?: GraphQLInputFieldConfig['extensions']
} & NexusGenPluginFieldConfig<TypeName, FieldName>
/**
* Deprecated, prefer core.CommonInputFieldConfig
*
* TODO(tim): Remove at 1.0
*/
export interface ScalarInputFieldConfig<T> extends CommonInputFieldConfig<any, any> {
default?: T
}
export interface OutputScalarConfig<TypeName extends string, FieldName extends string>
extends CommonOutputFieldConfig<TypeName, FieldName> {
/** Resolve method for the field */
resolve?: FieldResolver<TypeName, FieldName>
}
export interface NexusOutputFieldConfig<TypeName extends string, FieldName extends string>
extends OutputScalarConfig<TypeName, FieldName> {
type: GetGen<'allOutputTypes', string> | AllNexusOutputTypeDefs
}
export type NexusOutputFieldDef = NexusOutputFieldConfig<string, any> & {
name: string
configFor: 'outputField'
parentType: string
subscribe?: GraphQLFieldResolver<any, any>
wrapping?: NexusWrapKind[]
}
// prettier-ignore
export type ScalarOutSpread<TypeName extends string, FieldName extends string> =
NeedsResolver<TypeName, FieldName> extends true
? [ScalarOutConfig<TypeName, FieldName>]
: HasGen3<'argTypes', TypeName, FieldName> extends true
? [ScalarOutConfig<TypeName, FieldName>]
: [ScalarOutConfig<TypeName, FieldName>] | []
// prettier-ignore
export type ScalarOutConfig<TypeName extends string, FieldName extends string> =
NeedsResolver<TypeName, FieldName> extends true
? OutputScalarConfig<TypeName, FieldName> &
{
resolve: FieldResolver<TypeName, FieldName>
}
: OutputScalarConfig<TypeName, FieldName>
export type FieldOutConfig<TypeName extends string, FieldName extends string> = NeedsResolver<
TypeName,
FieldName
> extends true
? NexusOutputFieldConfig<TypeName, FieldName> & {
resolve: FieldResolver<TypeName, FieldName>
}
: NexusOutputFieldConfig<TypeName, FieldName>
export interface OutputDefinitionBuilder {
typeName: string
addField(config: NexusOutputFieldDef): void
addDynamicOutputMembers(block: OutputDefinitionBlock<any>, wrapping?: NexusWrapKind[]): void
warn(msg: string): void
}
export interface InputDefinitionBuilder {
typeName: string
addField(config: NexusInputFieldDef): void
addDynamicInputFields(block: InputDefinitionBlock<any>, wrapping?: NexusWrapKind[]): void
warn(msg: string): void
}
// prettier-ignore
export interface OutputDefinitionBlock<TypeName extends string>
extends NexusGenCustomOutputMethods<TypeName>,
NexusGenCustomOutputProperties<TypeName>
{}
/** The output definition block is passed to the "definition" function property of the "objectType" / "interfaceType" */
export class OutputDefinitionBlock<TypeName extends string> {
/** The name of the enclosing object type. */
readonly typeName: string
constructor(protected typeBuilder: OutputDefinitionBuilder, protected wrapping?: NexusWrapKind[]) {
this.typeName = typeBuilder.typeName
this.typeBuilder.addDynamicOutputMembers(this, this.wrapping)
}
/**
* [API Docs](https://nxs.li/docs/api/list) | [GraphQL 2018
* Spec](https://spec.graphql.org/June2018/#sec-Type-System.List)
*
* Chain this property to wrap the right-hand-side type (the field type, another list, nonNull, etc.) with a
* List type.
*
* Chains are read backwards, right to left, like function composition. In other words the thing on the left
* wraps the thing on the right.
*
* This is a shorthand equivalent to:
*
* `t.field('...', { type: list('...') })`
*
* @example
* objectType({
* name: 'User',
* definition(t) {
* t.list.nonNull.string('aliases')
* },
* })
*
* // GraphQL SDL
* // -----------
* //
* // type User {
* // aliases: [String!]
* // }
*/
get list() {
return this._wrapClass('List')
}
/**
* [API Docs](https://nxs.li/docs/api/nonNull) | [Nexus Nullability
* Guide](https://nexusjs.org/docs/guides/nullability) | [GraphQL 2018
* Spec](https://spec.graphql.org/June2018/#sec-Type-System.Non-Null)
*
* Chain this property to wrap the right-hand-side type (the field type or a list) with a Non-Null type.
*
* In Nexus output types are nullable by default so this is useful to configure a field differently. Note if
* you find yourself using this most of the time then what you probably what is to change the
* nonNullDefaults configuration either gloally in your makeSchema config or at the type definition level
* in one of your type configs to be false for outputs.
*
* Chains are read backwards, right to left, like function composition. In other words the thing on the left
* wraps the thing on the right.
*
* This is a shorthand equivalent to:
*
* `t.field('...', { type: nonNull('...') })`
*
* @example
* objectType({
* name: 'User',
* definition(t) {
* t.nonNull.list.string('aliases')
* },
* })
*
* // GraphQL SDL
* // -----------
* //
* // type User {
* // aliases: [String]!
* // }
*/
get nonNull(): Omit<OutputDefinitionBlock<TypeName>, 'nonNull' | 'nullable'> {
return this._wrapClass('NonNull')
}
/**
* [API Docs](https://nxs.li/docs/api/null) | [Nexus Nullability
* Guide](https://nexusjs.org/docs/guides/nullability) | [GraphQL 2018
* Spec](https://spec.graphql.org/June2018/#sec-Type-System.Non-Null)
*
* Chain this property to _unwrap_ the right-hand-side type (the field type or a list) of a Non-Null type.
*
* In Nexus output types are nullable by default so this is only useful when you have changed your
* nonNullDefaults configuration either gloally in your makeSchema config or at the type definition level
* in one of your type configs to be false for outputs.
*
* Chains are read backwards, right to left, like function composition. In other words the thing on the left
* wraps the thing on the right.
*
* This is a shorthand equivalent to:
*
* `t.field('...', { type: nullable('...') })`
*
* @example
* objectType({
* name: 'User',
* nonNullDefaults: {
* outputs: true,
* },
* definition(t) {
* t.id('id')
* t.nullable.string('bio')
* },
* })
*
* // GraphQL SDL
* // -----------
* //
* // type User {
* // id: ID!
* // bio: String
* // }
*/
get nullable(): Omit<OutputDefinitionBlock<TypeName>, 'nonNull' | 'nullable'> {
return this._wrapClass('Null')
}
/**
* [GraphQL 2018 spec](https://spec.graphql.org/June2018/#sec-Boolean)
*
* Define a field whose type is Boolean.
*
* Boolean types are [scalars](https://spec.graphql.org/June2018/#sec-Scalars) representing true or false.
* They are represented in JavaScript using the [boolean primitive
* type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean).
*
* This is a shorthand equivalent to:
*
* ` t.field('...', { type: boolean() }) `
*
* @example
* objectType({
* name: 'User',
* definition(t) {
* t.boolean('active')
* },
* })
*
* @param name The name of this field. Must conform to the regex pattern: [_A-Za-z][_0-9A-Za-z]*
* @param config The configuration for things like the field's type, its description, its arguments, its
* resolver, and more. See jsdoc on each field within for details.
*
* This parameter is optional if no resolver is required. No resolver is required if the [source
* typing](https://nxs.li/guides/backing-types) includes a field whose name matches this one and whose
* type is compatable. The default resolver behaviour will be to simply return that field from the
* received source type.
*/
boolean<FieldName extends string>(name: FieldName, ...config: ScalarOutSpread<TypeName, FieldName>) {
this.addScalarField(name, 'Boolean', config)
}
/**
* [GraphQL 2018 spec](https://spec.graphql.org/June2018/#sec-String)
*
* Define a field whose type is String.
*
* String types are [scalars](https://spec.graphql.org/June2018/#sec-Scalars) representing UTF-8 (aka.
* unicode) character sequences. It is most often used to represent free-form human-readable text. They are
* represented in JavaScript using the [string priimtive
* type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String).
*
* This is a shorthand, equivalent to:
*
* ` t.field('...', { type: string() }) `
*
* @example
* objectType({
* name: 'User',
* definition(t) {
* t.string('bio')
* },
* })
*
* @param name The name of this field. Must conform to the regex pattern: [_A-Za-z][_0-9A-Za-z]*
* @param config The configuration for things like the field's type, its description, its arguments, its
* resolver, and more. See jsdoc on each field within for details.
*
* This parameter is optional if no resolver is required. No resolver is required if the [source
* typing](https://nxs.li/guides/backing-types) includes a field whose name matches this one and whose
* type is compatable. The default resolver behaviour will be to simply return that field from the
* received source type.
*/
string<FieldName extends string>(name: FieldName, ...config: ScalarOutSpread<TypeName, FieldName>) {
this.addScalarField(name, 'String', config)
}
/**
* [GraphQL 2018 spec](https://spec.graphql.org/June2018/#sec-ID)
*
* Define a field whose type is ID.
*
* ID types are [scalars](https://spec.graphql.org/June2018/#sec-Scalars) representing unique identifiers
* often used to refetch an object or as the key for a cache. It is serialized in the same way as the
* [String](https://spec.graphql.org/June2018/#sec-String) type but unlike String not intended to be
* human-readable. They are represented in JavaScript using the [string priimtive
* type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String).
*
* This is a shorthand, equivalent to:
*
* ` t.field('...', { type: id() }) `
*
* @example
* objectType({
* name: 'User',
* definition(t) {
* t.id('id')
* },
* })
*
* @param name The name of this field. Must conform to the regex pattern: [_A-Za-z][_0-9A-Za-z]*
* @param config The configuration for things like the field's type, its description, its arguments, its
* resolver, and more. See jsdoc on each field within for details.
*
* This parameter is optional if no resolver is required. No resolver is required if the [source
* typing](https://nxs.li/guides/backing-types) includes a field whose name matches this one and whose
* type is compatable. The default resolver behaviour will be to simply return that field from the
* received source type.
*/
id<FieldName extends string>(name: FieldName, ...config: ScalarOutSpread<TypeName, FieldName>) {
this.addScalarField(name, 'ID', config)
}
/**
* [GraphQL 2018 spec](https://spec.graphql.org/June2018/#sec-Int)
*
* Define a field whose type is Int.
*
* Int types are [scalars](https://spec.graphql.org/June2018/#sec-Scalars) representing a signed 32-bit
* numeric non-fractional value. They are represented in JavaScript using the [number primitive
* type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number).
*
* This is a shorthand equivalent to:
*
* ` t.field('...', { type: int() }) `
*
* @example
* objectType({
* name: 'User',
* definition(t) {
* t.int('age')
* },
* })
*
* @param name The name of this field. Must conform to the regex pattern: [_A-Za-z][_0-9A-Za-z]*
* @param config The configuration for things like the field's type, its description, its arguments, its
* resolver, and more. See jsdoc on each field within for details.
*
* This parameter is optional if no resolver is required. No resolver is required if the [source
* typing](https://nxs.li/guides/backing-types) includes a field whose name matches this one and whose
* type is compatable. The default resolver behaviour will be to simply return that field from the
* received source type.
*/
int<FieldName extends string>(name: FieldName, ...config: ScalarOutSpread<TypeName, FieldName>) {
this.addScalarField(name, 'Int', config)
}
/**
* [GraphQL 2018 spec](https://spec.graphql.org/June2018/#sec-Float)
*
* Define a field whose type is Float.
*
* Float types are [scalars](https://spec.graphql.org/June2018/#sec-Scalars) representing signed
* double‐precision fractional values as specified by IEEE 754. They are represented in JavaScript using
* the [number primitive
* type](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number).
*
* This is a shorthand, equivalent to:
*
* ` t.field('...', { type: float() }) `
*
* @example
* objectType({
* name: 'User',
* definition(t) {
* t.float('height')
* },
* })
*
* @param name The name of this field. Must conform to the regex pattern: [_A-Za-z][_0-9A-Za-z]*
* @param config The configuration for things like the field's type, its description, its arguments, its
* resolver, and more. See jsdoc on each field within for details.
*
* This parameter is optional if no resolver is required. No resolver is required if the [source
* typing](https://nxs.li/guides/backing-types) includes a field whose name matches this one and whose
* type is compatable. The default resolver behaviour will be to simply return that field from the
* received source type.
*/
float<FieldName extends string>(name: FieldName, ...config: ScalarOutSpread<TypeName, FieldName>) {
this.addScalarField(name, 'Float', config)
}
/**
* [GraphQL 2018 Spec](https://spec.graphql.org/June2018/#sec-Language.Fields)
*
* Define a field on this object.
*
* A field describes one discrete piece of information available to request within a [selection
* set](https://spec.graphql.org/June2018/#sec-Selection-Sets). They are in fact most of what any selection
* set will contain. Fields can be typed as scalars (marking the terminal point of a branch of a selection
* set) or as other object types in your schema thus allowing you to model relationships between things.
*
* @example
* objectType({
* name: 'User',
* definition(t) {
* t.field('id', {
* type: id(),
* description: 'The unique identification number for this user',
* })
* },
* })
*
* @param name The name of this field. Must conform to the regex pattern: [_A-Za-z][_0-9A-Za-z]*
* @param config The configuration for things like the field's type, its description, its arguments,
* its resolver, and more. See jsdoc on each field within for details.
*/
field<FieldName extends string>(name: FieldName, config: FieldOutConfig<TypeName, FieldName>) {
this.typeBuilder.addField({
name,
...config,
configFor: 'outputField',
wrapping: this.wrapping,
parentType: this.typeName,
} as any)
}
private _wrapClass(kind: NexusWrapKind): OutputDefinitionBlock<TypeName> {
const previousWrapping = this.wrapping?.[0]
if (
(kind === 'NonNull' || kind === 'Null') &&
(previousWrapping === 'NonNull' || previousWrapping === 'Null')
) {
return new OutputDefinitionBlock(this.typeBuilder, this.wrapping || [])
}
return new OutputDefinitionBlock(this.typeBuilder, [kind].concat(this.wrapping || []))
}
private addScalarField<FieldName extends string>(
fieldName: FieldName,
typeName: BaseScalars,
opts: [] | ScalarOutSpread<TypeName, any>
) {
let fieldConfig: FieldOutConfig<any, any> = {
type: typeName,
}
/* istanbul ignore if */
if (typeof opts[0] === 'function') {
fieldConfig.resolve = opts[0] as any
console.warn(messages.removedFunctionShorthand(typeName, fieldName))
} else {
fieldConfig = { ...fieldConfig, ...opts[0] }
}
this.field(fieldName, fieldConfig as any)
}
}
export interface NexusInputFieldConfig<TypeName extends string, FieldName extends string>
extends CommonInputFieldConfig<TypeName, FieldName> {
type: AllInputTypes | AllNexusInputTypeDefs
}
export type NexusInputFieldDef = NexusInputFieldConfig<string, string> & {
configFor: 'inputField'
name: string
wrapping?: NexusWrapKind[]
parentType: string
}
export interface InputDefinitionBlock<TypeName extends string> extends NexusGenCustomInputMethods<TypeName> {}
export class InputDefinitionBlock<TypeName extends string> {
readonly typeName: string
constructor(protected typeBuilder: InputDefinitionBuilder, protected wrapping?: NexusWrapKind[]) {
this.typeName = typeBuilder.typeName
this.typeBuilder.addDynamicInputFields(this, this.wrapping)
}
get list() {
return this._wrapClass('List')
}
get nonNull(): Omit<InputDefinitionBlock<TypeName>, 'nonNull' | 'nullable'> {
return this._wrapClass('NonNull')
}
get nullable(): Omit<InputDefinitionBlock<TypeName>, 'nonNull' | 'nullable'> {
return this._wrapClass('Null')
}
string<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
this.field(fieldName, { ...opts, type: 'String' })
}
int<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
this.field(fieldName, { ...opts, type: 'Int' })
}
boolean<FieldName extends string>(
fieldName: FieldName,
opts?: CommonInputFieldConfig<TypeName, FieldName>
) {
this.field(fieldName, { ...opts, type: 'Boolean' })
}
id<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
this.field(fieldName, { ...opts, type: 'ID' })
}
float<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
this.field(fieldName, { ...opts, type: 'Float' })
}
field<FieldName extends string>(
fieldName: FieldName,
fieldConfig: NexusInputFieldConfig<TypeName, FieldName>
) {
this.typeBuilder.addField({
name: fieldName,
...fieldConfig,
wrapping: this.wrapping,
parentType: this.typeName,
configFor: 'inputField',
})
}
private _wrapClass(kind: NexusWrapKind) {
const previousWrapping = this.wrapping?.[0]
if (
(kind === 'NonNull' || kind === 'Null') &&
(previousWrapping === 'NonNull' || previousWrapping === 'Null')
) {
return new InputDefinitionBlock(this.typeBuilder, this.wrapping || [])
}
return new InputDefinitionBlock(this.typeBuilder, [kind].concat(this.wrapping || []))
}
}