From d33b21309203c1f53ebacd76555ccb2fa0ee4908 Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 22 Feb 2022 17:00:15 +0800 Subject: [PATCH] add setLabels api and tests --- packages/api/src/entity/label.ts | 7 - packages/api/src/entity/link.ts | 8 +- packages/api/src/entity/link_label.ts | 27 ++++ packages/api/src/generated/graphql.ts | 56 ++++++++ packages/api/src/generated/schema.graphql | 22 +++ .../api/src/resolvers/function_resolvers.ts | 3 + packages/api/src/resolvers/labels/index.ts | 73 ++++++++++ packages/api/src/schema.ts | 22 +++ packages/api/test/db.ts | 15 ++ packages/api/test/resolvers/labels.test.ts | 128 +++++++++++++++--- 10 files changed, 333 insertions(+), 28 deletions(-) create mode 100644 packages/api/src/entity/link_label.ts diff --git a/packages/api/src/entity/label.ts b/packages/api/src/entity/label.ts index dec2046f4..0b36eddec 100644 --- a/packages/api/src/entity/label.ts +++ b/packages/api/src/entity/label.ts @@ -4,13 +4,10 @@ import { CreateDateColumn, Entity, JoinColumn, - JoinTable, - ManyToMany, ManyToOne, PrimaryGeneratedColumn, } from 'typeorm' import { User } from './user' -import { Link } from './link' @Entity({ name: 'labels' }) export class Label extends BaseEntity { @@ -24,10 +21,6 @@ export class Label extends BaseEntity { @JoinColumn({ name: 'user_id' }) user!: User - @ManyToMany(() => Link, (link) => link.labels) - @JoinTable({ name: 'link_labels' }) - link?: Link - @Column('text') color!: string diff --git a/packages/api/src/entity/link.ts b/packages/api/src/entity/link.ts index fcd9ee2f4..40c0f208a 100644 --- a/packages/api/src/entity/link.ts +++ b/packages/api/src/entity/link.ts @@ -63,7 +63,11 @@ export class Link extends BaseEntity { @UpdateDateColumn() updatedAt?: Date - @ManyToMany(() => Label, (label) => label.link) - @JoinTable({ name: 'link_labels' }) + @ManyToMany(() => Label) + @JoinTable({ + name: 'link_labels', + joinColumn: { name: 'link_id' }, + inverseJoinColumn: { name: 'label_id' }, + }) labels?: Label[] } diff --git a/packages/api/src/entity/link_label.ts b/packages/api/src/entity/link_label.ts new file mode 100644 index 000000000..5bcdd64d6 --- /dev/null +++ b/packages/api/src/entity/link_label.ts @@ -0,0 +1,27 @@ +import { + BaseEntity, + CreateDateColumn, + Entity, + JoinColumn, + ManyToOne, + PrimaryGeneratedColumn, +} from 'typeorm' +import { Link } from './link' +import { Label } from './label' + +@Entity({ name: 'link_labels' }) +export class LinkLabel extends BaseEntity { + @PrimaryGeneratedColumn('uuid') + id!: string + + @ManyToOne(() => Link) + @JoinColumn({ name: 'link_id' }) + link!: Link + + @ManyToOne(() => Label) + @JoinColumn({ name: 'label_id' }) + label!: Label + + @CreateDateColumn() + createdAt!: Date +} diff --git a/packages/api/src/generated/graphql.ts b/packages/api/src/generated/graphql.ts index e854ff299..c844284eb 100644 --- a/packages/api/src/generated/graphql.ts +++ b/packages/api/src/generated/graphql.ts @@ -778,6 +778,7 @@ export type Mutation = { setBookmarkArticle: SetBookmarkArticleResult; setDeviceToken: SetDeviceTokenResult; setFollow: SetFollowResult; + setLabels: SetLabelsResult; setLinkArchived: ArchiveLinkResult; setShareArticle: SetShareArticleResult; setShareHighlight: SetShareHighlightResult; @@ -919,6 +920,11 @@ export type MutationSetFollowArgs = { }; +export type MutationSetLabelsArgs = { + input: SetLabelsInput; +}; + + export type MutationSetLinkArchivedArgs = { input: ArchiveLinkInput; }; @@ -1350,6 +1356,29 @@ export type SetFollowSuccess = { updatedUser: User; }; +export type SetLabelsError = { + __typename?: 'SetLabelsError'; + errorCodes: Array; +}; + +export enum SetLabelsErrorCode { + BadRequest = 'BAD_REQUEST', + NotFound = 'NOT_FOUND', + Unauthorized = 'UNAUTHORIZED' +} + +export type SetLabelsInput = { + labelIds: Array; + linkId: Scalars['ID']; +}; + +export type SetLabelsResult = SetLabelsError | SetLabelsSuccess; + +export type SetLabelsSuccess = { + __typename?: 'SetLabelsSuccess'; + labels: Array