From 4482b68cf3e31d649364b96a4e43b8dfa09f3d04 Mon Sep 17 00:00:00 2001 From: "John D. Johnson II" Date: Mon, 3 Mar 2025 13:22:46 -0700 Subject: [PATCH 1/4] test(extensions) test cases for interface extensions related to (#1775) These tests will fail without the next upcoming commit which adds extensions into the InterfaceType. --- tests/functional/extensions.ts | 55 +++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 4 deletions(-) diff --git a/tests/functional/extensions.ts b/tests/functional/extensions.ts index 63b05e1da..fa8bb21f7 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,26 @@ describe("Extensions", () => { } } + @InterfaceType({ + resolveType() { + return "SampleObjectInterfaceImplementation"; + }, + }) + @Extensions({ + meta: "interface_extension", + }) + class SampleInterfaceType { + @Extensions({ + meta: "interface_extension", + }) + @Field() + withInterfaceFieldExtension!: string; + } + @ObjectType({ + implements: [SampleInterfaceType], + }) + class SampleObjectInterfaceImplementation {} + @Resolver() class SampleResolver { @Query(() => SampleObjectType) @@ -97,12 +119,17 @@ describe("Extensions", () => { return new SampleObjectType(); } - @Query(() => ExtensionsOnClassObjectType) - extensionsOnClassObjectType(): ExtensionsOnClassObjectType { - return new ExtensionsOnClassObjectType(); + @Query(() => SampleObjectInterfaceImplementation) + sampleObjectInterfaceImplementation(): SampleObjectInterfaceImplementation { + return new SampleObjectInterfaceImplementation(); } - @Query() + @Query(() => SampleInterfaceType) + sampleInterfaceType(): SampleInterfaceType { + return new SampleInterfaceType(); + } + + @Query(() => ExtensionsOnClassObjectType) @Extensions({ mandatory: true }) queryWithExtensions(): string { return "queryWithExtensions"; @@ -269,6 +296,26 @@ describe("Extensions", () => { }); }); + describe("InterfaceObjectResolver", () => { + it("should add extensions to interface types", async () => { + const fields = ( + schema.getType("SampleObjectInterfaceImplementation") as GraphQLObjectType + ).getFields(); + expect(fields.withInterfaceFieldExtension.extensions).toEqual({ + meta: "interface_extension", + }); + }); + }); + + describe("InterfaceClassResolver", () => { + it("should add extensions to interface types", async () => { + const sampleInterface = schema.getType("SampleInterfaceType") as GraphQLInterfaceType; + expect(sampleInterface.extensions).toEqual({ + meta: "interface_extension", + }); + }); + }); + describe("Inheritance", () => { beforeAll(async () => { getMetadataStorage().clear(); From fb1926bfefbb772eeebfee136d0e10e68d418580 Mon Sep 17 00:00:00 2001 From: "John D. Johnson II" Date: Mon, 3 Mar 2025 13:24:36 -0700 Subject: [PATCH 2/4] fix(extensions) add interface class extensions related to (#1775) The tests in extensions.ts from commit 4482b68cf3e will fail without this commit. --- src/schema/schema-generator.ts | 1 + 1 file changed, 1 insertion(+) 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 => From eca9ffbf84a2eeeaeccc57e326c1bbaa5abbdaf6 Mon Sep 17 00:00:00 2001 From: "John D. Johnson II" Date: Mon, 3 Mar 2025 13:49:11 -0700 Subject: [PATCH 3/4] test(extensions) fixed testcases that were deleted in the previous commit fixed stylistic issues (#1775) --- tests/functional/extensions.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/tests/functional/extensions.ts b/tests/functional/extensions.ts index fa8bb21f7..1050ded77 100644 --- a/tests/functional/extensions.ts +++ b/tests/functional/extensions.ts @@ -97,16 +97,13 @@ describe("Extensions", () => { return "SampleObjectInterfaceImplementation"; }, }) - @Extensions({ - meta: "interface_extension", - }) + @Extensions({ meta: "interface_extension" }) class SampleInterfaceType { - @Extensions({ - meta: "interface_extension", - }) @Field() + @Extensions({ meta: "interface_extension" }) withInterfaceFieldExtension!: string; } + @ObjectType({ implements: [SampleInterfaceType], }) @@ -119,6 +116,11 @@ describe("Extensions", () => { return new SampleObjectType(); } + @Query(() => ExtensionsOnClassObjectType) + extensionsOnClassObjectType(): ExtensionsOnClassObjectType { + return new ExtensionsOnClassObjectType(); + } + @Query(() => SampleObjectInterfaceImplementation) sampleObjectInterfaceImplementation(): SampleObjectInterfaceImplementation { return new SampleObjectInterfaceImplementation(); @@ -129,7 +131,7 @@ describe("Extensions", () => { return new SampleInterfaceType(); } - @Query(() => ExtensionsOnClassObjectType) + @Query() @Extensions({ mandatory: true }) queryWithExtensions(): string { return "queryWithExtensions"; From 72f68adf81c65a58e252311566cbca98e46dede9 Mon Sep 17 00:00:00 2001 From: "John D. Johnson II" Date: Tue, 4 Mar 2025 05:24:09 -0700 Subject: [PATCH 4/4] test(extensions) made sure interface extensions work when object type extends interface type related to (#1775) Made sure the tests and code works when object type extends interface type and when one interface type extends another interface type. --- tests/functional/extensions.ts | 78 ++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 3 deletions(-) diff --git a/tests/functional/extensions.ts b/tests/functional/extensions.ts index 1050ded77..b57813940 100644 --- a/tests/functional/extensions.ts +++ b/tests/functional/extensions.ts @@ -107,7 +107,17 @@ describe("Extensions", () => { @ObjectType({ implements: [SampleInterfaceType], }) - class SampleObjectInterfaceImplementation {} + class SampleObjectInterfaceImplementation extends SampleInterfaceType {} + + @InterfaceType({ + implements: [SampleInterfaceType], + }) + class SampleInterfaceInterfaceImplementation extends SampleInterfaceType {} + + @ObjectType({ + implements: [SampleInterfaceInterfaceImplementation], + }) + class SampleObjectInterfaceInterfaceImplementation extends SampleInterfaceInterfaceImplementation {} @Resolver() class SampleResolver { @@ -126,6 +136,16 @@ describe("Extensions", () => { return new SampleObjectInterfaceImplementation(); } + @Query(() => SampleInterfaceInterfaceImplementation) + SampleInterfaceInterfaceImplementation(): SampleInterfaceInterfaceImplementation { + return new SampleInterfaceInterfaceImplementation(); + } + + @Query(() => SampleObjectInterfaceInterfaceImplementation) + SampleObjectInterfaceInterfaceImplementation(): SampleObjectInterfaceInterfaceImplementation { + return new SampleObjectInterfaceInterfaceImplementation(); + } + @Query(() => SampleInterfaceType) sampleInterfaceType(): SampleInterfaceType { return new SampleInterfaceType(); @@ -298,8 +318,15 @@ describe("Extensions", () => { }); }); - describe("InterfaceObjectResolver", () => { + 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(); @@ -307,15 +334,60 @@ describe("Extensions", () => { 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("InterfaceClassResolver", () => { + 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", () => {