Lucas/t 364 on comment drawer i can fetch all comment threads with (#193)

* Added prisma to suggested extension in container

* Added comments and authors on drawer with proper resolving

* Fix lint

* Fix console log

* Fixed generated front graphql from rebase
This commit is contained in:
Lucas Bordeau 2023-06-05 15:23:29 +02:00 committed by GitHub
parent 6de90024ef
commit fe70f30a29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 208 additions and 12 deletions

View File

@ -2,6 +2,7 @@
"recommendations": [
"ms-vscode-remote.remote-containers",
"ms-vscode.makefile-tools",
"dbaeumer.vscode-eslint"
"dbaeumer.vscode-eslint",
"Prisma.prisma"
]
}

View File

@ -101,11 +101,28 @@ export type CommentThreadCreateNestedOneWithoutCommentsInput = {
connect?: InputMaybe<CommentThreadWhereUniqueInput>;
};
export type CommentThreadOrderByWithRelationInput = {
commentThreadTargets?: InputMaybe<CommentThreadTargetOrderByRelationAggregateInput>;
comments?: InputMaybe<CommentOrderByRelationAggregateInput>;
createdAt?: InputMaybe<SortOrder>;
deletedAt?: InputMaybe<SortOrder>;
id?: InputMaybe<SortOrder>;
updatedAt?: InputMaybe<SortOrder>;
};
export type CommentThreadRelationFilter = {
is?: InputMaybe<CommentThreadWhereInput>;
isNot?: InputMaybe<CommentThreadWhereInput>;
};
export enum CommentThreadScalarFieldEnum {
CreatedAt = 'createdAt',
DeletedAt = 'deletedAt',
Id = 'id',
UpdatedAt = 'updatedAt',
WorkspaceId = 'workspaceId'
}
export type CommentThreadTarget = {
__typename?: 'CommentThreadTarget';
commentThread: CommentThread;
@ -142,6 +159,10 @@ export type CommentThreadTargetListRelationFilter = {
some?: InputMaybe<CommentThreadTargetWhereInput>;
};
export type CommentThreadTargetOrderByRelationAggregateInput = {
_count?: InputMaybe<SortOrder>;
};
export type CommentThreadTargetWhereInput = {
AND?: InputMaybe<Array<CommentThreadTargetWhereInput>>;
NOT?: InputMaybe<Array<CommentThreadTargetWhereInput>>;
@ -808,6 +829,7 @@ export type PipelineWhereUniqueInput = {
export type Query = {
__typename?: 'Query';
findManyCommentThreads: Array<CommentThread>;
findManyCompany: Array<Company>;
findManyPerson: Array<Person>;
findManyPipeline: Array<Pipeline>;
@ -816,6 +838,16 @@ export type Query = {
};
export type QueryFindManyCommentThreadsArgs = {
cursor?: InputMaybe<CommentThreadWhereUniqueInput>;
distinct?: InputMaybe<Array<CommentThreadScalarFieldEnum>>;
orderBy?: InputMaybe<Array<CommentThreadOrderByWithRelationInput>>;
skip?: InputMaybe<Scalars['Int']>;
take?: InputMaybe<Scalars['Int']>;
where?: InputMaybe<CommentThreadWhereInput>;
};
export type QueryFindManyCompanyArgs = {
cursor?: InputMaybe<CompanyWhereUniqueInput>;
distinct?: InputMaybe<Array<CompanyScalarFieldEnum>>;
@ -1063,6 +1095,13 @@ export type GetPeopleCountsQueryVariables = Exact<{
export type GetPeopleCountsQuery = { __typename?: 'Query', people: Array<{ __typename?: 'Person', commentsCount: number }> };
export type GetCommentThreadsByTargetsQueryVariables = Exact<{
commentThreadTargetIds: Array<Scalars['String']> | Scalars['String'];
}>;
export type GetCommentThreadsByTargetsQuery = { __typename?: 'Query', findManyCommentThreads: Array<{ __typename?: 'CommentThread', id: string, comments?: Array<{ __typename?: 'Comment', id: string, body: string, createdAt: any, updatedAt: any, author: { __typename?: 'User', id: string, displayName: string, avatarUrl?: string | null } }> | null }> };
export type GetCompaniesQueryVariables = Exact<{
orderBy?: InputMaybe<Array<CompanyOrderByWithRelationInput> | CompanyOrderByWithRelationInput>;
where?: InputMaybe<CompanyWhereInput>;
@ -1258,6 +1297,54 @@ export function useGetPeopleCountsLazyQuery(baseOptions?: Apollo.LazyQueryHookOp
export type GetPeopleCountsQueryHookResult = ReturnType<typeof useGetPeopleCountsQuery>;
export type GetPeopleCountsLazyQueryHookResult = ReturnType<typeof useGetPeopleCountsLazyQuery>;
export type GetPeopleCountsQueryResult = Apollo.QueryResult<GetPeopleCountsQuery, GetPeopleCountsQueryVariables>;
export const GetCommentThreadsByTargetsDocument = gql`
query GetCommentThreadsByTargets($commentThreadTargetIds: [String!]!) {
findManyCommentThreads(
where: {commentThreadTargets: {some: {commentableId: {in: $commentThreadTargetIds}}}}
) {
id
comments {
id
body
createdAt
updatedAt
author {
id
displayName
avatarUrl
}
}
}
}
`;
/**
* __useGetCommentThreadsByTargetsQuery__
*
* To run a query within a React component, call `useGetCommentThreadsByTargetsQuery` and pass it any options that fit your needs.
* When your component renders, `useGetCommentThreadsByTargetsQuery` returns an object from Apollo Client that contains loading, error, and data properties
* you can use to render your UI.
*
* @param baseOptions options that will be passed into the query, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options;
*
* @example
* const { data, loading, error } = useGetCommentThreadsByTargetsQuery({
* variables: {
* commentThreadTargetIds: // value for 'commentThreadTargetIds'
* },
* });
*/
export function useGetCommentThreadsByTargetsQuery(baseOptions: Apollo.QueryHookOptions<GetCommentThreadsByTargetsQuery, GetCommentThreadsByTargetsQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useQuery<GetCommentThreadsByTargetsQuery, GetCommentThreadsByTargetsQueryVariables>(GetCommentThreadsByTargetsDocument, options);
}
export function useGetCommentThreadsByTargetsLazyQuery(baseOptions?: Apollo.LazyQueryHookOptions<GetCommentThreadsByTargetsQuery, GetCommentThreadsByTargetsQueryVariables>) {
const options = {...defaultOptions, ...baseOptions}
return Apollo.useLazyQuery<GetCommentThreadsByTargetsQuery, GetCommentThreadsByTargetsQueryVariables>(GetCommentThreadsByTargetsDocument, options);
}
export type GetCommentThreadsByTargetsQueryHookResult = ReturnType<typeof useGetCommentThreadsByTargetsQuery>;
export type GetCommentThreadsByTargetsLazyQueryHookResult = ReturnType<typeof useGetCommentThreadsByTargetsLazyQuery>;
export type GetCommentThreadsByTargetsQueryResult = Apollo.QueryResult<GetCommentThreadsByTargetsQuery, GetCommentThreadsByTargetsQueryVariables>;
export const GetCompaniesDocument = gql`
query GetCompanies($orderBy: [CompanyOrderByWithRelationInput!], $where: CompanyWhereInput) {
companies: findManyCompany(orderBy: $orderBy, where: $where) {

View File

@ -0,0 +1,27 @@
import { CommentThreadForDrawer } from '@/comments/types/CommentThreadForDrawer';
import { CommentTextInput } from './CommentTextInput';
type OwnProps = {
commentThread: CommentThreadForDrawer;
};
export function CommentThread({ commentThread }: OwnProps) {
function handleSendComment(text: string) {
console.log(text);
}
return (
<div>
{commentThread.comments?.map((comment) => (
<div key={comment.id}>
<div>
{comment.author?.displayName} - {comment.createdAt}
</div>
<div>{comment.body}</div>
</div>
))}
<CommentTextInput onSend={handleSendComment} />
</div>
);
}

View File

@ -1,30 +1,36 @@
import { useRecoilState } from 'recoil';
import { CommentThreadForDrawer } from '@/comments/types/CommentThreadForDrawer';
import { RightDrawerBody } from '@/ui/layout/right-drawer/components/RightDrawerBody';
import { RightDrawerPage } from '@/ui/layout/right-drawer/components/RightDrawerPage';
import { RightDrawerTopBar } from '@/ui/layout/right-drawer/components/RightDrawerTopBar';
import { useGetCommentThreadsByTargetsQuery } from '~/generated/graphql';
import { commentableEntityArrayState } from '../../states/commentableEntityArrayState';
import { CommentTextInput } from './CommentTextInput';
import { CommentThread } from './CommentThread';
export function RightDrawerComments() {
const [commentableEntityArray] = useRecoilState(commentableEntityArrayState);
function handleSendComment(text: string) {
console.log(text);
}
const { data: queryResult } = useGetCommentThreadsByTargetsQuery({
variables: {
commentThreadTargetIds: commentableEntityArray.map(
(commentableEntity) => commentableEntity.id,
),
},
});
const commentThreads: CommentThreadForDrawer[] =
queryResult?.findManyCommentThreads ?? [];
return (
<RightDrawerPage>
<RightDrawerTopBar title="Comments" />
<RightDrawerBody>
{commentableEntityArray.map((commentableEntity) => (
<div key={commentableEntity.id}>
{commentableEntity.type} - {commentableEntity.id}
</div>
{commentThreads.map((commentThread) => (
<CommentThread commentThread={commentThread} />
))}
<CommentTextInput onSend={handleSendComment} />
</RightDrawerBody>
</RightDrawerPage>
);

View File

@ -34,3 +34,28 @@ export const usePeopleCommentsCountQuery = (personId: string) => {
});
return { ...rest, data: data?.people[0].commentsCount };
};
export const GET_COMMENT_THREADS_BY_TARGETS = gql`
query GetCommentThreadsByTargets($commentThreadTargetIds: [String!]!) {
findManyCommentThreads(
where: {
commentThreadTargets: {
some: { commentableId: { in: $commentThreadTargetIds } }
}
}
) {
id
comments {
id
body
createdAt
updatedAt
author {
id
displayName
avatarUrl
}
}
}
}
`;

View File

@ -0,0 +1,4 @@
import { GetCommentThreadsByTargetsQuery } from '~/generated/graphql';
export type CommentThreadForDrawer =
GetCommentThreadsByTargetsQuery['findManyCommentThreads'][0];

View File

@ -23,6 +23,7 @@ import { CommentThreadRelationsResolver } from './resolvers/relations/comment-th
import { PipelineRelationsResolver } from './resolvers/relations/pipeline-relations.resolver';
import { PipelineStageRelationsResolver } from './resolvers/relations/pipeline-stage-relations.resolver';
import { GraphQLError } from 'graphql';
import { CommentRelationsResolver } from './resolvers/relations/comment-relations.resolver';
@Module({
imports: [
@ -51,6 +52,7 @@ import { GraphQLError } from 'graphql';
PipelineStageResolver,
CompanyRelationsResolver,
CommentRelationsResolver,
PersonRelationsResolver,
UserRelationsResolver,
WorkspaceMemberRelationsResolver,

View File

@ -1,4 +1,4 @@
import { Resolver, Args, Mutation } from '@nestjs/graphql';
import { Resolver, Args, Mutation, Query } from '@nestjs/graphql';
import { UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from 'src/auth/guards/jwt.auth.guard';
import { PrismaService } from 'src/database/prisma.service';
@ -7,11 +7,16 @@ import { AuthWorkspace } from './decorators/auth-workspace.decorator';
import { CommentThread } from '../@generated/comment-thread/comment-thread.model';
import { CreateOneCommentThreadArgs } from '../@generated/comment-thread/create-one-comment-thread.args';
import { CreateOneCommentThreadGuard } from './guards/create-one-comment-thread.guard';
import { FindManyCommentThreadArgs } from '../@generated/comment-thread/find-many-comment-thread.args';
import { ArgsService } from './services/args.service';
@UseGuards(JwtAuthGuard)
@Resolver(() => CommentThread)
export class CommentThreadResolver {
constructor(private readonly prismaService: PrismaService) {}
constructor(
private readonly prismaService: PrismaService,
private readonly argsService: ArgsService,
) {}
@UseGuards(CreateOneCommentThreadGuard)
@Mutation(() => CommentThread, {
@ -35,4 +40,21 @@ export class CommentThreadResolver {
},
});
}
@Query(() => [CommentThread])
async findManyCommentThreads(
@Args() args: FindManyCommentThreadArgs,
@AuthWorkspace() workspace: Workspace,
) {
const preparedArgs =
await this.argsService.prepareFindManyArgs<FindManyCommentThreadArgs>(
args,
workspace,
);
const result = await this.prismaService.commentThread.findMany(
preparedArgs,
);
return result;
}
}

View File

@ -0,0 +1,22 @@
import * as TypeGraphQL from '@nestjs/graphql';
import { PrismaService } from 'src/database/prisma.service';
import { User } from 'src/api/@generated/user/user.model';
import { Comment } from 'src/api/@generated/comment/comment.model';
@TypeGraphQL.Resolver(() => Comment)
export class CommentRelationsResolver {
constructor(private readonly prismaService: PrismaService) {}
@TypeGraphQL.ResolveField(() => User, {
nullable: true,
})
async author(@TypeGraphQL.Parent() comment: Comment): Promise<User | null> {
return await this.prismaService.comment
.findFirst({
where: {
id: comment.id,
},
})
.author({});
}
}