From c10d9c533094485268fba398a6759c2407948285 Mon Sep 17 00:00:00 2001 From: jdolle <1841898+jdolle@users.noreply.github.com> Date: Fri, 20 Nov 2026 16:20:30 -0800 Subject: [PATCH] Reject deprecated field if interface field is not --- .changeset/calm-owls-deprecate.md | 39 +++++++++++ .../fixtures/huge-schema/these6e8.graphql | 8 +-- .../supergraph/errors/INVALID-GRAPHQL.spec.ts | 69 +++++++++++++++++++ ...ted-interface-field-implementation-rule.ts | 50 ++++++++++++++ .../validation/validate-supergraph.ts | 2 + 5 files changed, 164 insertions(+), 4 deletions(-) create mode 100644 .changeset/calm-owls-deprecate.md create mode 100644 src/supergraph/validation/rules/deprecated-interface-field-implementation-rule.ts diff --git a/.changeset/calm-owls-deprecate.md b/.changeset/calm-owls-deprecate.md new file mode 100644 index 00000000..f0ae4170 --- /dev/null +++ b/.changeset/calm-owls-deprecate.md @@ -0,0 +1,39 @@ +--- +"@theguild/federation-composition": patch +--- + +Fail composition when a field is `@deprecated` but the interface field it implements is not. + +The GraphQL specification does not allow this, and graphql-js 17 rejects such a schema. Composition used to succeed and produce a supergraph and public schema that fail schema validation. + +This can happen within one subgraph, and also across subgraphs that are each valid on their own. For example, the `catalog` subgraph defines the interface and the `inventory` subgraph deprecates the field on the shared entity: + +```graphql +# catalog +interface Product { + sku: String! + title: String +} + +type Book implements Product @key(fields: "sku") { + sku: String! + title: String +} +``` + +```graphql +# inventory +type Book @key(fields: "sku") { + sku: String! @deprecated(reason: "Use upc") + upc: String! + inStock: Boolean! +} +``` + +After merging, `Book.sku` is deprecated and `Product.sku` is not. Composition now fails with an `INVALID_GRAPHQL` error: + +``` +Interface field Product.sku is not deprecated, so implementation field Book.sku must not be deprecated. +``` + +To fix it, deprecate the interface field as well, or remove the deprecation from the implementing field. diff --git a/__tests__/fixtures/huge-schema/these6e8.graphql b/__tests__/fixtures/huge-schema/these6e8.graphql index c2ed1edc..f10f882d 100644 --- a/__tests__/fixtures/huge-schema/these6e8.graphql +++ b/__tests__/fixtures/huge-schema/these6e8.graphql @@ -179,10 +179,10 @@ type large7aa { interface sharpca8 { midst81a: ID! - worst202: String! - until355: String! - untilb9e: faintf2b! - until756: [moneydec!]! + worst202: String! @deprecated + until355: String! @deprecated + untilb9e: faintf2b! @deprecated + until756: [moneydec!]! @deprecated below721: String! @deprecated pupil0ee: knock6dd } diff --git a/__tests__/supergraph/errors/INVALID-GRAPHQL.spec.ts b/__tests__/supergraph/errors/INVALID-GRAPHQL.spec.ts index ec3202e4..3249309f 100644 --- a/__tests__/supergraph/errors/INVALID-GRAPHQL.spec.ts +++ b/__tests__/supergraph/errors/INVALID-GRAPHQL.spec.ts @@ -100,4 +100,73 @@ testVersions((api, version) => { ]), ); }); + + // Apollo composes this, but the result is rejected by graphql-js 17. + api.runIf("guild", () => { + test("deprecated field -> interface field not deprecated", () => { + const compose = (nodeIdDirectives: string) => + api.composeServices([ + { + typeDefs: graphql` + extend schema + @link( + url: "https://specs.apollo.dev/federation/${version}" + import: ["@key"] + ) + + type Query { + node: Node + } + + interface Node { + id: ID! ${nodeIdDirectives} + } + + interface Entity implements Node { + id: ID! @deprecated(reason: "a") + } + + type Foo implements Entity & Node @key(fields: "id") { + id: ID! + } + `, + name: "a", + }, + { + typeDefs: graphql` + extend schema + @link( + url: "https://specs.apollo.dev/federation/${version}" + import: ["@key"] + ) + + type Foo @key(fields: "id") { + id: ID! @deprecated(reason: "b") + name: String + } + `, + name: "b", + }, + ]); + + const result = compose(""); + + assertCompositionFailure(result); + + expect(result.errors).toEqual([ + expect.objectContaining({ + message: + "Interface field Node.id is not deprecated, so implementation field Foo.id must not be deprecated.", + extensions: expect.objectContaining({ code: "INVALID_GRAPHQL" }), + }), + expect.objectContaining({ + message: + "Interface field Node.id is not deprecated, so implementation field Entity.id must not be deprecated.", + extensions: expect.objectContaining({ code: "INVALID_GRAPHQL" }), + }), + ]); + + assertCompositionSuccess(compose('@deprecated(reason: "c")')); + }); + }); }); diff --git a/src/supergraph/validation/rules/deprecated-interface-field-implementation-rule.ts b/src/supergraph/validation/rules/deprecated-interface-field-implementation-rule.ts new file mode 100644 index 00000000..7419753a --- /dev/null +++ b/src/supergraph/validation/rules/deprecated-interface-field-implementation-rule.ts @@ -0,0 +1,50 @@ +import { GraphQLError } from "graphql"; +import { + InterfaceTypeFieldState, + InterfaceTypeState, +} from "../../composition/interface-type.js"; +import { + ObjectTypeFieldState, + ObjectTypeState, +} from "../../composition/object-type.js"; +import { SupergraphVisitorMap } from "../../composition/visitor.js"; +import { SupergraphState } from "../../state.js"; +import { SupergraphValidationContext } from "../validation-context.js"; + +export function DeprecatedInterfaceFieldImplementationRule( + context: SupergraphValidationContext, + supergraph: SupergraphState, +): SupergraphVisitorMap { + function check( + typeState: ObjectTypeState | InterfaceTypeState, + fieldState: ObjectTypeFieldState | InterfaceTypeFieldState, + ) { + if (!fieldState.deprecated) { + return; + } + + for (const interfaceName of typeState.interfaces) { + const interfaceField = supergraph.interfaceTypes + .get(interfaceName) + ?.fields.get(fieldState.name); + + if (interfaceField && !interfaceField.deprecated) { + context.reportError( + new GraphQLError( + `Interface field ${interfaceName}.${fieldState.name} is not deprecated, so implementation field ${typeState.name}.${fieldState.name} must not be deprecated.`, + { + extensions: { + code: "INVALID_GRAPHQL", + }, + }, + ), + ); + } + } + } + + return { + ObjectTypeField: check, + InterfaceTypeField: check, + }; +} diff --git a/src/supergraph/validation/validate-supergraph.ts b/src/supergraph/validation/validate-supergraph.ts index 5dfb7476..fba233de 100644 --- a/src/supergraph/validation/validate-supergraph.ts +++ b/src/supergraph/validation/validate-supergraph.ts @@ -5,6 +5,7 @@ import { AuthOnRequiresRule } from "./rules/auth-on-requires-rule.js"; import { AuthOnContextRule } from "./rules/auth-on-context-rule.js"; import { ContextualArgumentRule } from "./rules/contextual-argument-rule.js"; import { DefaultValueUsesInaccessibleRule } from "./rules/default-value-uses-inaccessible-rule.js"; +import { DeprecatedInterfaceFieldImplementationRule } from "./rules/deprecated-interface-field-implementation-rule.js"; import { DirectiveCompositionRule } from "./rules/directive-composition-rule.js"; import { EnumValuesRule } from "./rules/enum-values-rule.js"; import { ExtensionWithBaseRule } from "./rules/extension-with-base.js"; @@ -95,6 +96,7 @@ export function validateSupergraph( SubgraphNameRule, RequiredArgumentOrFieldIsNotInaccessibleRule, InterfaceSubtypeRule, + DeprecatedInterfaceFieldImplementationRule, NoInaccessibleOnImplementedInterfaceFieldsRule, ListSizeSlicingArgumentsRule, ContextualArgumentRule,