Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: input fields with default should not be typed as optional #563

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/typegenPrinter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export class TypegenPrinter {
if (!this.hasResolver(field, type)) {
if (typeof obj !== 'string') {
obj[field.name] = [
this.argSeparator(field.type as GraphQLInputType),
this.argSeparator(field.type as GraphQLInputType, false),
this.printOutputType(field.type),
]
}
Expand Down Expand Up @@ -552,13 +552,14 @@ export class TypegenPrinter {
}

normalizeArg(arg: GraphQLInputField | GraphQLArgument): [string, string] {
return [this.argSeparator(arg.type), this.argTypeRepresentation(arg.type)]
return [this.argSeparator(arg.type, Boolean(arg.defaultValue)), this.argTypeRepresentation(arg.type)]
}

argSeparator(type: GraphQLInputType) {
if (isNonNullType(type)) {
argSeparator(type: GraphQLInputType, hasDefaultValue: boolean) {
if (hasDefaultValue || isNonNullType(type)) {
return ':'
}

return '?:'
}

Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/sdlConverter.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export const PostFilters = inputObjectType({
type: OrderEnum,
required: true,
})
t.string(\\"search\\")
t.string(\\"search\\", { default: \\"nexus\\" })
}
});

Expand Down Expand Up @@ -354,7 +354,7 @@ const PostFilters = inputObjectType({
type: OrderEnum,
required: true,
})
t.string(\\"search\\")
t.string(\\"search\\", { default: \\"nexus\\" })
}
});

Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/typegenPrinter.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ exports[`typegenPrinter builds the input object type defs 1`] = `
}
PostFilters: { // input type
order: NexusGenEnums['OrderEnum']; // OrderEnum!
search?: string | null; // String
search: string | null; // String
}
}"
`;
Expand Down Expand Up @@ -154,7 +154,7 @@ export interface NexusGenInputs {
}
PostFilters: { // input type
order: NexusGenEnums['OrderEnum']; // OrderEnum!
search?: string | null; // String
search: string | null; // String
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/_sdl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ enum OrderEnum {

input PostFilters {
order: OrderEnum!
search: String
search: String = "nexus"
}

"""
Expand Down
2 changes: 1 addition & 1 deletion tests/typegen/schema.gen.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type Post implements Node {

input PostFilters {
order: OrderEnum!
search: String
search: String = "nexus"
}

type Query {
Expand Down
2 changes: 1 addition & 1 deletion tests/typegen/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface NexusGenInputs {
PostFilters: {
// input type
order: NexusGenEnums['OrderEnum'] // OrderEnum!
search?: string | null // String
search: string | null // String
}
}

Expand Down