Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/thin-pandas-fetch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@theguild/federation-composition": patch
---

Fix in containsSupergraphSpec to treat directive definition as actual definition
24 changes: 22 additions & 2 deletions __tests__/subgraph-validation-and-compositon-root-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { describe, expect, test } from "vitest";
import { assertCompositionSuccess, composeServices } from "../src/compose.js";
import { ServiceDefinition } from "../src/types.js";
import { validate } from "../src/validate.js";
import { containsSupergraphSpec } from "../src/graphql/contains-supergraph-spec.js";

const serviceA: ServiceDefinition = {
name: "serviceA",
Expand Down Expand Up @@ -51,8 +52,7 @@ describe("Test to validate custom root query is treated as Query", () => {
const hasCustomRootQueryAsObjectType = supergraph.some(
(def) =>
def.kind === Kind.OBJECT_TYPE_DEFINITION &&
(def.name.value === "RootQuery" ||
def.name.value === "CustomQueryName"),
(def.name.value === "RootQuery" || def.name.value === "CustomQueryName")
);

expect(hasCustomRootQueryAsObjectType).eq(false);
Expand All @@ -76,3 +76,23 @@ describe("Test to validate custom root query is treated as Query", () => {
}
});
});

describe("Test to validate monolith schema is not treated as supergraph spec", () => {
test("Shouldn't treat properties with spec directive names as supergraph spec", () => {
const isSupergraph = containsSupergraphSpec(`#graphql
schema {
query: RootQueryType
}

type Link {
link: String
}

type RootQueryType {
foo: Link
}
`);

expect(isSupergraph).eq(false);
});
});
2 changes: 1 addition & 1 deletion src/graphql/contains-supergraph-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export function containsSupergraphSpec(sdl: string): boolean {
const patterns: string[] = [];

for (const { name, kind } of getSupergraphSpecNodes()) {
if (kind === Kind.DIRECTIVE) {
if (kind === Kind.DIRECTIVE || kind === Kind.DIRECTIVE_DEFINITION) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The getSupergraphSpecNodes function returns nodes from parsed SDLs. For directives, these will be DirectiveDefinitionNode with kind equal to Kind.DIRECTIVE_DEFINITION. The kind will not be Kind.DIRECTIVE. Therefore, the check for Kind.DIRECTIVE is redundant and can be removed for simplicity.

Suggested change
if (kind === Kind.DIRECTIVE || kind === Kind.DIRECTIVE_DEFINITION) {
if (kind === Kind.DIRECTIVE_DEFINITION) {

// "@NAME" for directives
patterns.push(`@${name}`);
} else {
Expand Down