forked from the-guild-org/graphql-federation-gateway-audit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
product.subgraph.ts
45 lines (40 loc) · 895 Bytes
/
product.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
import { createSubgraph } from "../../subgraph.js";
import { products } from "./data.js";
export default createSubgraph("product", {
typeDefs: /* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external"]
)
type Product @key(fields: "id") {
id: ID!
name: String!
}
type Query {
products: [Product!]!
}
`,
resolvers: {
Product: {
__resolveReference(key: { id: string }) {
const product = products.find((product) => product.id === key.id);
if (!product) {
return null;
}
return {
id: product.id,
name: product.name,
};
},
},
Query: {
products() {
return products.map((p) => ({
id: p.id,
name: p.name,
}));
},
},
},
});