Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not registering object types when interface type is used as object type field type #736

Merged
merged 2 commits into from
Oct 23, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Fixes
- fix crashing when of union's or interface type's `resolveType` function returns `undefined` or `null` (#731)
- fix crashing when no reflected type available for fields with params decorators (#724)
- fix not registering object types implementing interface type when interface type is used as object type field type (#736)

## v1.1.0
### Features
Expand Down
10 changes: 7 additions & 3 deletions src/schema/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,17 +129,21 @@ export abstract class SchemaGenerator {
this.buildTypesInfo(options.resolvers);

const orphanedTypes = options.orphanedTypes || (options.resolvers ? [] : undefined);
const schema = new GraphQLSchema({
const prebuiltSchema = new GraphQLSchema({
query: this.buildRootQueryType(options.resolvers),
mutation: this.buildRootMutationType(options.resolvers),
subscription: this.buildRootSubscriptionType(options.resolvers),
types: this.buildOtherTypes(orphanedTypes),
directives: options.directives,
});
const finalSchema = new GraphQLSchema({
...prebuiltSchema.toConfig(),
// run after first build to make `usedInterfaceTypes` working
types: this.buildOtherTypes(orphanedTypes),
})

BuildContext.reset();
this.usedInterfaceTypes = new Set<Function>();
return schema;
return finalSchema;
}

private static checkForErrors(options: SchemaGeneratorOptions) {
Expand Down
51 changes: 51 additions & 0 deletions tests/functional/interfaces-and-inheritance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1221,6 +1221,57 @@ describe("Interfaces and inheritance", () => {
);
});

it("should by default automatically register all and only the object types that implements an interface type used as field type", async () => {
getMetadataStorage().clear();
@InterfaceType()
class IFooBar {
@Field(() => String)
fooBarKind: string;
}
@ObjectType({ implements: IFooBar })
class Foo extends IFooBar {
fooBarKind = "Foo";
}
@ObjectType({ implements: IFooBar })
class Bar extends IFooBar {
fooBarKind = "Bar";
}
@ObjectType()
class FooBar {
@Field(() => IFooBar)
iFooBarField: IFooBar;
}
@Resolver()
class TestResolver {
@Query(() => FooBar)
foobar() {
return new FooBar();
}
}
const { schemaIntrospection } = await getSchemaInfo({
resolvers: [TestResolver],
});

expect(schemaIntrospection.types).toContainEqual(
expect.objectContaining({
kind: TypeKind.INTERFACE,
name: "IFooBar",
}),
);
expect(schemaIntrospection.types).toContainEqual(
expect.objectContaining({
kind: TypeKind.OBJECT,
name: "Bar",
}),
);
expect(schemaIntrospection.types).toContainEqual(
expect.objectContaining({
kind: TypeKind.OBJECT,
name: "Foo",
}),
);
});

it("should register only the object types from orphanedType when interface type has disabled auto registering", async () => {
@InterfaceType({ autoRegisterImplementations: false })
abstract class SampleUsedInterface {
Expand Down