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

support defaultValue for input types #203

Merged
merged 18 commits into from
Dec 15, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion examples/simple-usage/recipe-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class RecipeResolver implements ResolverInterface<Recipe> {
@FieldResolver()
ratingsCount(
@Root() recipe: Recipe,
@Arg("minRate", type => Int, { nullable: true }) minRate: number = 0.0,
@Arg("minRate", type => Int, { nullable: true, defaultValue: 0.0 }) minRate: number,
): number {
return recipe.ratings.filter(rating => rating >= minRate).length;
}
Expand Down
2 changes: 1 addition & 1 deletion examples/simple-usage/schema.gql
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Recipe {
description: String
ratings: [Int!]!
creationDate: DateTime!
ratingsCount(minRate: Int): Int!
ratingsCount(minRate: Int = 0): Int!
averageRating: Float
}

Expand Down
1 change: 1 addition & 0 deletions src/decorators/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export type SubscriptionTopicFunc = (

export interface DecoratorTypeOptions {
nullable?: boolean;
defaultValue?: any;
}
export interface TypeOptions extends DecoratorTypeOptions {
array?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function wrapWithTypeOptions<T extends GraphQLType>(
if (typeOptions.array) {
gqlType = new GraphQLList(new GraphQLNonNull(gqlType));
}
if (!typeOptions.nullable) {
if (!typeOptions.nullable && typeOptions.defaultValue === undefined) {
gqlType = new GraphQLNonNull(gqlType);
}
return gqlType as T;
Expand Down
3 changes: 3 additions & 0 deletions src/schema/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export abstract class SchemaGenerator {
fieldsMap[field.schemaName] = {
description: field.description,
type: this.getGraphQLInputType(field.name, field.getType(), field.typeOptions),
defaultValue: field.typeOptions.defaultValue,
};
return fieldsMap;
},
Expand Down Expand Up @@ -410,6 +411,7 @@ export abstract class SchemaGenerator {
args[param.name] = {
description: param.description,
type: this.getGraphQLInputType(param.name, param.getType(), param.typeOptions),
defaultValue: param.typeOptions.defaultValue,
};
} else if (param.kind === "args") {
const argumentType = getMetadataStorage().argumentTypes.find(
Expand Down Expand Up @@ -437,6 +439,7 @@ export abstract class SchemaGenerator {
args[field.schemaName] = {
description: field.description,
type: this.getGraphQLInputType(field.name, field.getType(), field.typeOptions),
defaultValue: field.typeOptions.defaultValue,
};
});
}
Expand Down
12 changes: 11 additions & 1 deletion tests/functional/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ describe("Resolvers", () => {
@Arg("stringArrayArg", type => String) stringArrayArg: string[],
@Arg("explicitArrayArg", type => [String]) explicitArrayArg: any,
@Arg("nullableStringArg", { nullable: true }) nullableStringArg?: string,
@Arg("defaultStringArg", { defaultValue: "bob" }) defaultStringArg?: string,
benawad marked this conversation as resolved.
Show resolved Hide resolved
): any {
return "argMethodField";
}
Expand Down Expand Up @@ -270,7 +271,7 @@ describe("Resolvers", () => {
const argMethodFieldInnerType = argMethodFieldType.ofType as IntrospectionNamedTypeRef;

expect(argMethodField.name).toEqual("argMethodField");
expect(argMethodField.args).toHaveLength(8);
expect(argMethodField.args).toHaveLength(9);
expect(argMethodFieldType.kind).toEqual(TypeKind.NON_NULL);
expect(argMethodFieldInnerType.kind).toEqual(TypeKind.SCALAR);
expect(argMethodFieldInnerType.name).toEqual("String");
Expand Down Expand Up @@ -375,6 +376,15 @@ describe("Resolvers", () => {
expect(inputArgInnerType.kind).toEqual(TypeKind.INPUT_OBJECT);
expect(inputArgInnerType.name).toEqual("SampleInput");
});

it("should generate nullable string arg type with defaultValue for object field method", async () => {
const inputArg = argMethodField.args.find(arg => arg.name === "defaultStringArg")!;
const nullableDefaultValueStringArgType = inputArg.type as IntrospectionNamedTypeRef;

expect(inputArg.defaultValue).toBe('"bob"');
expect(nullableDefaultValueStringArgType.kind).toEqual(TypeKind.SCALAR);
expect(nullableDefaultValueStringArgType.name).toEqual("String");
});
});

describe("Args object", () => {
Expand Down