forked from the-guild-org/graphql-federation-gateway-audit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc.subgraph.ts
65 lines (58 loc) · 1.71 KB
/
c.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
59
60
61
62
63
64
65
import { createSubgraph } from "../../subgraph.js";
import { products } from "./data.js";
export default createSubgraph("c", {
typeDefs: /* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key", "@external", "@requires"]
)
type Product @key(fields: "id") {
id: ID!
isExpensive: Boolean! @external
include: Boolean! @requires(fields: "isExpensive")
skip: Boolean! @requires(fields: "isExpensive")
neverCalledInclude: Boolean! @requires(fields: "isExpensive")
neverCalledSkip: Boolean! @requires(fields: "isExpensive")
}
`,
resolvers: {
Product: {
__resolveReference(
key: { id: string } | { id: string; isExpensive: boolean },
) {
const product = products.find((product) => product.id === key.id);
if (!product) {
return null;
}
if ("isExpensive" in key) {
return {
id: product.id,
isExpensive: key.isExpensive,
};
}
return {
id: product.id,
};
},
include(product: { isExpensive?: number }) {
if (typeof product.isExpensive !== "boolean") {
throw new Error("isExpensive is missing");
}
return true;
},
neverCalledInclude() {
throw new Error("neverCalledInclude should not be called");
},
skip(product: { isExpensive?: number }) {
if (typeof product.isExpensive !== "boolean") {
throw new Error("isExpensive is missing");
}
return true;
},
neverCalledSkip() {
throw new Error("neverCalledSkip should not be called");
},
},
},
});