-
-
Notifications
You must be signed in to change notification settings - Fork 815
Expand file tree
/
Copy pathpost.ts
More file actions
34 lines (31 loc) · 905 Bytes
/
post.ts
File metadata and controls
34 lines (31 loc) · 905 Bytes
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
import { Comments, type IPost, Posts, Users } from "../../utils/data";
import { builder } from "../builder";
import { Comment } from "./comment";
import { User } from "./user";
export const Post = builder.objectRef<IPost>("Post");
Post.implement({
fields: (t) => ({
id: t.exposeID("id"),
title: t.exposeString("title"),
content: t.exposeString("content"),
author: t.field({
type: User,
nullable: true,
resolve: (post) => [...Users.values()].find((user) => user.id === post.authorId),
}),
comments: t.field({
type: [Comment],
resolve: (post) => [...Comments.values()].filter((comment) => comment.postId === post.id),
}),
}),
});
builder.queryFields((t) => ({
post: t.field({
type: Post,
nullable: true,
args: {
id: t.arg.id({ required: true }),
},
resolve: (_root, args) => Posts.get(String(args.id)),
}),
}));