forked from the-guild-org/graphql-federation-gateway-audit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
price.subgraph.ts
55 lines (48 loc) · 1.22 KB
/
price.subgraph.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { createSubgraph } from "../../subgraph.js";
import { products } from "./data.js";
export default createSubgraph("price", {
typeDefs: /* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external"]
)
# Composition will fail if it's not an extension (I have no idea why this is the case)
extend type Product @key(fields: "id") {
id: ID! @external
price: Float
}
type Query {
cheapestProduct: Product
}
`,
resolvers: {
Product: {
__resolveReference(key: { id: string }) {
const product = products.find((product) => product.id === key.id);
if (!product) {
return null;
}
return {
id: product.id,
price: product.price,
};
},
},
Query: {
cheapestProduct() {
let cheapest: (typeof products)[number] | null = null;
for (const product of products) {
if (cheapest) {
if (product.price < cheapest.price) {
cheapest = product;
}
} else {
cheapest = product;
}
}
return cheapest;
},
},
},
});