Skip to content
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
25 changes: 22 additions & 3 deletions src/specs/object.type-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ describe('objectTypeFactory', function () {
@D.ObjectType()
class Obj { @D.Field() title: string; }
const GQLType: any = objectTypeFactory(Obj);
assert(GQLType._typeConfig.name === 'Obj');
assert(GQLType._typeConfig.fields.title.type instanceof graphql.GraphQLScalarType);
assert(GQLType.name === 'Obj');
assert(GQLType.getFields().title.type instanceof graphql.GraphQLScalarType);
});

it('returns GraphQLInputObjectType with a class annotated by @InputObjectType', function () {
Expand All @@ -62,11 +62,30 @@ describe('objectTypeFactory', function () {
@D.InputObjectType()
class Obj { @D.Field() title: string; @D.Field({ type: undefined }) nested: {}; }
try {
const GQLType: any = objectTypeFactory(Obj, true);
const GQLType = objectTypeFactory(Obj, true);
GQLType.getFields();
assert.fail();
} catch (e) {
const err = e as SchemaFactoryError;
assert(err.type === SchemaFactoryErrorType.NO_FIELD);
}
});

it('returns GraphQLObjectType if includes circular references', function () {
@D.ObjectType()
class Obj { @D.Field() circle: Obj; }
const GQLType = objectTypeFactory(Obj);
assert(GQLType.name === 'Obj');
assert((GQLType.getFields().circle.type as graphql.GraphQLObjectType).getFields().circle.type instanceof graphql.GraphQLObjectType);
});

it('allows thunk circular dependecies', function () {
@D.ObjectType()
class A { @D.Field({type: () => B}) circle: any; } // tslint:disable-line:no-use-before-declare
@D.ObjectType()
class B { @D.Field({type: () => A}) circle: any; }
const GQLType = objectTypeFactory(A);
assert(GQLType.name === 'A');
assert((GQLType.getFields().circle.type as graphql.GraphQLObjectType).getFields().circle.type instanceof graphql.GraphQLObjectType);
});
});
6 changes: 5 additions & 1 deletion src/type-factory/field.type-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ function convertType(typeFn: Function, metadata: FieldMetadata | ArgumentMetadat
returnType = objectTypeFactory(typeFn, isInput);
}
} else {
returnType = metadata.type;
try {
returnType = typeof metadata.type === 'function' ? metadata.type() : metadata.type;
} catch (e) {
returnType = metadata.type;
}

if (returnType && returnType.prototype && getMetadataArgsStorage().filterUnionTypeByClass(returnType).length > 0) {
returnType = unionTypeFactory(returnType, isInput);
Expand Down
2 changes: 1 addition & 1 deletion src/type-factory/object.type-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function objectTypeFactory(target: Function, isInput: boolean = false): g
SchemaFactoryErrorType.NO_FIELD);
}

const fields = fieldMetadataList.reduce((map, fieldMetadata) => {
const fields = () => fieldMetadataList.reduce((map, fieldMetadata) => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🤔 clever 👏

I will leave this SO answer here for future reference on why this works https://stackoverflow.com/a/39290345/429521

let field = fieldTypeFactory(fieldMetadata.target, fieldMetadata.field, isInput);
if (!field) {
throw new SchemaFactoryError(`@ObjectType()'s ${fieldMetadata.field.name} is annotated by @Field() but no type could be inferred`,
Expand Down