Skip to content

fix: do not apply contract logic on external object's fields #6907

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
71 changes: 71 additions & 0 deletions integration-tests/tests/schema/contracts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,77 @@
`);
});

test(`@external object's fields are do not have contract tag applied`, async () => {
const result = await client.composeAndValidate.mutate({
type: 'federation',
native: true,
schemas: [
{
raw: /* GraphQL */ `
extend schema
@link(url: "https://specs.apollo.dev/link/v1.0")
@link(url: "https://specs.apollo.dev/federation/v2.8", import: ["@key", "@tag"])

type Query {
user: User @tag(name: "public")
}

type User @key(fields: "id") {
id: ID! @tag(name: "public")
ssn: String
}
`,
source: 'user.graphql',
url: 'https://localhost:3000/graphql',
},
{
raw: /* GraphQL */ `
extend schema
@link(url: "https://specs.apollo.dev/link/v1.0")
@link(
url: "https://specs.apollo.dev/federation/v2.8"
import: ["@key", "@external", "@requires", "@tag"]
)

extend type User @external {
ssn: String
}

type User @key(fields: "id") {
id: ID!
creditScore: Int @requires(fields: "ssn") @tag(name: "public")
}
`,
source: 'credit.graphql',
url: 'https://localhost:3001/graphql',
},
],
external: null,
contracts: [
{
id: 'foo',
filter: {
removeUnreachableTypesFromPublicApiSchema: false,
exclude: null,
include: ['public'],
},
},
],
});
expect(result.errors).toEqual([]);

Check failure on line 678 in integration-tests/tests/schema/contracts.spec.ts

View workflow job for this annotation

GitHub Actions / test / integration (2)

tests/schema/contracts.spec.ts > @external object's fields are do not have contract tag applied

AssertionError: expected [ { …(2) } ] to deeply equal [] - Expected + Received - [] + [ + { + "message": "[credit.graphql] On field \"User.creditScore\", for @requires(fields: \"ssn\"): field \"User.ssn\" should not be part of a @requires since it is already provided by this subgraph (it is not marked @external)", + "source": "composition", + }, + ] ❯ tests/schema/contracts.spec.ts:678:25
expect(result.contracts?.[0].errors).toEqual([]);
expect(result.contracts?.[0].sdl).toMatchInlineSnapshot(`
type User {
id: ID!
creditScore: Int
}

type Query {
user: User
}
`);
});

test('include with exclude is possible', async () => {
const result = await client.composeAndValidate.mutate({
type: 'federation',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,38 @@ describe('applyTagFilterToInaccessibleTransformOnSubgraphSchema', () => {
}
`);
});

test('external objects do not apply inaccessible based on tags', () => {
const filter: Federation2SubgraphDocumentNodeByTagsFilter = {
include: new Set(['public']),
exclude: new Set(),
};
const sdl = parse(/* GraphQL */ `
extend schema
@link(url: "https://specs.apollo.dev/link/v1.0")
@link(url: "https://specs.apollo.dev/federation/v2.8", import: ["@external"])

type Position @external {
x: Int!
y: Int!
}
`);

const output = applyTagFilterToInaccessibleTransformOnSubgraphSchema(
sdl,
buildSchemaCoordinateTagRegister([sdl]),
filter,
);

expect(print(output.typeDefs)).toMatchInlineSnapshot(`
extend schema @link(url: "https://specs.apollo.dev/link/v1.0") @link(url: "https://specs.apollo.dev/federation/v2.8", import: ["@external"])

type Position @external {
x: Int!
y: Int!
}
`);
});
});

describe('interface type', () => {
Expand Down
11 changes: 5 additions & 6 deletions packages/services/schema/src/lib/federation-tag-extraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,6 @@ export function applyTagFilterToInaccessibleTransformOnSubgraphSchema(
fieldLikeNode: InputValueDefinitionNode | FieldDefinitionNode,
node: InputValueDefinitionNode,
) {
// Check for external tag because we cannot contribute directives to external fields.
if (node.directives?.find(d => d.name.value === externalDirectiveName)) {
return node;
}

const tagsOnNode = getTagsForSchemaCoordinate(
`${objectLikeNode.name.value}.${fieldLikeNode.name.value}(${node.name.value}:)`,
);
Expand Down Expand Up @@ -204,6 +199,7 @@ export function applyTagFilterToInaccessibleTransformOnSubgraphSchema(

for (const node of nodes) {
const tagsOnNode = getTagsForSchemaCoordinate(node.name.value);
const nodeIsExternal = !!node.directives?.find(d => d.name.value === externalDirectiveName);

let newNode = {
...node,
Expand All @@ -222,7 +218,10 @@ export function applyTagFilterToInaccessibleTransformOnSubgraphSchema(
}

// Check for external tag because we cannot contribute directives to external fields.
if (fieldNode.directives?.find(d => d.name.value === externalDirectiveName)) {
const fieldIsExternal = !!fieldNode.directives?.find(
d => d.name.value === externalDirectiveName,
);
if (nodeIsExternal || fieldIsExternal) {
return fieldNode;
}

Expand Down
Loading