Skip to content
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
39 changes: 39 additions & 0 deletions .changeset/calm-owls-deprecate.md
Original file line number Diff line number Diff line change
@@ -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.
8 changes: 4 additions & 4 deletions __tests__/fixtures/huge-schema/these6e8.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
69 changes: 69 additions & 0 deletions __tests__/supergraph/errors/INVALID-GRAPHQL.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")'));
});
});
});
Original file line number Diff line number Diff line change
@@ -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,
};
}
2 changes: 2 additions & 0 deletions src/supergraph/validation/validate-supergraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -95,6 +96,7 @@ export function validateSupergraph(
SubgraphNameRule,
RequiredArgumentOrFieldIsNotInaccessibleRule,
InterfaceSubtypeRule,
DeprecatedInterfaceFieldImplementationRule,
NoInaccessibleOnImplementedInterfaceFieldsRule,
ListSizeSlicingArgumentsRule,
ContextualArgumentRule,
Expand Down
Loading