forked from the-guild-org/graphql-federation-gateway-audit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.subgraph.ts
58 lines (50 loc) · 1.16 KB
/
types.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
56
57
58
import { createSubgraph } from "../../subgraph.js";
import { categories, products } from "./data.js";
export default createSubgraph("types", {
typeDefs: /* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@shareable"]
)
interface Node {
id: ID!
}
type Product implements Node @key(fields: "id") @shareable {
id: ID!
name: String!
price: Float!
}
type Category implements Node @key(fields: "id") {
id: ID!
name: String!
}
`,
resolvers: {
Product: {
__resolveReference(key: { id: string }) {
const product = products.find((p) => p.id === key.id);
if (!product) {
return null;
}
return {
id: product.id,
name: product.name,
price: product.price,
};
},
},
Category: {
__resolveReference(key: { id: string }) {
const cat = categories.find((c) => c.id === key.id);
if (!cat) {
return null;
}
return {
id: cat.id,
name: cat.name,
};
},
},
},
});