Skip to content

Commit

Permalink
feat(apps/gql): add User.panoPosts resolver & fix pageInfo/cursor thi…
Browse files Browse the repository at this point in the history
…ngs (#565)

This implements the panoPosts resolver for user as a connection type so
it's already ready for pagination.

Also fixed #564 (it was stringifying instead of parsing)
  • Loading branch information
usirin authored Jul 29, 2023
1 parent 0f05265 commit e66e303
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
1 change: 1 addition & 0 deletions apps/gql/loaders/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ export const transformUser = (user: User) => ({
...user,
__typename: "User" as const,
username: user.username ?? "",
panoPosts: null,
});
36 changes: 20 additions & 16 deletions apps/gql/schema/resolvers/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { DateResolver, DateTimeResolver } from "graphql-scalars";

import { ConnectionKey } from "@kampus/gql-utils";
import { type ConnectionArguments } from "@kampus/gql-utils/connection";
import { type ConnectionArguments, type PageInfo } from "@kampus/gql-utils/connection";
import { parse, stringify } from "@kampus/gql-utils/global-id";
import { type User } from "@kampus/prisma";
import { assertNever } from "@kampus/std";
Expand All @@ -14,10 +14,16 @@ import { type Resolvers, type ResolversInterfaceTypes } from "../types.generated

type NodeTypename = ResolversInterfaceTypes<Dictionary>["Node"]["__typename"];

const transformConnectionArgs = (type: NodeTypename, args: ConnectionArguments) => ({
const transformPageInfo = (type: NodeTypename, pageInfo: PageInfo) => ({
...pageInfo,
startCursor: stringify(type, pageInfo.startCursor ?? ""),
endCursor: stringify(type, pageInfo.endCursor ?? ""),
});

const parseConnectionArgs = (args: ConnectionArguments) => ({
...args,
after: args.after ? stringify(type, args.after) : null,
before: args.before ? stringify(type, args.before) : null,
after: args.after ? parse(args.after).value : null,
before: args.before ? parse(args.before).value : null,
});

export const resolvers = {
Expand Down Expand Up @@ -70,7 +76,9 @@ export const resolvers = {
term: async (_, args, { loaders }) =>
transformSozlukTerm(await loaders.sozluk.term.load(args.id)),
terms: async (_, args, { loaders }) => {
return transformSozlukTermsConnection(await loaders.sozluk.terms.load(args));
return transformSozlukTermsConnection(
await loaders.sozluk.terms.load(parseConnectionArgs(args))
);
},
},
SozlukTerm: {
Expand All @@ -86,11 +94,7 @@ export const resolvers = {
},
SozlukTermConnection: {
edges: (connection) => connection.edges,
pageInfo: (connection) => ({
...connection.pageInfo,
startCursor: stringify("SozlukTerm", connection.pageInfo.startCursor ?? ""),
endCursor: stringify("SozlukTerm", connection.pageInfo.endCursor ?? ""),
}),
pageInfo: (connection) => transformPageInfo("SozlukTerm", connection.pageInfo),
totalCount: (connection) => connection.totalCount,
},
SozlukTermEdge: {
Expand All @@ -109,7 +113,7 @@ export const resolvers = {
},
allPosts: async (_, args, { loaders }) => {
const posts = await loaders.pano.post.all.load(
new ConnectionKey(null, transformConnectionArgs("PanoPost", args))
new ConnectionKey(null, parseConnectionArgs(args))
);

return transformPanoPostConnection(posts);
Expand All @@ -130,11 +134,7 @@ export const resolvers = {
PanoPostConnection: {
nodes: (connection) => connection.nodes,
edges: (connection) => connection.edges,
pageInfo: (connection) => ({
...connection.pageInfo,
startCursor: stringify("PanoPost", connection.pageInfo.startCursor ?? ""),
endCursor: stringify("PanoPost", connection.pageInfo.endCursor ?? ""),
}),
pageInfo: (connection) => transformPageInfo("PanoPost", connection.pageInfo),
totalCount: (connection) => connection.totalCount,
},
PanoPostEdge: {
Expand All @@ -150,5 +150,9 @@ export const resolvers = {
User: {
id: (user) => stringify("User", user.id),
username: (u) => u.username,
panoPosts: async (user, args, { loaders }) =>
transformPanoPostConnection(
await loaders.pano.post.byUserID.load(new ConnectionKey(user.id, args))
),
},
} satisfies Resolvers;
8 changes: 8 additions & 0 deletions apps/gql/schema/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ type PageInfo {
type User implements Node {
id: ID!
username: String!

panoPosts(
after: String
before: String
first: Int
last: Int
filter: PanoPostFilter
): PanoPostConnection
}

type SozlukQuery {
Expand Down
15 changes: 15 additions & 0 deletions apps/gql/schema/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,18 @@ export type SozlukTermEdge = {
export type User = Node & {
__typename?: "User";
id: Scalars["ID"]["output"];
panoPosts: Maybe<PanoPostConnection>;
username: Scalars["String"]["output"];
};

export type UserPanoPostsArgs = {
after: InputMaybe<Scalars["String"]["input"]>;
before: InputMaybe<Scalars["String"]["input"]>;
filter: InputMaybe<PanoPostFilter>;
first: InputMaybe<Scalars["Int"]["input"]>;
last: InputMaybe<Scalars["Int"]["input"]>;
};

export type WithIndex<TObject> = TObject & Record<string, any>;
export type ResolversObject<TObject> = WithIndex<TObject>;

Expand Down Expand Up @@ -458,6 +467,12 @@ export type UserResolvers<
ParentType extends ResolversParentTypes["User"] = ResolversParentTypes["User"]
> = ResolversObject<{
id: Resolver<ResolversTypes["ID"], ParentType, ContextType>;
panoPosts: Resolver<
Maybe<ResolversTypes["PanoPostConnection"]>,
ParentType,
ContextType,
Partial<UserPanoPostsArgs>
>;
username: Resolver<ResolversTypes["String"], ParentType, ContextType>;
__isTypeOf?: IsTypeOfResolverFn<ParentType, ContextType>;
}>;
Expand Down

0 comments on commit e66e303

Please sign in to comment.