Skip to content

Commit

Permalink
style-posts
Browse files Browse the repository at this point in the history
  • Loading branch information
benawad committed Aug 14, 2020
1 parent 236a4df commit e967790
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 10 deletions.
9 changes: 8 additions & 1 deletion server/src/resolvers/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
Ctx,
UseMiddleware,
Int,
FieldResolver,
Root,
} from "type-graphql";
import { Post } from "../entities/Post";
import { MyContext } from "../types";
Expand All @@ -22,8 +24,13 @@ class PostInput {
text: string;
}

@Resolver()
@Resolver(Post)
export class PostResolver {
@FieldResolver(() => String)
textSnippet(@Root() post: Post) {
return post.text.slice(0, 50);
}

@Query(() => [Post])
async posts(
@Arg("limit", () => Int) limit: number,
Expand Down
4 changes: 3 additions & 1 deletion web/src/generated/graphql.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type Post = {
creatorId: Scalars['Float'];
createdAt: Scalars['String'];
updatedAt: Scalars['String'];
textSnippet: Scalars['String'];
};

export type User = {
Expand Down Expand Up @@ -238,7 +239,7 @@ export type PostsQuery = (
{ __typename?: 'Query' }
& { posts: Array<(
{ __typename?: 'Post' }
& Pick<Post, 'id' | 'createdAt' | 'updatedAt' | 'title'>
& Pick<Post, 'id' | 'createdAt' | 'updatedAt' | 'title' | 'textSnippet'>
)> }
);

Expand Down Expand Up @@ -351,6 +352,7 @@ export const PostsDocument = gql`
createdAt
updatedAt
title
textSnippet
}
}
`;
Expand Down
1 change: 1 addition & 0 deletions web/src/graphql/queries/posts.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ query Posts($limit: Int!, $cursor: String) {
createdAt
updatedAt
title
textSnippet
}
}
37 changes: 29 additions & 8 deletions web/src/pages/index.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
import { NavBar } from "../components/NavBar";
import { withUrqlClient } from "next-urql";
import { createUrqlClient } from "../utils/createUrqlClient";
import { usePostsQuery } from "../generated/graphql";
import { Layout } from "../components/Layout";
import { Link } from "@chakra-ui/core";
import { Link, Stack, Box, Heading, Text, Flex, Button } from "@chakra-ui/core";
import NextLink from "next/link";

const Index = () => {
const [{ data }] = usePostsQuery({
const [{ data, fetching }] = usePostsQuery({
variables: {
limit: 10,
},
});

if (!fetching && !data) {
return <div>you got query failed for some reason</div>;
}

return (
<Layout>
<NextLink href="/create-post">
<Link>create post</Link>
</NextLink>
<Flex align="center">
<Heading>LiReddit</Heading>
<NextLink href="/create-post">
<Link ml="auto">create post</Link>
</NextLink>
</Flex>
<br />
{!data ? (
{!data && fetching ? (
<div>loading...</div>
) : (
data.posts.map((p) => <div key={p.id}>{p.title}</div>)
<Stack spacing={8}>
{data!.posts.map((p) => (
<Box key={p.id} p={5} shadow="md" borderWidth="1px">
<Heading fontSize="xl">{p.title}</Heading>
<Text mt={4}>{p.textSnippet}</Text>
</Box>
))}
</Stack>
)}
{data ? (
<Flex>
<Button isLoading={fetching} m="auto" my={8}>
load more
</Button>
</Flex>
) : null}
</Layout>
);
};
Expand Down

0 comments on commit e967790

Please sign in to comment.