Skip to content

Commit

Permalink
Merge pull request #1140 from jshearer/fix/multi-level-interfaces
Browse files Browse the repository at this point in the history
fix(): Fix handling of multi-level interfaces
  • Loading branch information
kamilmysliwiec committed Nov 9, 2020
2 parents b3f6517 + 171ce0e commit e4158d8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
10 changes: 5 additions & 5 deletions lib/schema-builder/factories/object-type-definition.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export class ObjectTypeDefinitionFactory {
): ObjectTypeDefinition {
const prototype = Object.getPrototypeOf(metadata.target);
const getParentType = () => {
const parentTypeDefinition = this.typeDefinitionsStorage.getObjectTypeByTarget(
prototype,
);
const parentTypeDefinition =
this.typeDefinitionsStorage.getObjectTypeByTarget(prototype) ||
this.typeDefinitionsStorage.getInterfaceByTarget(prototype);
return parentTypeDefinition ? parentTypeDefinition.type : undefined;
};
return {
Expand All @@ -69,7 +69,7 @@ export class ObjectTypeDefinitionFactory {

private generateInterfaces(
metadata: ObjectTypeMetadata,
getParentType: () => GraphQLObjectType,
getParentType: () => GraphQLObjectType | GraphQLInterfaceType,
) {
const prototype = Object.getPrototypeOf(metadata.target);

Expand All @@ -95,7 +95,7 @@ export class ObjectTypeDefinitionFactory {
private generateFields(
metadata: ObjectTypeMetadata,
options: BuildSchemaOptions,
getParentType: () => GraphQLObjectType,
getParentType: () => GraphQLObjectType | GraphQLInterfaceType,
): () => GraphQLFieldConfigMap<any, any> {
const prototype = Object.getPrototypeOf(metadata.target);
metadata.properties.forEach(({ typeFn }) =>
Expand Down
20 changes: 9 additions & 11 deletions tests/code-first/recipes/models/recipe.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { Field, ID, InterfaceType, ObjectType } from '../../../../lib';
import { METADATA_FACTORY_NAME } from '../../../../lib/plugin/plugin-constants';

@InterfaceType()
export abstract class Base {
@Field((type) => ID)
id: string;
}

@InterfaceType({
description: 'example interface',
resolveType: (value) => {
return Recipe;
},
})
export abstract class IRecipe {
@Field((type) => ID)
id: string;

export abstract class IRecipe extends Base {
@Field()
title: string;
}

@ObjectType({ implements: IRecipe, description: 'recipe object type' })
export class Recipe {
@Field((type) => ID)
id: string;

@Field()
title: string;

export class Recipe extends IRecipe {
@Field({ nullable: true })
description?: string;

Expand All @@ -35,6 +32,7 @@ export class Recipe {
}

constructor(recipe: Partial<Recipe>) {
super();
Object.assign(this, recipe);
}

Expand Down

0 comments on commit e4158d8

Please sign in to comment.