Skip to content

Commit

Permalink
feat(schema): revise inputs of search API
Browse files Browse the repository at this point in the history
  • Loading branch information
devformatters authored and Zeck Li committed Jul 15, 2020
1 parent c44cbd2 commit ddc0f8d
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 33 deletions.
4 changes: 2 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1711,8 +1711,8 @@ enum Role {
admin
}

enum SearchFilter {
author
input SearchFilter {
authorId: ID
}

input SearchInput {
Expand Down
32 changes: 14 additions & 18 deletions src/connectors/articleService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,20 +216,20 @@ export class ArticleService extends BaseService {
findByTitle = async ({
title,
oss = false,
filters,
filter,
}: {
title: string
oss?: boolean
filters?: Record<string, any>
filter?: Record<string, any>
}) => {
const query = this.knex.select().from(this.table).where({ title })

if (!oss) {
query.andWhere({ state: ARTICLE_STATE.active })
}

if (filters && Object.keys(filters).length > 0) {
query.andWhere(filters)
if (filter && Object.keys(filter).length > 0) {
query.andWhere(filter)
}

return query.orderBy('id', 'desc')
Expand Down Expand Up @@ -381,20 +381,20 @@ export class ArticleService extends BaseService {
searchByMediaHash = async ({
key,
oss = false,
filters,
filter,
}: {
key: string
oss?: boolean
filters?: Record<string, any>
filter?: Record<string, any>
}) => {
const query = this.knex.select().from(this.table).where({ mediaHash: key })

if (!oss) {
query.andWhere({ state: ARTICLE_STATE.active })
}

if (filters && Object.keys(filters).length > 0) {
query.andWhere(filters)
if (filter && Object.keys(filter).length > 0) {
query.andWhere(filter)
}

const rows = await query
Expand All @@ -413,12 +413,12 @@ export class ArticleService extends BaseService {
first = 20,
offset,
oss = false,
filters,
filter,
}: GQLSearchInput & {
author?: string
offset: number
oss?: boolean
filters: Record<string, any>
filter?: Record<string, any>
}) => {
const searchBody = bodybuilder()
.query('multi_match', {
Expand All @@ -442,26 +442,22 @@ export class ArticleService extends BaseService {
}

// add filter
if (filters && Object.keys(filters).length > 0) {
searchBody.filter('term', filters)
if (filter && Object.keys(filter).length > 0) {
searchBody.filter('term', filter)
}

try {
// check if media hash in search key
const re = /^([0-9a-zA-Z]{49,59})$/gi
const match = re.exec(key)
if (match) {
return this.searchByMediaHash({
key: match[1],
oss,
filters,
})
return this.searchByMediaHash({ key: match[1], oss, filter })
}

// take the condition that searching for exact article title into consideration
const idsByTitle = []
if (key.length >= 5 && offset === 0) {
const articles = await this.findByTitle({ title: key, oss, filters })
const articles = await this.findByTitle({ title: key, oss, filter })
for (const article of articles) {
idsByTitle.push(article.id)
}
Expand Down
4 changes: 2 additions & 2 deletions src/definitions/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1514,8 +1514,8 @@ export const enum GQLSearchTypes {
Tag = 'Tag',
}

export const enum GQLSearchFilter {
author = 'author',
export interface GQLSearchFilter {
authorId?: string
}

export interface GQLSearchResultConnection extends GQLConnection {
Expand Down
8 changes: 1 addition & 7 deletions src/queries/system/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ const resolver: QueryToSearchResolver = async (
)
}

// add search filter
const filters: Record<string, any> = {}
if (input.filter === 'author') {
filters.authorId = viewer.id
}

const offset = cursorToIndex(input.after) + 1

const serviceMap = {
Expand All @@ -44,7 +38,7 @@ const resolver: QueryToSearchResolver = async (
}

const connection = await serviceMap[input.type]
.search({ ...input, offset, filters })
.search({ ...input, offset })
.then(({ nodes, totalCount }) => {
nodes = _.compact(nodes)
return {
Expand Down
8 changes: 4 additions & 4 deletions src/types/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,10 @@ export default /* GraphQL */ `
oss: Boolean
}
input SearchFilter {
authorId: ID
}
input ReleasesInput {
platform: PlatformType!
channel: ChannelType!
Expand Down Expand Up @@ -319,10 +323,6 @@ export default /* GraphQL */ `
Tag
}
enum SearchFilter {
author
}
enum BoostTypes {
Article
User
Expand Down

0 comments on commit ddc0f8d

Please sign in to comment.