Skip to content

Fixes #1775 interface class extensions added #1776

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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/schema/schema-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ export abstract class SchemaGenerator {
name: interfaceType.name,
description: interfaceType.description,
astNode: getInterfaceTypeDefinitionNode(interfaceType.name, interfaceType.directives),
extensions: interfaceType.extensions,
interfaces: () => {
let interfaces = (interfaceType.interfaceClasses || []).map<GraphQLInterfaceType>(
interfaceClass =>
Expand Down
121 changes: 121 additions & 0 deletions tests/functional/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import "reflect-metadata";
import {
type GraphQLFieldMap,
type GraphQLInputObjectType,
GraphQLInterfaceType,
type GraphQLObjectType,
type GraphQLSchema,
} from "graphql";
Expand All @@ -11,6 +12,7 @@ import {
Field,
FieldResolver,
InputType,
InterfaceType,
Mutation,
ObjectType,
Query,
Expand Down Expand Up @@ -90,6 +92,33 @@ describe("Extensions", () => {
}
}

@InterfaceType({
resolveType() {
return "SampleObjectInterfaceImplementation";
},
})
@Extensions({ meta: "interface_extension" })
class SampleInterfaceType {
@Field()
@Extensions({ meta: "interface_extension" })
withInterfaceFieldExtension!: string;
}

@ObjectType({
implements: [SampleInterfaceType],
})
class SampleObjectInterfaceImplementation extends SampleInterfaceType {}

@InterfaceType({
implements: [SampleInterfaceType],
})
class SampleInterfaceInterfaceImplementation extends SampleInterfaceType {}

@ObjectType({
implements: [SampleInterfaceInterfaceImplementation],
})
class SampleObjectInterfaceInterfaceImplementation extends SampleInterfaceInterfaceImplementation {}

@Resolver()
class SampleResolver {
@Query(() => SampleObjectType)
Expand All @@ -102,6 +131,26 @@ describe("Extensions", () => {
return new ExtensionsOnClassObjectType();
}

@Query(() => SampleObjectInterfaceImplementation)
sampleObjectInterfaceImplementation(): SampleObjectInterfaceImplementation {
return new SampleObjectInterfaceImplementation();
}

@Query(() => SampleInterfaceInterfaceImplementation)
SampleInterfaceInterfaceImplementation(): SampleInterfaceInterfaceImplementation {
return new SampleInterfaceInterfaceImplementation();
}

@Query(() => SampleObjectInterfaceInterfaceImplementation)
SampleObjectInterfaceInterfaceImplementation(): SampleObjectInterfaceInterfaceImplementation {
return new SampleObjectInterfaceInterfaceImplementation();
}

@Query(() => SampleInterfaceType)
sampleInterfaceType(): SampleInterfaceType {
return new SampleInterfaceType();
}

@Query()
@Extensions({ mandatory: true })
queryWithExtensions(): string {
Expand Down Expand Up @@ -269,6 +318,78 @@ describe("Extensions", () => {
});
});

describe("Interface Fields", () => {
it("should add extensions to interface types", async () => {
const fields = (schema.getType("SampleInterfaceType") as GraphQLObjectType).getFields();
expect(fields.withInterfaceFieldExtension.extensions).toEqual({
meta: "interface_extension",
});
});

it("should add extensions to ObjectType which extends InterfaceType", async () => {
const fields = (
schema.getType("SampleObjectInterfaceImplementation") as GraphQLObjectType
).getFields();
expect(fields.withInterfaceFieldExtension.extensions).toEqual({
meta: "interface_extension",
});
});

it("should add extensions to Interface which extends InterfaceType", async () => {
const fields = (
schema.getType("SampleInterfaceInterfaceImplementation") as GraphQLObjectType
).getFields();
expect(fields.withInterfaceFieldExtension.extensions).toEqual({
meta: "interface_extension",
});
});

it("should add extensions to ObjectType which extends InterfaceType which extends InterfaceType", async () => {
const fields = (
schema.getType("SampleInterfaceInterfaceImplementation") as GraphQLObjectType
).getFields();
expect(fields.withInterfaceFieldExtension.extensions).toEqual({
meta: "interface_extension",
});
});
});

describe("Interface Class", () => {
it("should add extensions to interface types", async () => {
const sampleInterface = schema.getType("SampleInterfaceType") as GraphQLInterfaceType;
expect(sampleInterface.extensions).toEqual({
meta: "interface_extension",
});
});

it("should add extensions to ObjectType which extends InterfaceType", async () => {
const sampleInterface = schema.getType(
"SampleObjectInterfaceImplementation",
) as GraphQLInterfaceType;
expect(sampleInterface.extensions).toEqual({
meta: "interface_extension",
});
});

it("should add extensions to InterfaceType which extends InterfaceType", async () => {
const sampleInterface = schema.getType(
"SampleInterfaceInterfaceImplementation",
) as GraphQLInterfaceType;
expect(sampleInterface.extensions).toEqual({
meta: "interface_extension",
});
});

it("should add extensions to ObjectType which extends InterfaceType which extends InterfaceType", async () => {
const sampleInterface = schema.getType(
"SampleObjectInterfaceInterfaceImplementation",
) as GraphQLInterfaceType;
expect(sampleInterface.extensions).toEqual({
meta: "interface_extension",
});
});
});

describe("Inheritance", () => {
beforeAll(async () => {
getMetadataStorage().clear();
Expand Down