Skip to content

Commit

Permalink
post-pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 13, 2020
1 parent f5c2c4f commit 81de2e8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
22 changes: 20 additions & 2 deletions server/src/resolvers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import {
Field,
Ctx,
UseMiddleware,
Int,
} from "type-graphql";
import { Post } from "../entities/Post";
import { MyContext } from "../types";
import { isAuth } from "../middleware/isAuth";
import { getConnection } from "typeorm";

@InputType()
class PostInput {
Expand All @@ -23,8 +25,24 @@ class PostInput {
@Resolver()
export class PostResolver {
@Query(() => [Post])
async posts(): Promise<Post[]> {
return Post.find();
async posts(
@Arg("limit", () => Int) limit: number,
@Arg("cursor", () => String, { nullable: true }) cursor: string | null
): Promise<Post[]> {
const realLimit = Math.min(50, limit);
const qb = getConnection()
.getRepository(Post)
.createQueryBuilder("p")
.orderBy('"createdAt"', "DESC")
.take(realLimit);

if (cursor) {
qb.where('"createdAt" < :cursor', {
cursor: new Date(parseInt(cursor)),
});
}

return qb.getMany();
}

@Query(() => Post, { nullable: true })
Expand Down
15 changes: 12 additions & 3 deletions web/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export type Query = {
};


export type QueryPostsArgs = {
cursor?: Maybe<Scalars['String']>;
limit: Scalars['Int'];
};


export type QueryPostArgs = {
id: Scalars['Float'];
};
Expand Down Expand Up @@ -222,7 +228,10 @@ export type MeQuery = (
)> }
);

export type PostsQueryVariables = Exact<{ [key: string]: never; }>;
export type PostsQueryVariables = Exact<{
limit: Scalars['Int'];
cursor?: Maybe<Scalars['String']>;
}>;


export type PostsQuery = (
Expand Down Expand Up @@ -336,8 +345,8 @@ export function useMeQuery(options: Omit<Urql.UseQueryArgs<MeQueryVariables>, 'q
return Urql.useQuery<MeQuery>({ query: MeDocument, ...options });
};
export const PostsDocument = gql`
query Posts {
posts {
query Posts($limit: Int!, $cursor: String) {
posts(cursor: $cursor, limit: $limit) {
id
createdAt
updatedAt
Expand Down
4 changes: 2 additions & 2 deletions web/src/graphql/queries/posts.graphql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
query Posts {
posts {
query Posts($limit: Int!, $cursor: String) {
posts(cursor: $cursor, limit: $limit) {
id
createdAt
updatedAt
Expand Down
6 changes: 5 additions & 1 deletion web/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import { Link } from "@chakra-ui/core";
import NextLink from "next/link";

const Index = () => {
const [{ data }] = usePostsQuery();
const [{ data }] = usePostsQuery({
variables: {
limit: 10,
},
});
return (
<Layout>
<NextLink href="/create-post">
Expand Down

0 comments on commit 81de2e8

Please sign in to comment.