Skip to content

added new test: requires-with-argument-conflict #138

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ async function getTestCases(router: ReturnType<typeof createRouter>) {
import("./test-suites/abstract-types/index.js"),
import("./test-suites/fed1-external-extends-resolvable/index.js"),
import("./test-suites/requires-with-argument/index.js"),
import("./test-suites/requires-with-argument-conflict/index.js"),
import("./test-suites/keys-mashup/index.js"),
import("./test-suites/null-keys/index.js"),
].map((i) => i.then((e) => e.default))
Expand Down
83 changes: 83 additions & 0 deletions src/test-suites/requires-with-argument-conflict/a.subgraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { createSubgraph } from "../../subgraph.js";
import { products } from "./data.js";

export default createSubgraph("a", {
typeDefs: /* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external", "@requires"]
)

type Product @key(fields: "upc") {
upc: String!
weight: Int @external
price(currency: String!): Int @external
shippingEstimate: Int
@requires(
fields: """
price(currency: "USD") weight
"""
)
shippingEstimateEUR: Int
@requires(
fields: """
price(currency: "EUR") weight
"""
)
category: Category @external
isExpensiveCategory: Boolean
@requires(
fields: """
category { averagePrice(currency: "USD") }
"""
)
}

type Category @external {
averagePrice(currency: String!): Int
}
`,
resolvers: {
Product: {
__resolveReference(
key: { upc: string; price: number; weight: number } | { upc: string },
) {
const product = products.find((p) => p.upc === key.upc);

if (!product) {
return null;
}

if ("weight" in key && "price" in key) {
return {
upc: product.upc,
weight: key.weight,
price: key.price,
category: product.category,
};
}

return {
upc: product.upc,
category: product.category,
};
},
shippingEstimate(
product: { price: number; weight: number },
args: { currency?: string },
) {
const value = product.price * product.weight * 10;

if (args.currency === "EUR") {
return value * 1.5;
}

return value;
},
isExpensiveCategory(product: { category: { averagePrice: number } }) {
return product.category.averagePrice > 11;
},
},
},
});
55 changes: 55 additions & 0 deletions src/test-suites/requires-with-argument-conflict/b.subgraph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createSubgraph } from "../../subgraph.js";
import { products } from "./data.js";

export default createSubgraph("b", {
typeDefs: /* GraphQL */ `
extend schema
@link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])

type Query {
products: [Product]
}

type Product @key(fields: "upc") {
upc: String!
name: String
price(currency: String!): Int
weight: Int
category: Category
}

type Category {
averagePrice(currency: String!): Int
}
`,
resolvers: {
Query: {
products() {
return products.map((p) => ({
upc: p.upc,
name: p.name,
price: p.price,
weight: p.weight,
category: p.category,
}));
},
},
Product: {
__resolveReference(key: { upc: string }) {
const product = products.find((p) => p.upc === key.upc);

if (!product) {
return null;
}

return {
upc: product.upc,
name: product.name,
price: product.price,
weight: product.weight,
category: product.category,
};
},
},
},
});
20 changes: 20 additions & 0 deletions src/test-suites/requires-with-argument-conflict/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const products = [
{
upc: "p1",
name: "p-name-1",
price: 11,
weight: 1,
category: {
averagePrice: 11,
},
},
{
upc: "p2",
name: "p-name-2",
price: 22,
weight: 2,
category: {
averagePrice: 22,
},
},
];
6 changes: 6 additions & 0 deletions src/test-suites/requires-with-argument-conflict/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { serve } from "../../supergraph.js";
import a from "./a.subgraph.js";
import b from "./b.subgraph.js";
import test from "./test.js";

export default serve("requires-with-argument-conflict", [a, b], test);
37 changes: 37 additions & 0 deletions src/test-suites/requires-with-argument-conflict/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createTest } from "../../testkit.js";

export default [
createTest(
/* GraphQL */ `
query {
products {
upc
name
shippingEstimate
shippingEstimateEUR
isExpensiveCategory
}
}
`,
{
data: {
products: [
{
upc: "p1",
name: "p-name-1",
shippingEstimate: 110,
shippingEstimateEUR: 165,
isExpensiveCategory: false,
},
{
upc: "p2",
name: "p-name-2",
shippingEstimate: 440,
shippingEstimateEUR: 660,
isExpensiveCategory: true,
},
],
},
},
),
];
Loading