Skip to content

Commit

Permalink
feat: add query for autocomplete suggestions (#158)
Browse files Browse the repository at this point in the history
* feat: add query for autocomplete suggestion

* chore: change to getAutoComplete

Co-authored-by: Rutaj Dash <33367546+rutajdash@users.noreply.github.com>

* chore: change to getAutoComplete

Co-authored-by: Rutaj Dash <33367546+rutajdash@users.noreply.github.com>

* chore: change to getAutoComplete

Co-authored-by: Rutaj Dash <33367546+rutajdash@users.noreply.github.com>

* chore: change to getAutoComplete

Co-authored-by: Rutaj Dash <33367546+rutajdash@users.noreply.github.com>

Co-authored-by: Rutaj Dash <33367546+rutajdash@users.noreply.github.com>
  • Loading branch information
120EE0692 and rutajdash authored Nov 5, 2022
1 parent bf9e089 commit 53f2d03
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
18 changes: 18 additions & 0 deletions server/schema/article/article.datasources.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,23 @@ const search = (keywords, allowRestricted, onlyPublished, limit, offset) =>
createdAt: 'desc',
});

const autoComplete = (keyword, allowRestricted, onlyPublished, limit) =>
ArticleModel.aggregate([
{
$search: { index: 'autoComplete', autocomplete: { query: keyword, path: 'title' } },
},
{
$match: {
$and: getBaseConditions(allowRestricted, onlyPublished),
},
},
{
$limit: limit,
},
]).sort({
createdAt: 'desc',
});

const create = async (
articleType,
title,
Expand Down Expand Up @@ -425,6 +442,7 @@ const ArticleDataSources = () => ({
findByYearAndMonth,
findAll,
search,
autoComplete,
create,
updateProps,
updateUsers,
Expand Down
16 changes: 16 additions & 0 deletions server/schema/article/article.query.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const {
getListOfArticles,
getArticlesByCategories,
searchArticle,
getAutoComplete,
listArticlesByYearAndMonth,
listAllArticles,
countOfArticlesBySubCategory,
Expand Down Expand Up @@ -166,6 +167,21 @@ module.exports = new GraphQLObjectType({
},
resolve: searchArticle,
},
getAutoComplete: {
description: 'auto complete suggestion for articles using keywords',
type: new GraphQLList(ArticleType),
args: {
keywords: {
description: 'The search keywords.',
type: new GraphQLNonNull(GraphQLString),
},
limit: {
description: 'The number of results to return',
type: GraphQLInt,
},
},
resolve: getAutoComplete,
},
countTotalArticles: {
description: 'Counts the total number of articles',
type: GraphQLInt,
Expand Down
20 changes: 20 additions & 0 deletions server/schema/article/article.resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,26 @@ module.exports = {
throw APIError(null, error);
}
},
getAutoComplete: async (
_parent,
{ keywords, limit = DEF_LIMIT },
{ session, authToken, decodedToken, API: { Article } }
) => {
try {
const allowRestricted = UserPermission.exists(session, authToken, decodedToken, 'article.list.restricted');
const onlyPublished = !UserPermission.exists(session, authToken, decodedToken, 'article.list.unpublished');

const _articles = await Article.autoComplete(keywords, allowRestricted, onlyPublished, limit);

if (!_articles || _articles.length <= 0) {
throw APIError('NOT_FOUND', null, { reason: 'No articles were found with the given keywords.' });
}
return _articles;
} catch (error) {
throw APIError(null, error);
}
},

createArticle: async (
_parent,
{ articleType, title, authors, photographers, designers, tech, categories },
Expand Down

0 comments on commit 53f2d03

Please sign in to comment.