forked from the-guild-org/graphql-federation-gateway-audit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
products.subgraph.ts
48 lines (43 loc) · 1013 Bytes
/
products.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
import { createSubgraph } from "../../subgraph.js";
import { products } from "./data.js";
export default createSubgraph("products", {
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: Int
weight: Int
}
`,
resolvers: {
Query: {
products() {
return products.map((p) => ({
upc: p.upc,
name: p.name,
price: p.price,
weight: p.weight,
}));
},
},
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,
};
},
},
},
});