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

Validate directive argument values against the type they are declared with at every level, so a nested list such as `[[], ["ok"]]` for `[[String!]!]!` is accepted and a bad item anywhere in a list is rejected. Tag names written unquoted are now also recognised by schema contract filters, and `@link(url:)` and `@link(as:)` stay strict, since they are read before any of this runs.
5 changes: 5 additions & 0 deletions .changeset/gentle-moons-attend.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@theguild/federation-composition": patch
---

Accept two ways of writing directive argument values that were rejected before: an unquoted value where the argument is a `String` or `ID` (`@tag(name: Public)`), and a quoted value where the argument is an enum (`@example(level: "HIGH")`, if the enum has that value). Both are read as the value that was meant.
5 changes: 5 additions & 0 deletions .changeset/olive-pears-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@theguild/federation-composition": patch
---

Fix directive argument values that were wrongly rejected: an `ID` argument now accepts string and int literals, an explicit `null` is accepted for any nullable argument, and `null` is accepted as an item of a nullable list item type. A list value is no longer accepted for a non-list argument, and every item of a list is now checked instead of only the first one.
31 changes: 31 additions & 0 deletions __tests__/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,37 @@ testVersions((api, version) => {
return;
}

test("accepts an unquoted context name", () => {
const result = compose([
{
name: "accounts",
typeDefs: federationSchema(
version,
parse(/* GraphQL */ `
type Customer @key(fields: "id") @context(name: customerCtx) {
id: ID!
segment: String!
order: Order!
}

type Order @key(fields: "id") {
id: ID!
discount(
segment: String @fromContext(field: "$customerCtx { segment }")
): Int
}

type Query {
customer(id: ID!): Customer
}
`),
),
},
]);

assertCompositionSuccess(result);
});

describe("@context and @fromContext location", () => {
describe("the context name", () => {
test("rejects an empty context name", () => {
Expand Down
37 changes: 37 additions & 0 deletions __tests__/contracts/compose-schema-contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,43 @@ describe("composeSchemaContract", () => {
`);
});

test("excludes an unquoted tag name, as composition accepts it", () => {
const result = composeSchemaContract(
[
{
name: "products",
url: "https://products.example.com/graphql",
typeDefs: parse(/* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.5"
import: ["@key", "@tag"]
)

type Product @key(fields: "id") {
id: ID!
name: String
secret: String @tag(name: internal)
}

type Query {
product: Product
}
`),
},
],
{
include: new Set(),
exclude: new Set(["internal"]),
},
true,
);

expect(result.errors).toEqual(undefined);
expect((result as CompositionSuccess).publicSdl).toContain("name: String");
expect((result as CompositionSuccess).publicSdl).not.toContain("secret");
});

test("keeps types referenced by composed directives when removing unreachable types", () => {
const result = composeSchemaContract(
[
Expand Down
159 changes: 159 additions & 0 deletions __tests__/subgraph/errors/INVALID_GRAPHQL.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
assertCompositionFailure,
assertCompositionSuccess,
graphql,
satisfiesVersionRange,
testVersions,
} from "../../shared/testkit.js";

Expand Down Expand Up @@ -543,3 +544,161 @@ testVersions((api, version) => {
);
});
});

testVersions((api, version) => {
// `schema` goes after the federation `@link`, `link` inside it
const compose = (c: {
imports?: string;
link?: string;
schema?: string;
sdl: string;
}) =>
api.composeServices([
{
name: "users",
typeDefs: graphql`
extend schema
@link(
url: "https://specs.apollo.dev/federation/${version}"
import: ["@key"${c.imports ?? ""}]
${c.link ?? ""}
)
${c.schema ?? ""}

${c.sdl}

type Query {
users: [User!]!
}
`,
},
]);

const relationship = `
@link(url: "https://myspecs.dev/rel/v1.0", import: ["@rel"])
@composeDirective(name: "@rel")
`;

test.each([
{
name: "an unquoted value for a String argument, as a string",
imports: `, "@tag"`,
sdl: `directive @rel(kind: String, kinds: [String]) on FIELD_DEFINITION
type User @key(fields: "id") {
id: ID!
a: String @rel(kind: UserLink, kinds: [UserLink, "other"]) @tag(name: Public)
}`,
contains: `@tag(name: "Public")`,
},
{
name: "a quoted value for an enum argument, as an enum value",
imports: `, "@composeDirective"`,
schema: relationship,
sdl: `directive @rel(level: Level, levels: [Level]) on FIELD_DEFINITION
enum Level { HIGH LOW }
type User @key(fields: "id") {
id: ID!
a: String @rel(level: "HIGH", levels: ["HIGH", LOW])
}`,
contains: `@rel(level: HIGH, levels: [HIGH, LOW])`,
since: "v2.1" as const, // @composeDirective
},
{
name: "an unquoted value for an aliased directive",
imports: `, { name: "@tag", as: "@mytag" }`,
sdl: `type User @key(fields: "id") { id: ID! a: String @mytag(name: Public) }`,
contains: `(name: "Public")`, // the directive keeps its alias
},
{
name: "string and int literals for ID",
sdl: `directive @ref(id: ID, ids: [ID]) on FIELD_DEFINITION
type User @key(fields: "id") {
id: ID!
a: String @ref(id: "abc")
b: String @ref(id: 1)
c: String @ref(ids: ["abc", 1])
}`,
},
{
name: "null for a nullable argument",
sdl: `directive @opt(s: String, i: Int, list: [String]) on FIELD_DEFINITION
type User @key(fields: "id") {
id: ID!
a: String @opt(s: null, i: null, list: null)
b: String @opt(list: [null])
}`,
},
{
name: "a nested list checked at every level",
sdl: `directive @nested(v: [[String!]!]!) on FIELD_DEFINITION
type User @key(fields: "id") { id: ID! a: String @nested(v: [[], ["ok"]]) }`,
},
{
name: "a quoted value for @link(for:)",
link: `for: "EXECUTION"`,
sdl: `type User @key(fields: "id") { id: ID! }`,
},
])("INVALID_GRAPHQL - accepts $name", ({ contains, since, ...c }) => {
if (since && satisfiesVersionRange(`< ${since}`, version)) {
return;
}

const result = compose(c);

assertCompositionSuccess(result);

if (contains) {
expect(result.supergraphSdl).toContain(contains);
}
});

test.each([
{
name: "a quoted value that is not a value of the enum",
sdl: `directive @rel(level: Level) on FIELD_DEFINITION
enum Level { HIGH LOW }
type User @key(fields: "id") { id: ID! a: String @rel(level: "NOPE") }`,
error: `"@rel(level:)" of type "Level" in application of "@rel" to "User.a"`,
},
{
name: "a list value for a non-list argument",
sdl: `directive @one(s: String) on FIELD_DEFINITION
type User @key(fields: "id") { id: ID! a: String @one(s: ["txt"]) }`,
error: `"@one(s:)" of type "String" in application of "@one" to "User.a"`,
},
{
name: "null as an item of a non-null item type",
sdl: `directive @many(list: [String!]) on FIELD_DEFINITION
type User @key(fields: "id") { id: ID! a: String @many(list: [null]) }`,
error: `"@many(list:)" of type "[String!]" in application of "@many" to "User.a"`,
},
{
name: "a bad item of a nested list",
sdl: `directive @nested(v: [[String!]!]!) on FIELD_DEFINITION
type User @key(fields: "id") { id: ID! a: String @nested(v: [["ok", 42]]) }`,
error: `"@nested(v:)" of type "[[String!]!]!" in application of "@nested" to "User.a"`,
},
{
name: "an unquoted @link url",
schema: `@link(url: notAUrl)`,
sdl: `type User @key(fields: "id") { id: ID! }`,
error: `"@link(url:)" of type "String" in application of "@link" to "schema"`,
// Apollo throws `TypeError: Invalid URL` instead of reporting it
skipApollo: true,
},
])("INVALID_GRAPHQL - rejects $name", ({ error, skipApollo, ...c }) => {
if (skipApollo && api.library === "apollo") {
return;
}

const result = compose(c);

assertCompositionFailure(result);
expect(result.errors).toContainEqual(
expect.objectContaining({
message: `[users] Invalid value for ${error}.`,
extensions: expect.objectContaining({ code: "INVALID_GRAPHQL" }),
}),
);
});
});
6 changes: 4 additions & 2 deletions src/contracts/tag-extraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -690,12 +690,14 @@ export function createTagDirectiveNameExtractionStrategy(
directiveName: string,
): TagExtractionStrategy {
return (directiveNode: DirectiveNode) => {
const value = directiveNode.arguments?.[0]?.value;
if (
directiveNode.name.value === directiveName &&
directiveNode.arguments?.[0].name.value === "name" &&
directiveNode.arguments?.[0]?.value.kind === Kind.STRING
// an unquoted tag name is accepted by composition, so it is accepted here
(value?.kind === Kind.STRING || value?.kind === Kind.ENUM)
) {
return directiveNode.arguments[0].value.value ?? null;
return value.value ?? null;
}
return null;
};
Expand Down
Loading
Loading