Skip to content

Commit

Permalink
fix: #618 Add inputFieldDefTypes for declarativeWrappingPlugin (#682)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Nov 27, 2020
1 parent 79e6c02 commit 2dd90b4
Show file tree
Hide file tree
Showing 21 changed files with 107 additions and 20 deletions.
44 changes: 30 additions & 14 deletions src/definitions/definitionBlocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,22 @@ export type CommonOutputFieldConfig<TypeName extends string, FieldName extends s
args?: ArgsRecord
} & 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>
} & 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> {
/**
Expand Down Expand Up @@ -182,15 +198,8 @@ export class OutputDefinitionBlock<TypeName extends string> {
}
}

export interface ScalarInputFieldConfig<T> extends CommonFieldConfig {
/**
* The default value for the field, if any
*/
default?: T
}

export interface NexusInputFieldConfig<TypeName extends string, FieldName extends string>
extends ScalarInputFieldConfig<GetGen3<'inputTypes', TypeName, FieldName>> {
extends CommonInputFieldConfig<TypeName, FieldName> {
type: AllInputTypes | AllNexusInputTypeDefs<string>
}

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

string(fieldName: string, opts?: ScalarInputFieldConfig<string>) {
string<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
this.addScalarField(fieldName, 'String', opts)
}

int(fieldName: string, opts?: ScalarInputFieldConfig<number>) {
int<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
this.addScalarField(fieldName, 'Int', opts)
}

boolean(fieldName: string, opts?: ScalarInputFieldConfig<boolean>) {
boolean<FieldName extends string>(
fieldName: FieldName,
opts?: CommonInputFieldConfig<TypeName, FieldName>
) {
this.addScalarField(fieldName, 'Boolean', opts)
}

id(fieldName: string, opts?: ScalarInputFieldConfig<string>) {
id<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
this.addScalarField(fieldName, 'ID', opts)
}

float(fieldName: string, opts?: ScalarInputFieldConfig<number>) {
float<FieldName extends string>(fieldName: FieldName, opts?: CommonInputFieldConfig<TypeName, FieldName>) {
this.addScalarField(fieldName, 'Float', opts)
}

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

protected addScalarField(fieldName: string, typeName: BaseScalars, opts: ScalarInputFieldConfig<any> = {}) {
protected addScalarField(
fieldName: string,
typeName: BaseScalars,
opts: CommonInputFieldConfig<any, any> = {}
) {
this.typeBuilder.addField({
name: fieldName,
type: typeName,
Expand Down
7 changes: 6 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ export interface PluginConfig {
*/
description?: string
/**
* Any type definitions we want to add to the field definitions
* Any type definitions we want to add to output field definitions
*/
fieldDefTypes?: StringLike | StringLike[]
/**
* Any type definitions we want to add to input field definitions
*/
inputFieldDefTypes?: StringLike | StringLike[]
/**
* Any type definitions we want to add to the type definition option
*/
Expand Down Expand Up @@ -251,6 +255,7 @@ function validatePluginConfig(pluginConfig: PluginConfig): void {
const validOptionalProps = [
'description',
'fieldDefTypes',
'inputFieldDefTypes',
'objectTypeDefTypes',
'argTypeDefTypes',
...optionalPropFns,
Expand Down
1 change: 1 addition & 0 deletions src/plugins/declarativeWrappingPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export const declarativeWrappingPlugin = (config: DeclarativeWrappingPluginConfi
name: 'declarativeWrapping',
fieldDefTypes: config.disable ? undefined : DeclarativeWrapping,
argTypeDefTypes: config.disable ? undefined : DeclarativeWrapping,
inputFieldDefTypes: config.disable ? undefined : DeclarativeWrapping,
description: 'Provides a declarative nullable/list API, available by default pre-0.19',
onAddOutputField(field) {
return {
Expand Down
9 changes: 8 additions & 1 deletion src/typegenPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class TypegenPrinter {
if (typeof val === 'string') {
const baseType = this.schema.getType(val)
return this.prependDoc(
` ${key}<FieldName extends string>(fieldName: FieldName, opts?: core.ScalarInputFieldConfig<core.GetGen3<"inputTypes", TypeName, FieldName>>): void // ${JSON.stringify(
` ${key}<FieldName extends string>(fieldName: FieldName, opts?: core.CommonInputFieldConfig<TypeName, FieldName>): void // ${JSON.stringify(
val
)};`,
baseType?.description
Expand Down Expand Up @@ -828,6 +828,9 @@ export class TypegenPrinter {
const pluginFieldExt: string[] = [
` interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {`,
]
const pluginInputFieldExt: string[] = [
` interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {`,
]
const pluginArgExt: string[] = [` interface NexusGenPluginArgConfig {`]
const pluginSchemaExt: string[] = [` interface NexusGenPluginSchemaConfig {`]
const pluginTypeExt: string[] = [` interface NexusGenPluginTypeConfig<TypeName extends string> {`]
Expand All @@ -837,6 +840,9 @@ export class TypegenPrinter {
if (plugin.config.fieldDefTypes) {
pluginFieldExt.push(padLeft(this.printType(plugin.config.fieldDefTypes), ' '))
}
if (plugin.config.inputFieldDefTypes) {
pluginInputFieldExt.push(padLeft(this.printType(plugin.config.inputFieldDefTypes), ' '))
}
if (plugin.config.objectTypeDefTypes) {
pluginTypeExt.push(padLeft(this.printType(plugin.config.objectTypeDefTypes), ' '))
}
Expand All @@ -851,6 +857,7 @@ export class TypegenPrinter {
[
pluginTypeExt.concat(' }').join('\n'),
pluginFieldExt.concat(' }').join('\n'),
pluginInputFieldExt.concat(' }').join('\n'),
pluginSchemaExt.concat(' }').join('\n'),
pluginArgExt.concat(' }').join('\n'),
].join('\n'),
Expand Down
1 change: 1 addition & 0 deletions src/typegenTypeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare global {
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginArgConfig {}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/__snapshots__/backingTypes.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ declare global {
}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
}
interface NexusGenPluginSchemaConfig {
}
interface NexusGenPluginArgConfig {
Expand Down
2 changes: 2 additions & 0 deletions tests/__snapshots__/typegenPrinter.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,8 @@ declare global {
}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {
}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
}
interface NexusGenPluginSchemaConfig {
}
interface NexusGenPluginArgConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}
10 changes: 9 additions & 1 deletion tests/integrations/declarativeWrappingPlugin/__app.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
import { objectType, queryField, intArg } from '../../../src'
import { objectType, queryField, intArg, inputObjectType } from '../../../src'
import './__typegen'

const DeclarativeWrappingOutput = objectType({
name: 'DeclarativeWrappingOutput',
definition(t) {
t.string('someNullField', {
nullable: true,
args: {
input: inputObjectType({
name: 'InlineInputType',
definition(t) {
t.int('abc', { required: true })
},
}).asArg(),
},
})
t.string('someRequiredField', {
nullable: false,
Expand Down
35 changes: 33 additions & 2 deletions tests/integrations/declarativeWrappingPlugin/__typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ declare global {
interface NexusGen extends NexusGenTypes {}
}

export interface NexusGenInputs {}
export interface NexusGenInputs {
InlineInputType: {
// input type
abc: number // Int!
}
}

export interface NexusGenEnums {}

Expand Down Expand Up @@ -66,6 +71,10 @@ export interface NexusGenArgTypes {
// args
int: number // Int!
}
someNullField: {
// args
input?: NexusGenInputs['InlineInputType'] | null // InlineInputType
}
}
}

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

export type NexusGenObjectNames = keyof NexusGenObjects

export type NexusGenInputNames = never
export type NexusGenInputNames = keyof NexusGenInputs

export type NexusGenEnumNames = never

Expand Down Expand Up @@ -151,6 +160,28 @@ declare global {
*/
required?: boolean
}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
/**
* Whether the type can be null
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
nullable?: boolean
/**
* Whether the type is list of values, or just a single value.
* If list is true, we assume the type is a list. If list is an array,
* we'll assume that it's a list with the depth. The boolean indicates whether
* the type is required (non-null), where true = nonNull, false = nullable.
* @see declarativeWrappingPlugin
*/
list?: true | boolean[]
/**
* Whether the type should be non null, `required: true` = `nullable: false`
* @default (depends on whether nullability is configured in type or schema)
* @see declarativeWrappingPlugin
*/
required?: boolean
}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {
/**
Expand Down
3 changes: 2 additions & 1 deletion tests/integrations/kitchenSink/__typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ declare global {
*/
myCustomScalar<FieldName extends string>(
fieldName: FieldName,
opts?: core.ScalarInputFieldConfig<core.GetGen3<'inputTypes', TypeName, FieldName>>
opts?: core.CommonInputFieldConfig<TypeName, FieldName>
): void // "MyCustomScalar";
title(...args: any): void
}
Expand Down Expand Up @@ -273,6 +273,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}
1 change: 1 addition & 0 deletions tests/integrations/unionTooComplexToRepresent/__typegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4716,6 +4716,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}
2 changes: 2 additions & 0 deletions tests/plugins/__snapshots__/fieldAuthorizePlugin.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,8 @@ declare global {
*/
authorize?: FieldAuthorizeResolver<TypeName, FieldName>
}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
}
interface NexusGenPluginSchemaConfig {
}
interface NexusGenPluginArgConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ declare global {
*/
complexity?: QueryComplexity<TypeName, FieldName>
}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {
}
interface NexusGenPluginSchemaConfig {
}
interface NexusGenPluginArgConfig {
Expand Down
1 change: 1 addition & 0 deletions tests/typegen/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ export interface NexusGenTypes {
declare global {
interface NexusGenPluginTypeConfig<TypeName extends string> {}
interface NexusGenPluginFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginInputFieldConfig<TypeName extends string, FieldName extends string> {}
interface NexusGenPluginSchemaConfig {}
interface NexusGenPluginArgConfig {}
}

0 comments on commit 2dd90b4

Please sign in to comment.