Skip to content

Commit 712f13c

Browse files
committed
Fixed exception if nested @InputObjectType is undefined
1 parent 7881dc5 commit 712f13c

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/object_type_factory.spec.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,26 @@ describe('objectTypeFactory', function() {
5050
assert(GQLType._typeConfig.name === 'Obj');
5151
});
5252

53-
it('returns GraphQLInputObjectType with a class annotated by nested @InputObjectType objects', function() {
54-
@D.InputObjectType()
55-
class Nested { @D.Field() title: string; }
53+
it('returns GraphQLInputObjectType with a class annotated by nested @InputObjectType objects', function() {
54+
@D.InputObjectType()
55+
class Nested { @D.Field() title: string; }
5656

57-
@D.InputObjectType()
58-
class Obj { @D.Field() title: string; @D.Field({type: Nested }) nested: Nested; }
57+
@D.InputObjectType()
58+
class Obj { @D.Field() title: string; @D.Field({type: Nested }) nested: Nested; }
59+
const GQLType = objectTypeFactory(Obj, true);
60+
assert(GQLType._typeConfig.name === 'Obj');
61+
});
62+
63+
it('raises exception if nested @InputObjectType is undefined', function() {
64+
// this can be caused when order of `import` is messed up and/or nested type can not be infered
65+
@D.InputObjectType()
66+
class Obj { @D.Field() title: string; @D.Field({type: undefined }) nested: {}; }
67+
try {
5968
const GQLType = objectTypeFactory(Obj, true);
60-
assert(GQLType._typeConfig.name === 'Obj');
69+
assert.fail();
70+
} catch (e) {
71+
const err = e as SchemaFactoryError;
72+
assert(err.type === SchemaFactoryErrorType.NO_FIELD);
73+
}
6174
});
6275
});

src/object_type_factory.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ export function objectTypeFactory(target: Function, isInput?: boolean) {
2929
const fieldMetadataList = Reflect.getMetadata(GQ_FIELDS_KEY, target.prototype) as FieldTypeMetadata[];
3030
const fields: {[key: string]: any} = {};
3131
fieldMetadataList.forEach(def => {
32-
fields[def.name] = fieldTypeFactory(target, def, isInput);
32+
let field = fieldTypeFactory(target, def, isInput);
33+
if (!field) {
34+
// tslint:disable-next-line:max-line-length
35+
throw new SchemaFactoryError(`@ObjectType()'s ${def.name} is annotated by @Filed() but no type could be inferred`, SchemaFactoryErrorType.NO_FIELD);
36+
}
37+
fields[def.name] = field;
3338
});
3439
if (!!isInput) {
3540
objectTypeRepository[objectTypeMetadata.name] = new graphql.GraphQLInputObjectType({

0 commit comments

Comments
 (0)