Skip to content
This repository has been archived by the owner on Oct 17, 2021. It is now read-only.

feat: Prejudice.relatedBooksを追加 #55

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/prejudices/dto/resolve-related-books.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {ArgsType, Field} from '@nestjs/graphql';

import {BookOrder, BookOrderField} from '~/books/books.entities';
import {OffsetPaginationArgs, OrderDirection} from '~/common/common.entities';

@ArgsType()
export class ResolveRelatedBooksArgs extends OffsetPaginationArgs {
@Field(() => BookOrder, {
defaultValue: {
direction: OrderDirection.DESC,
field: BookOrderField.TITLE,
},
nullable: true,
})
orderBy!: BookOrder;
}
5 changes: 5 additions & 0 deletions src/prejudices/prejudices.cypher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export const CYPHER_GET_PREJUDICE_RELATED_BOOKS_ORDERBY_TITLE_AT_ASC =
export const CYPHER_GET_PREJUDICE_RELATED_BOOKS_ORDERBY_TITLE_AT_DESC =
getRelatedBooks('title', 'DESC');

export const CYPHER_COUNT_PREJUDICE_RELATED_BOOKS = `
MATCH (:Prejudice {id: $id})-[:RELATED_BOOK]->(b:Book)
RETURN count(b) AS count
`;

export const CYPHER_CREATE_PREJUDICE = `
MATCH (pu:User {id: $posted})
MATCH (ru:User {id: $received})
Expand Down
15 changes: 7 additions & 8 deletions src/prejudices/prejudices.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ import {
PostPrejudiceArgs,
PostPrejudicePayload,
} from './dto/post-prejudice.dto';
import {ResolveRelatedBooksArgs} from './dto/resolve-related-books.dto';

import {UserEntity} from '~/users/users.entities';
import {UsersService} from '~/users/users.service';
import {SettingsService} from '~/settings/settings.service';
import {GraphQLJwtGuard} from '~/auth/graphql-jwt.guard';
import {Viewer, ViewerType} from '~/auth/viewer.decorator';
import {AnswerEntity} from '~/answers/answers.entities';
import {BookArray} from '~/books/books.entities';

@Resolver(() => PrejudiceEntity)
export class PrejudicesResolver {
Expand Down Expand Up @@ -84,22 +86,19 @@ export class PrejudicesResolver {
return this.prejudicesService.resolveAnswer(id);
}

/*
@ResolveField('relatedBooks')
@ResolveField(() => BookArray, {name: 'relatedBooks'})
async getRelatedBooks(
@Parent() {id}: PrejudiceEntity,
@Args('skip') skip: number,
@Args('limit') limit: number,
@Args('orderBy') orderBy: BookOrder,
): Promise<BookConnection> {
@Args() {skip, limit, orderBy}: ResolveRelatedBooksArgs,
): Promise<BookArray> {
const nodes = await this.prejudicesService.resolveRelatedBooks(id, {
skip,
limit,
orderBy,
});
return {nodes};
const totalCount = await this.prejudicesService.countRelatedBooks(id);
return {nodes, totalCount};
}
*/

@Query(() => PrejudiceEntity, {name: 'prejudice'})
async getPrejudiceById(
Expand Down
11 changes: 11 additions & 0 deletions src/prejudices/prejudices.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
CYPHER_RESOLVE_PREJUDICE_TITLE,
CYPHER_RESOLVE_PREJUDICE_CREATED_AT,
CYPHER_RESOLVE_PREJUDICE_NUMBER,
CYPHER_COUNT_PREJUDICE_RELATED_BOOKS,
} from './prejudices.cypher';
import {PrejudiceEntity} from './prejudices.entities';

Expand Down Expand Up @@ -94,6 +95,16 @@ export class PrejudicesService {
);
}

async countRelatedBooks(id: string) {
const result = await this.neo4jService.read(
CYPHER_COUNT_PREJUDICE_RELATED_BOOKS,
{id},
);
if (result.records.length !== 1)
throw new Error('something broken with neo4j');
return result.records[0].get('count').toNumber();
}

async getById(id: string): Promise<PrejudiceEntity | null> {
const result = await this.neo4jService.read(CYPHER_GET_PREJUDICE_BY_ID, {
id,
Expand Down