Skip to content

Commit 2dd90b4

Browse files
authored
fix: #618 Add inputFieldDefTypes for declarativeWrappingPlugin (#682)
1 parent 79e6c02 commit 2dd90b4

File tree

21 files changed

+107
-20
lines changed

21 files changed

+107
-20
lines changed

src/definitions/definitionBlocks.ts

+30-14
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,22 @@ export type CommonOutputFieldConfig<TypeName extends string, FieldName extends s
2424
args?: ArgsRecord
2525
} & NexusGenPluginFieldConfig<TypeName, FieldName>
2626

27+
export type CommonInputFieldConfig<TypeName extends string, FieldName extends string> = CommonFieldConfig & {
28+
/**
29+
* The default value for the field, if any
30+
*/
31+
default?: GetGen3<'inputTypes', TypeName, FieldName>
32+
} & NexusGenPluginFieldConfig<TypeName, FieldName>
33+
34+
/**
35+
* Deprecated, prefer core.CommonInputFieldConfig
36+
*
37+
* TODO(tim): Remove at 1.0
38+
*/
39+
export interface ScalarInputFieldConfig<T> extends CommonInputFieldConfig<any, any> {
40+
default?: T
41+
}
42+
2743
export interface OutputScalarConfig<TypeName extends string, FieldName extends string>
2844
extends CommonOutputFieldConfig<TypeName, FieldName> {
2945
/**
@@ -182,15 +198,8 @@ export class OutputDefinitionBlock<TypeName extends string> {
182198
}
183199
}
184200

185-
export interface ScalarInputFieldConfig<T> extends CommonFieldConfig {
186-
/**
187-
* The default value for the field, if any
188-
*/
189-
default?: T
190-
}
191-
192201
export interface NexusInputFieldConfig<TypeName extends string, FieldName extends string>
193-
extends ScalarInputFieldConfig<GetGen3<'inputTypes', TypeName, FieldName>> {
202+
extends CommonInputFieldConfig<TypeName, FieldName> {
194203
type: AllInputTypes | AllNexusInputTypeDefs<string>
195204
}
196205

@@ -222,23 +231,26 @@ export class InputDefinitionBlock<TypeName extends string> {
222231
return this._wrapClass('Null')
223232
}
224233

225-
string(fieldName: string, opts?: ScalarInputFieldConfig<string>) {
234+
string<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
226235
this.addScalarField(fieldName, 'String', opts)
227236
}
228237

229-
int(fieldName: string, opts?: ScalarInputFieldConfig<number>) {
238+
int<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
230239
this.addScalarField(fieldName, 'Int', opts)
231240
}
232241

233-
boolean(fieldName: string, opts?: ScalarInputFieldConfig<boolean>) {
242+
boolean<FieldName extends string>(
243+
fieldName: FieldName,
244+
opts?: CommonInputFieldConfig<TypeName, FieldName>
245+
) {
234246
this.addScalarField(fieldName, 'Boolean', opts)
235247
}
236248

237-
id(fieldName: string, opts?: ScalarInputFieldConfig<string>) {
249+
id<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
238250
this.addScalarField(fieldName, 'ID', opts)
239251
}
240252

241-
float(fieldName: string, opts?: ScalarInputFieldConfig<number>) {
253+
float<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
242254
this.addScalarField(fieldName, 'Float', opts)
243255
}
244256

@@ -266,7 +278,11 @@ export class InputDefinitionBlock<TypeName extends string> {
266278
return new InputDefinitionBlock(this.typeBuilder, [kind].concat(this.wrapping || []))
267279
}
268280

269-
protected addScalarField(fieldName: string, typeName: BaseScalars, opts: ScalarInputFieldConfig<any> = {}) {
281+
protected addScalarField(
282+
fieldName: string,
283+
typeName: BaseScalars,
284+
opts: CommonInputFieldConfig<any, any> = {}
285+
) {
270286
this.typeBuilder.addField({
271287
name: fieldName,
272288
type: typeName,

src/plugin.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ export interface PluginConfig {
6262
*/
6363
description?: string
6464
/**
65-
* Any type definitions we want to add to the field definitions
65+
* Any type definitions we want to add to output field definitions
6666
*/
6767
fieldDefTypes?: StringLike | StringLike[]
68+
/**
69+
* Any type definitions we want to add to input field definitions
70+
*/
71+
inputFieldDefTypes?: StringLike | StringLike[]
6872
/**
6973
* Any type definitions we want to add to the type definition option
7074
*/
@@ -251,6 +255,7 @@ function validatePluginConfig(pluginConfig: PluginConfig): void {
251255
const validOptionalProps = [
252256
'description',
253257
'fieldDefTypes',
258+
'inputFieldDefTypes',
254259
'objectTypeDefTypes',
255260
'argTypeDefTypes',
256261
...optionalPropFns,

src/plugins/declarativeWrappingPlugin.ts

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export const declarativeWrappingPlugin = (config: DeclarativeWrappingPluginConfi
7171
name: 'declarativeWrapping',
7272
fieldDefTypes: config.disable ? undefined : DeclarativeWrapping,
7373
argTypeDefTypes: config.disable ? undefined : DeclarativeWrapping,
74+
inputFieldDefTypes: config.disable ? undefined : DeclarativeWrapping,
7475
description: 'Provides a declarative nullable/list API, available by default pre-0.19',
7576
onAddOutputField(field) {
7677
return {

src/typegenPrinter.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export class TypegenPrinter {
226226
if (typeof val === 'string') {
227227
const baseType = this.schema.getType(val)
228228
return this.prependDoc(
229-
` ${key}<FieldName extends string>(fieldName: FieldName, opts?: core.ScalarInputFieldConfig<core.GetGen3<"inputTypes", TypeName, FieldName>>): void // ${JSON.stringify(
229+
` ${key}<FieldName extends string>(fieldName: FieldName, opts?: core.CommonInputFieldConfig<TypeName, FieldName>): void // ${JSON.stringify(
230230
val
231231
)};`,
232232
baseType?.description
@@ -828,6 +828,9 @@ export class TypegenPrinter {
828828
const pluginFieldExt: string[] = [
829829
` interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {`,
830830
]
831+
const pluginInputFieldExt: string[] = [
832+
` interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {`,
833+
]
831834
const pluginArgExt: string[] = [` interface NexusGenPluginArgConfig {`]
832835
const pluginSchemaExt: string[] = [` interface NexusGenPluginSchemaConfig {`]
833836
const pluginTypeExt: string[] = [` interface NexusGenPluginTypeConfig<TypeName extends string> {`]
@@ -837,6 +840,9 @@ export class TypegenPrinter {
837840
if (plugin.config.fieldDefTypes) {
838841
pluginFieldExt.push(padLeft(this.printType(plugin.config.fieldDefTypes), ' '))
839842
}
843+
if (plugin.config.inputFieldDefTypes) {
844+
pluginInputFieldExt.push(padLeft(this.printType(plugin.config.inputFieldDefTypes), ' '))
845+
}
840846
if (plugin.config.objectTypeDefTypes) {
841847
pluginTypeExt.push(padLeft(this.printType(plugin.config.objectTypeDefTypes), ' '))
842848
}
@@ -851,6 +857,7 @@ export class TypegenPrinter {
851857
[
852858
pluginTypeExt.concat(' }').join('\n'),
853859
pluginFieldExt.concat(' }').join('\n'),
860+
pluginInputFieldExt.concat(' }').join('\n'),
854861
pluginSchemaExt.concat(' }').join('\n'),
855862
pluginArgExt.concat(' }').join('\n'),
856863
].join('\n'),

src/typegenTypeHelpers.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ declare global {
1010
interface NexusGenPluginSchemaConfig {}
1111
interface NexusGenPluginTypeConfig<TypeName extends string> {}
1212
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
13+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
1314
interface NexusGenPluginArgConfig {}
1415
}
1516

tests/__snapshots__/backingTypes.spec.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ declare global {
133133
}
134134
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
135135
}
136+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
137+
}
136138
interface NexusGenPluginSchemaConfig {
137139
}
138140
interface NexusGenPluginArgConfig {

tests/__snapshots__/typegenPrinter.spec.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ declare global {
370370
}
371371
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
372372
}
373+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
374+
}
373375
interface NexusGenPluginSchemaConfig {
374376
}
375377
interface NexusGenPluginArgConfig {

tests/integrations/abstractTypes/allStrategiesOptionalWhenTypeNameEnabled/__typegen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export interface NexusGenTypes {
138138
declare global {
139139
interface NexusGenPluginTypeConfig<TypeName extends string> {}
140140
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
141+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
141142
interface NexusGenPluginSchemaConfig {}
142143
interface NexusGenPluginArgConfig {}
143144
}

tests/integrations/abstractTypes/isTypeOfsImplemented/__typegen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export interface NexusGenTypes {
136136
declare global {
137137
interface NexusGenPluginTypeConfig<TypeName extends string> {}
138138
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
139+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
139140
interface NexusGenPluginSchemaConfig {}
140141
interface NexusGenPluginArgConfig {}
141142
}

tests/integrations/abstractTypes/missingIsTypeOf/__typegen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export interface NexusGenTypes {
136136
declare global {
137137
interface NexusGenPluginTypeConfig<TypeName extends string> {}
138138
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
139+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
139140
interface NexusGenPluginSchemaConfig {}
140141
interface NexusGenPluginArgConfig {}
141142
}

tests/integrations/abstractTypes/missingResolveType/__typegen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export interface NexusGenTypes {
136136
declare global {
137137
interface NexusGenPluginTypeConfig<TypeName extends string> {}
138138
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
139+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
139140
interface NexusGenPluginSchemaConfig {}
140141
interface NexusGenPluginArgConfig {}
141142
}

tests/integrations/abstractTypes/missingResolveTypeOrIsTypeOf/__typegen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export interface NexusGenTypes {
126126
declare global {
127127
interface NexusGenPluginTypeConfig<TypeName extends string> {}
128128
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
129+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
129130
interface NexusGenPluginSchemaConfig {}
130131
interface NexusGenPluginArgConfig {}
131132
}

tests/integrations/abstractTypes/missingTypenameDiscriminant/__typegen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export interface NexusGenTypes {
138138
declare global {
139139
interface NexusGenPluginTypeConfig<TypeName extends string> {}
140140
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
141+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
141142
interface NexusGenPluginSchemaConfig {}
142143
interface NexusGenPluginArgConfig {}
143144
}

tests/integrations/abstractTypes/resolveTypeImplemented/__typegen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export interface NexusGenTypes {
136136
declare global {
137137
interface NexusGenPluginTypeConfig<TypeName extends string> {}
138138
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
139+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
139140
interface NexusGenPluginSchemaConfig {}
140141
interface NexusGenPluginArgConfig {}
141142
}

tests/integrations/declarativeWrappingPlugin/__app.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1-
import { objectType, queryField, intArg } from '../../../src'
1+
import { objectType, queryField, intArg, inputObjectType } from '../../../src'
22
import './__typegen'
33

44
const DeclarativeWrappingOutput = objectType({
55
name: 'DeclarativeWrappingOutput',
66
definition(t) {
77
t.string('someNullField', {
88
nullable: true,
9+
args: {
10+
input: inputObjectType({
11+
name: 'InlineInputType',
12+
definition(t) {
13+
t.int('abc', { required: true })
14+
},
15+
}).asArg(),
16+
},
917
})
1018
t.string('someRequiredField', {
1119
nullable: false,

tests/integrations/declarativeWrappingPlugin/__typegen.ts

+33-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ declare global {
77
interface NexusGen extends NexusGenTypes {}
88
}
99

10-
export interface NexusGenInputs {}
10+
export interface NexusGenInputs {
11+
InlineInputType: {
12+
// input type
13+
abc: number // Int!
14+
}
15+
}
1116

1217
export interface NexusGenEnums {}
1318

@@ -66,6 +71,10 @@ export interface NexusGenArgTypes {
6671
// args
6772
int: number // Int!
6873
}
74+
someNullField: {
75+
// args
76+
input?: NexusGenInputs['InlineInputType'] | null // InlineInputType
77+
}
6978
}
7079
}
7180

@@ -75,7 +84,7 @@ export interface NexusGenTypeInterfaces {}
7584

7685
export type NexusGenObjectNames = keyof NexusGenObjects
7786

78-
export type NexusGenInputNames = never
87+
export type NexusGenInputNames = keyof NexusGenInputs
7988

8089
export type NexusGenEnumNames = never
8190

@@ -151,6 +160,28 @@ declare global {
151160
*/
152161
required?: boolean
153162
}
163+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
164+
/**
165+
* Whether the type can be null
166+
* @default (depends on whether nullability is configured in type or schema)
167+
* @see declarativeWrappingPlugin
168+
*/
169+
nullable?: boolean
170+
/**
171+
* Whether the type is list of values, or just a single value.
172+
* If list is true, we assume the type is a list. If list is an array,
173+
* we'll assume that it's a list with the depth. The boolean indicates whether
174+
* the type is required (non-null), where true = nonNull, false = nullable.
175+
* @see declarativeWrappingPlugin
176+
*/
177+
list?: true | boolean[]
178+
/**
179+
* Whether the type should be non null, `required: true` = `nullable: false`
180+
* @default (depends on whether nullability is configured in type or schema)
181+
* @see declarativeWrappingPlugin
182+
*/
183+
required?: boolean
184+
}
154185
interface NexusGenPluginSchemaConfig {}
155186
interface NexusGenPluginArgConfig {
156187
/**

tests/integrations/kitchenSink/__typegen.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ declare global {
1111
*/
1212
myCustomScalar<FieldName extends string>(
1313
fieldName: FieldName,
14-
opts?: core.ScalarInputFieldConfig<core.GetGen3<'inputTypes', TypeName, FieldName>>
14+
opts?: core.CommonInputFieldConfig<TypeName, FieldName>
1515
): void // "MyCustomScalar";
1616
title(...args: any): void
1717
}
@@ -273,6 +273,7 @@ export interface NexusGenTypes {
273273
declare global {
274274
interface NexusGenPluginTypeConfig<TypeName extends string> {}
275275
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
276+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
276277
interface NexusGenPluginSchemaConfig {}
277278
interface NexusGenPluginArgConfig {}
278279
}

tests/integrations/unionTooComplexToRepresent/__typegen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4716,6 +4716,7 @@ export interface NexusGenTypes {
47164716
declare global {
47174717
interface NexusGenPluginTypeConfig<TypeName extends string> {}
47184718
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
4719+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
47194720
interface NexusGenPluginSchemaConfig {}
47204721
interface NexusGenPluginArgConfig {}
47214722
}

tests/plugins/__snapshots__/fieldAuthorizePlugin.spec.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ declare global {
171171
*/
172172
authorize?: FieldAuthorizeResolver<TypeName, FieldName>
173173
}
174+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
175+
}
174176
interface NexusGenPluginSchemaConfig {
175177
}
176178
interface NexusGenPluginArgConfig {

tests/plugins/__snapshots__/queryComplexityPlugin.spec.ts.snap

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ declare global {
126126
*/
127127
complexity?: QueryComplexity<TypeName, FieldName>
128128
}
129+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
130+
}
129131
interface NexusGenPluginSchemaConfig {
130132
}
131133
interface NexusGenPluginArgConfig {

tests/typegen/types.gen.ts

+1
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ export interface NexusGenTypes {
228228
declare global {
229229
interface NexusGenPluginTypeConfig<TypeName extends string> {}
230230
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
231+
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
231232
interface NexusGenPluginSchemaConfig {}
232233
interface NexusGenPluginArgConfig {}
233234
}

0 commit comments

Comments
 (0)