Skip to content

Commit

Permalink
feat(api): add api for listing article's linked drafts
Browse files Browse the repository at this point in the history
  • Loading branch information
devformatters authored and Zeck Li committed Oct 21, 2020
1 parent 0f0a7c3 commit fecb2ca
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ type Article implements Node {
oss: ArticleOSS!
remark: String

"""Drafts linked to this article."""
drafts: [Draft!]

"""The counting number of comments."""
commentCount: Int!

Expand Down
14 changes: 14 additions & 0 deletions src/connectors/draftService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,18 @@ export class DraftService extends BaseService {
*/
findByMediaHash = async (mediaHash: string) =>
this.knex.select().from(this.table).where({ mediaHash }).first()

/**
* Find published drafts by given article id.
*/
findByArticleId = async ({ articleId }: { articleId: string }) =>
this.knex
.select()
.from(this.table)
.where({
articleId,
archived: true,
publishState: PUBLISH_STATE.published,
})
.orderBy('created_at', 'desc')
}
15 changes: 15 additions & 0 deletions src/definitions/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ export interface GQLArticle extends GQLNode {
oss: GQLArticleOSS
remark?: string

/**
* Drafts linked to this article.
*/
drafts?: Array<GQLDraft>

/**
* The counting number of comments.
*/
Expand Down Expand Up @@ -3424,6 +3429,7 @@ export interface GQLArticleTypeResolver<TParent = any> {
transactionsReceivedBy?: ArticleToTransactionsReceivedByResolver<TParent>
oss?: ArticleToOssResolver<TParent>
remark?: ArticleToRemarkResolver<TParent>
drafts?: ArticleToDraftsResolver<TParent>
commentCount?: ArticleToCommentCountResolver<TParent>
pinCommentLimit?: ArticleToPinCommentLimitResolver<TParent>
pinCommentLeft?: ArticleToPinCommentLeftResolver<TParent>
Expand Down Expand Up @@ -3782,6 +3788,15 @@ export interface ArticleToRemarkResolver<TParent = any, TResult = any> {
): TResult
}

export interface ArticleToDraftsResolver<TParent = any, TResult = any> {
(
parent: TParent,
args: {},
context: Context,
info: GraphQLResolveInfo
): TResult
}

export interface ArticleToCommentCountResolver<TParent = any, TResult = any> {
(
parent: TParent,
Expand Down
16 changes: 16 additions & 0 deletions src/queries/draft/article/drafts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { connectionFromPromisedArray, cursorToIndex } from 'common/utils'
import { ArticleToDraftsResolver } from 'definitions'

const resolver: ArticleToDraftsResolver = async (
{ articleId, authorId },
_,
{ viewer, dataSources: { draftService } }
) => {
const isAuthor = authorId === viewer.id
if (!isAuthor) {
return []
}
return draftService.findByArticleId({ articleId })
}

export default resolver
4 changes: 4 additions & 0 deletions src/queries/draft/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import slugify from '@matters/slugify'
import { countWords, makeSummary, toGlobalId } from 'common/utils'

import article from './article'
import articleDrafts from './article/drafts'
import assets from './assets'
import collection from './collection'
import draftCover from './cover'
import drafts from './drafts'

export default {
Article: {
drafts: articleDrafts,
},
User: {
drafts,
},
Expand Down
3 changes: 3 additions & 0 deletions src/types/article.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ export default /* GraphQL */ `
# OSS
oss: ArticleOSS! @auth(mode: "${AUTH_MODE.admin}")
remark: String @auth(mode: "${AUTH_MODE.admin}")
"Drafts linked to this article."
drafts: [Draft!]
}
"This type contains content, count and related data of an article tag."
Expand Down

0 comments on commit fecb2ca

Please sign in to comment.