diff --git a/src/schema/schema-generator.ts b/src/schema/schema-generator.ts index ffa11adec..11396413b 100644 --- a/src/schema/schema-generator.ts +++ b/src/schema/schema-generator.ts @@ -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( interfaceClass => diff --git a/tests/functional/extensions.ts b/tests/functional/extensions.ts index 63b05e1da..b57813940 100644 --- a/tests/functional/extensions.ts +++ b/tests/functional/extensions.ts @@ -2,6 +2,7 @@ import "reflect-metadata"; import { type GraphQLFieldMap, type GraphQLInputObjectType, + GraphQLInterfaceType, type GraphQLObjectType, type GraphQLSchema, } from "graphql"; @@ -11,6 +12,7 @@ import { Field, FieldResolver, InputType, + InterfaceType, Mutation, ObjectType, Query, @@ -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) @@ -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 { @@ -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();