Skip to content
Open
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
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
70 changes: 63 additions & 7 deletions src/test/arg/basics.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { GraphQLString, GraphQLNonNull } from 'graphql';
import { Field, ObjectType, compileObjectType, Arg } from '~/domains';
import { graphql, GraphQLString, GraphQLNonNull } from 'graphql';
import {
Field,
ObjectType,
compileObjectType,
Arg,
SchemaRoot,
Query,
compileSchema,
} from '~/domains';

describe('Arguments with @Arg', () => {
it('Allows setting argument with @Arg decorator', () => {
Expand Down Expand Up @@ -91,25 +99,73 @@ describe('Arguments with @Arg', () => {
});

it('Will allow registering argument at runtime', () => {
@ObjectType()
Copy link
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
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', async () => {
@ObjectType()
class Foo {
@Field()
bar(
@Arg({ isNullable: true, defaultValue: 'default' })
baz: string,
): string {
return baz;
}
}
@SchemaRoot()
class FooSchema {
@Query()
foo(): Foo {
return new Foo();
}
}

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

const compiledSchema = compileSchema({ roots: [FooSchema] });
expect(await graphql(compiledSchema, '{ foo { bar } }')).toEqual({
data: { foo: { bar: 'default' } },
});
});
});