Skip to content

Commit

Permalink
return author name only
Browse files Browse the repository at this point in the history
  • Loading branch information
sywhb committed Jun 18, 2024
1 parent b2d61ca commit 0f3d6f4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 17 deletions.
6 changes: 4 additions & 2 deletions packages/api/src/entity/post.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
PrimaryGeneratedColumn,
UpdateDateColumn,
} from 'typeorm'
import { User } from './user'

Expand Down Expand Up @@ -37,9 +39,9 @@ export class Post {
@Column('text', { nullable: true })
thought?: string | null

@Column('timestamptz')
@CreateDateColumn({ type: 'timestamptz' })
createdAt!: Date

@Column('timestamptz')
@UpdateDateColumn({ type: 'timestamptz' })
updatedAt!: Date
}
9 changes: 5 additions & 4 deletions packages/api/src/generated/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,7 @@ export type CreatePostError = {
};

export enum CreatePostErrorCode {
BadRequest = 'BAD_REQUEST',
Unauthorized = 'UNAUTHORIZED'
}

Expand Down Expand Up @@ -2407,12 +2408,12 @@ export type ParseResult = {

export type Post = {
__typename?: 'Post';
author: User;
author: Scalars['String'];
content: Scalars['String'];
createdAt: Scalars['Date'];
highlights?: Maybe<Array<Highlight>>;
id: Scalars['ID'];
libraryItems: Array<Article>;
libraryItems?: Maybe<Array<Article>>;
ownedByViewer: Scalars['Boolean'];
thought?: Maybe<Scalars['String']>;
thumbnail?: Maybe<Scalars['String']>;
Expand Down Expand Up @@ -7057,12 +7058,12 @@ export type PageInfoResolvers<ContextType = ResolverContext, ParentType extends
};

export type PostResolvers<ContextType = ResolverContext, ParentType extends ResolversParentTypes['Post'] = ResolversParentTypes['Post']> = {
author?: Resolver<ResolversTypes['User'], ParentType, ContextType>;
author?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
content?: Resolver<ResolversTypes['String'], ParentType, ContextType>;
createdAt?: Resolver<ResolversTypes['Date'], ParentType, ContextType>;
highlights?: Resolver<Maybe<Array<ResolversTypes['Highlight']>>, ParentType, ContextType>;
id?: Resolver<ResolversTypes['ID'], ParentType, ContextType>;
libraryItems?: Resolver<Array<ResolversTypes['Article']>, ParentType, ContextType>;
libraryItems?: Resolver<Maybe<Array<ResolversTypes['Article']>>, ParentType, ContextType>;
ownedByViewer?: Resolver<ResolversTypes['Boolean'], ParentType, ContextType>;
thought?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
thumbnail?: Resolver<Maybe<ResolversTypes['String']>, ParentType, ContextType>;
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/generated/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@ type CreatePostError {
}

enum CreatePostErrorCode {
BAD_REQUEST
UNAUTHORIZED
}

Expand Down Expand Up @@ -1841,12 +1842,12 @@ input ParseResult {
}

type Post {
author: User!
author: String!
content: String!
createdAt: Date!
highlights: [Highlight!]
id: ID!
libraryItems: [Article!]!
libraryItems: [Article!]
ownedByViewer: Boolean!
thought: String
thumbnail: String
Expand Down
20 changes: 13 additions & 7 deletions packages/api/src/resolvers/function_resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,26 +812,32 @@ export const functionResolvers = {
recommendedAt: (recommendation: Recommendation) => recommendation.createdAt,
},
Post: {
author(post: Post, _: never, ctx: ResolverContext) {
return ctx.dataLoaders.users.load(post.userId)
async author(post: Post, _: never, ctx: ResolverContext) {
const author = await ctx.dataLoaders.users.load(post.userId)
return author?.name
},
ownedByViewer(post: Post, _: never, ctx: ResolverContext) {
console.log('ownedByViewer: ctx.claims?.uid', ctx.claims?.uid)
return post.userId === ctx.claims?.uid
},
libraryItems(
async libraryItems(
post: { libraryItemIds: string[] },
_: never,
ctx: ResolverContext
) {
return ctx.dataLoaders.libraryItems.loadMany(post.libraryItemIds)
const items = await ctx.dataLoaders.libraryItems.loadMany(
post.libraryItemIds
)
return items.filter((item) => !!item)
},
highlights(
async highlights(
post: { highlightIds: string[] },
_: never,
ctx: ResolverContext
) {
return ctx.dataLoaders.highlights.loadMany(post.highlightIds)
const highlights = await ctx.dataLoaders.highlights.loadMany(
post.highlightIds
)
return highlights.filter((highlight) => !!highlight)
},
},
...resultResolveTypeResolver('Login'),
Expand Down
8 changes: 8 additions & 0 deletions packages/api/src/resolvers/posts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ export const createPostResolver = authorized<
const { title, content, highlightIds, libraryItemIds, thought, thumbnail } =
input

if (libraryItemIds.length === 0) {
log.error('Invalid args', { libraryItemIds })

return {
errorCodes: [CreatePostErrorCode.BadRequest],
}
}

const postToCreate = {
userId: uid,
title,
Expand Down
5 changes: 3 additions & 2 deletions packages/api/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3342,11 +3342,11 @@ const schema = gql`
id: ID!
title: String!
content: String!
author: User!
author: String!
ownedByViewer: Boolean!
thumbnail: String
thought: String
libraryItems: [Article!]!
libraryItems: [Article!]
highlights: [Highlight!]
createdAt: Date!
updatedAt: Date!
Expand All @@ -3373,6 +3373,7 @@ const schema = gql`
enum CreatePostErrorCode {
UNAUTHORIZED
BAD_REQUEST
}
input UpdatePostInput {
Expand Down

0 comments on commit 0f3d6f4

Please sign in to comment.