Skip to content

Commit 02b053b

Browse files
committed
requires-with-argument-conflict
1 parent cc9d652 commit 02b053b

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { createSubgraph } from "../../subgraph.js";
2+
import { products } from "./data.js";
3+
4+
export default createSubgraph("a", {
5+
typeDefs: /* GraphQL */ `
6+
extend schema
7+
@link(
8+
url: "https://specs.apollo.dev/federation/v2.3"
9+
import: ["@key", "@external", "@requires"]
10+
)
11+
12+
type Product @key(fields: "upc") {
13+
upc: String!
14+
weight: Int @external
15+
price(currency: String!): Int @external
16+
shippingEstimate: Int
17+
@requires(
18+
fields: """
19+
price(currency: "USD") weight
20+
"""
21+
)
22+
shippingEstimateEUR: Int
23+
@requires(
24+
fields: """
25+
price(currency: "EUR") weight
26+
"""
27+
)
28+
category: Category @external
29+
isExpensiveCategory: Boolean
30+
@requires(
31+
fields: """
32+
category { averagePrice(currency: "USD") }
33+
"""
34+
)
35+
}
36+
37+
type Category @external {
38+
averagePrice(currency: String!): Int
39+
}
40+
`,
41+
resolvers: {
42+
Product: {
43+
__resolveReference(
44+
key: { upc: string; price: number; weight: number } | { upc: string },
45+
) {
46+
const product = products.find((p) => p.upc === key.upc);
47+
48+
if (!product) {
49+
return null;
50+
}
51+
52+
if ("weight" in key && "price" in key) {
53+
return {
54+
upc: product.upc,
55+
weight: key.weight,
56+
price: key.price,
57+
category: product.category,
58+
};
59+
}
60+
61+
return {
62+
upc: product.upc,
63+
category: product.category,
64+
};
65+
},
66+
shippingEstimate(
67+
product: { price: number; weight: number },
68+
args: { currency?: string },
69+
) {
70+
const value = product.price * product.weight * 10;
71+
72+
if (args.currency === "EUR") {
73+
return value * 1.5;
74+
}
75+
76+
return value;
77+
},
78+
isExpensiveCategory(product: { category: { averagePrice: number } }) {
79+
return product.category.averagePrice > 11;
80+
},
81+
},
82+
},
83+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { createSubgraph } from "../../subgraph.js";
2+
import { products } from "./data.js";
3+
4+
export default createSubgraph("b", {
5+
typeDefs: /* GraphQL */ `
6+
extend schema
7+
@link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key"])
8+
9+
type Query {
10+
products: [Product]
11+
}
12+
13+
type Product @key(fields: "upc") {
14+
upc: String!
15+
name: String
16+
price(currency: String!): Int
17+
weight: Int
18+
category: Category
19+
}
20+
21+
type Category {
22+
averagePrice(currency: String!): Int
23+
}
24+
`,
25+
resolvers: {
26+
Query: {
27+
products() {
28+
return products.map((p) => ({
29+
upc: p.upc,
30+
name: p.name,
31+
price: p.price,
32+
weight: p.weight,
33+
category: p.category,
34+
}));
35+
},
36+
},
37+
Product: {
38+
__resolveReference(key: { upc: string }) {
39+
const product = products.find((p) => p.upc === key.upc);
40+
41+
if (!product) {
42+
return null;
43+
}
44+
45+
return {
46+
upc: product.upc,
47+
name: product.name,
48+
price: product.price,
49+
weight: product.weight,
50+
category: product.category,
51+
};
52+
},
53+
},
54+
},
55+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export const products = [
2+
{
3+
upc: "p1",
4+
name: "p-name-1",
5+
price: 11,
6+
weight: 1,
7+
category: {
8+
averagePrice: 11,
9+
},
10+
},
11+
{
12+
upc: "p2",
13+
name: "p-name-2",
14+
price: 22,
15+
weight: 2,
16+
category: {
17+
averagePrice: 22,
18+
},
19+
},
20+
];
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { serve } from "../../supergraph.js";
2+
import a from "./a.subgraph.js";
3+
import b from "./b.subgraph.js";
4+
import test from "./test.js";
5+
6+
export default serve("requires-with-argument-conflict", [a, b], test);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { createTest } from "../../testkit.js";
2+
3+
export default [
4+
createTest(
5+
/* GraphQL */ `
6+
query {
7+
products {
8+
upc
9+
name
10+
shippingEstimate
11+
shippingEstimateEUR
12+
isExpensiveCategory
13+
}
14+
}
15+
`,
16+
{
17+
data: {
18+
products: [
19+
{
20+
upc: "p1",
21+
name: "p-name-1",
22+
shippingEstimate: 110,
23+
shippingEstimateEUR: 165,
24+
isExpensiveCategory: false,
25+
},
26+
{
27+
upc: "p2",
28+
name: "p-name-2",
29+
shippingEstimate: 440,
30+
shippingEstimateEUR: 660,
31+
isExpensiveCategory: true,
32+
},
33+
],
34+
},
35+
},
36+
),
37+
];

0 commit comments

Comments
 (0)