Skip to content
Open
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
1 change: 1 addition & 0 deletions src/domains/arg/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ function convertArgsArrayToArgsMap(
argsMap[argName] = {
type: finalType,
description: argConfig.description,
defaultValue: argConfig.defaultValue,
};
});
return argsMap;
Expand Down
1 change: 1 addition & 0 deletions src/domains/arg/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export interface ArgOptions {
description?: string;
type?: any;
isNullable?: boolean;
defaultValue?: any;
}

export const defaultArgOptions: ArgOptions = {
Expand Down
1 change: 1 addition & 0 deletions src/domains/arg/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface ArgInnerConfig {
description?: string;
isNullable?: boolean;
type?: any;
defaultValue?: any;
}

export const argRegistry = new DeepWeakMap<
Expand Down
45 changes: 40 additions & 5 deletions src/test/arg/basics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,25 +91,60 @@ describe('Arguments with @Arg', () => {
});

it('Will allow registering argument at runtime', () => {
@ObjectType()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should keep focusing only on registering args at runtime. Having default values tested here pollutes this test

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I agree, I haven't actually changed this test at all, I have no idea why github has decided to render the diff like this, my own tools don't. Though it does look like something autoformatted the parameter block (line 97), maybe that's it.

class Foo {
@Field()
bar(baz: string, bazRequired: string): string {
return baz;
}
}

Arg({ type: String, isNullable: true })(Foo.prototype, 'bar', 0);
Arg({ type: String, isNullable: false })(Foo.prototype, 'bar', 1);

const [bazArg, bazRequiredArg] = compileObjectType(
Foo,
).getFields().bar.args;

expect(bazArg.type).toBe(GraphQLString);
expect(bazRequiredArg.type).toEqual(new GraphQLNonNull(GraphQLString));
});

it('Respects unset defaultValue @Arg option', () => {
@ObjectType()
class Foo {
@Field()
bar(
@Arg({ isNullable: true })
baz: string,
@Arg({ isNullable: false })
bazRequired: string,
): string {
return baz;
}
}

Arg({type: String, isNullable: true})(Foo.prototype, 'bar', 0);
Arg({type: String, isNullable: false})(Foo.prototype, 'bar', 1);

const [bazArg, bazRequiredArg] = compileObjectType(
Foo,
).getFields().bar.args;

expect(bazArg.type).toBe(GraphQLString);
expect(bazArg.defaultValue).toBe(undefined);
expect(bazRequiredArg.type).toEqual(new GraphQLNonNull(GraphQLString));
});

it('Respects defaultValue @Arg option', () => {
@ObjectType()
class Foo {
@Field()
bar(
@Arg({ isNullable: true, defaultValue: 'default' })
baz: string,
): string {
return baz;
}
}
const compiledObject = compileObjectType(Foo);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good to test it basing on resolving some field that is based on knowing default value.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I couldn't think of a way to do this, maybe defining a schema in the test, compiling it and executing against it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added such a condition, I hope async/await is OK, seemed the easiest way to do it.

const [bazArg] = compiledObject.getFields().bar.args;
expect(bazArg.type).toBe(GraphQLString);
expect(bazArg.defaultValue).toBe('default');
});
});