-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathc.subgraph.ts
49 lines (46 loc) · 1.53 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
import { createSubgraph } from "../../subgraph.js";
import { books } from "./data.js";
export default createSubgraph("c", {
typeDefs: /* GraphQL */ `
extend schema
@link(
url: "https://specs.apollo.dev/federation/v2.3"
import: ["@key"]
)
type Book @key(fields: "id") {
id: ID!
author: Author
}
type Author {
id: ID!
name: String
}
`,
resolvers: {
Book: {
__resolveReference(reference: { id: String; } | { upc: String; }) {
if (reference != null) {
let book: { id: string; author: { id: string; name: string; } } | undefined;
if ('id' in reference && reference.id !== null) {
book = books.find((book) => book.id === reference.id);
}
if ('upc' in reference && reference.upc !== null) {
book = books.find((book) => book.upc === reference.upc);
}
if (book != null) {
return {
__typename: "Book",
id: book.id,
author: {
__typename: "Author",
id: book.author.id,
name: book.author.name
}
};
}
}
throw new Error("Invalid reference");
}
}
},
});