Skip to content

Commit

Permalink
feat(apps/gql): add PanoPost.upvoteCount resolver (#584)
Browse files Browse the repository at this point in the history
Closes #514
  • Loading branch information
rasitds authored Aug 1, 2023
1 parent 5a2dada commit aa653e9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
14 changes: 13 additions & 1 deletion apps/gql/loaders/pano.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { createPrismaConnectionLoader, createPrismaLoader } from "@kampus/gql-utils";
import {
createPrismaConnectionLoader,
createPrismaCountLoader,
createPrismaLoader,
} from "@kampus/gql-utils";
import { type Connection } from "@kampus/gql-utils/connection";
import { type Comment, type Post } from "@kampus/prisma";

Expand All @@ -16,6 +20,7 @@ export const createPanoLoaders = (clients: Clients) => {
return {
post: createPanoPostLoaders(clients),
comment: createPanoCommentLoaders(clients),
upvote: createPanoUpvoteLoaders(clients),
};
};

Expand All @@ -41,6 +46,12 @@ const createPanoPostLoaders = ({ prisma }: Clients) => {
};
};

const createPanoUpvoteLoaders = ({ prisma }: Clients) => {
const countByPostID = createPrismaCountLoader(prisma.upvote, "postID");

return { countByPostID };
};

const createPanoCommentLoaders = ({ prisma }: Clients) => {
const cacheCommentConnection = (connections: Connection<Comment>[]) => {
connections.forEach((connection) => {
Expand Down Expand Up @@ -75,6 +86,7 @@ export const transformPanoPost = (post: Post) => {
__typename: "PanoPost",
owner: null,
comments: null,
upvoteCount: null,
createdAt: post.createdAt.toISOString(),
} satisfies PanoPost;
};
Expand Down
3 changes: 3 additions & 0 deletions apps/gql/schema/resolvers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ export const resolvers = {
await loaders.pano.comment.byPostID.load(new ConnectionKey(post.id, args))
);
},
upvoteCount: (post, _, { loaders }) => {
return loaders.pano.upvote.countByPostID.load(new ConnectionKey(post.id));
},
createdAt: (post) => post.createdAt,
},
PanoPostConnection: {
Expand Down
15 changes: 3 additions & 12 deletions apps/gql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,8 @@ type PanoPost implements Node {
content: String
createdAt: DateTime!
owner: User
comments(
after: String
before: String
first: Int
last: Int
): PanoCommentConnection
comments(after: String, before: String, first: Int, last: Int): PanoCommentConnection
upvoteCount: Int
}

type PanoPostConnection {
Expand All @@ -118,12 +114,7 @@ type PanoComment implements Node {
owner: User
post: PanoPost
parent: PanoComment
comments(
after: String
before: String
first: Int
last: Int
): PanoCommentConnection
comments(after: String, before: String, first: Int, last: Int): PanoCommentConnection
createdAt: DateTime!
}

Expand Down
2 changes: 2 additions & 0 deletions apps/gql/schema/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type PanoPost = Node & {
id: Scalars["ID"]["output"];
owner: Maybe<User>;
title: Scalars["String"]["output"];
upvoteCount: Maybe<Scalars["Int"]["output"]>;
url: Maybe<Scalars["String"]["output"]>;
};

Expand Down Expand Up @@ -435,6 +436,7 @@ export type PanoPostResolvers<
id: Resolver<ResolversTypes["ID"], ParentType, ContextType>;
owner: Resolver<Maybe<ResolversTypes["User"]>, ParentType, ContextType>;
title: Resolver<ResolversTypes["String"], ParentType, ContextType>;
upvoteCount: Resolver<Maybe<ResolversTypes["Int"]>, ParentType, ContextType>;
url: Resolver<Maybe<ResolversTypes["String"]>, ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;
Expand Down

0 comments on commit aa653e9

Please sign in to comment.