Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add author related filter input for ListArticles #68

Merged
merged 1 commit into from
Mar 11, 2018
Merged
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
53 changes: 53 additions & 0 deletions src/graphql/queries/ListArticles.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GraphQLInt, GraphQLString, GraphQLInputObjectType } from 'graphql';
import client from 'util/client';

import {
createFilterType,
Expand Down Expand Up @@ -44,6 +45,23 @@ export default {
description:
'List only the articles whose number of replies matches the criteria.',
},
appId: {
type: GraphQLString,
description:
'Use with userId to show only articles from a specific user.',
},
userId: {
type: GraphQLString,
description:
'Use with appId to show only articles from a specific user.',
},
fromUserOfArticleId: {
type: GraphQLString,
description: `
Specify an articleId here to show only articles from the sender of that specified article.
When specified, it overrides the settings of appId and userId.
`,
},
}),
},
orderBy: {
Expand All @@ -70,6 +88,41 @@ export default {
const mustQueries = []; // Affects scores
const filterQueries = []; // Not affects scores

if (filter.fromUserOfArticleId) {
let specifiedArticle;
try {
specifiedArticle = (await client.get({
index: 'articles',
type: 'doc',
id: filter.fromUserOfArticleId,
_source: ['userId', 'appId'],
}))._source;
} catch (e) {
if (e.statusCode && e.statusCode === 404) {
throw new Error(
'fromUserOfArticleId does not match any existing articles'
);
}

// Re-throw unknown error
throw e;
}

// Overriding filter's userId and appId, as indicated in the description
//
filter.userId = specifiedArticle.userId;
filter.appId = specifiedArticle.appId;
}

if (filter.appId && filter.userId) {
filterQueries.push(
{ term: { appId: filter.appId } },
{ term: { userId: filter.userId } }
);
} else if (filter.appId || filter.userId) {
throw new Error('Both appId and userId must be specified at once');
}

if (filter.moreLikeThis) {
mustQueries.push({
// Ref: http://stackoverflow.com/a/8831494/1582110
Expand Down
6 changes: 6 additions & 0 deletions src/graphql/queries/__fixtures__/ListArticles.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default {
'/articles/doc/listArticleTest1': {
userId: 'user1',
appId: 'app1',
replyRequestCount: 2,
normalArticleReplyCount: 2,
updatedAt: 1,
Expand All @@ -10,13 +12,17 @@ export default {
`,
},
'/articles/doc/listArticleTest2': {
userId: 'user1',
appId: 'app1',
replyRequestCount: 1,
normalArticleReplyCount: 1,
updatedAt: 2,
text:
'臣亮言:先帝創業未半,而中道崩殂。今天下三分,益州 疲弊,此誠危急存亡之秋也。',
},
'/articles/doc/listArticleTest3': {
userId: 'user2',
appId: 'app1',
replyRequestCount: 0,
normalArticleReplyCount: 0,
updatedAt: 3,
Expand Down
64 changes: 64 additions & 0 deletions src/graphql/queries/__tests__/ListArticles.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,70 @@ describe('ListArticles', () => {
).toMatchSnapshot();
});

it('filters by userId, appId and fromUserOfArticleId', async () => {
// Lists only articles by userId & appId
expect(
await gql`
{
ListArticles(filter: { userId: "user1", appId: "app1" }) {
edges {
node {
id
}
}
totalCount
}
}
`()
).toMatchSnapshot();

// Lists only articles by fromUserOfArticleId
expect(
await gql`
{
ListArticles(filter: { fromUserOfArticleId: "listArticleTest1" }) {
edges {
node {
id
}
}
totalCount
}
}
`()
).toMatchSnapshot();
});

it('throws error when author filter is not set correctly', async () => {
const { errors: noUserIdError } = await gql`
{
ListArticles(filter: { appId: "specified-but-no-user-id" }) {
edges {
node {
id
}
}
totalCount
}
}
`();
expect(noUserIdError).toMatchSnapshot();

const { errors: notExistError } = await gql`
{
ListArticles(filter: { fromUserOfArticleId: "not-exist" }) {
edges {
node {
id
}
}
totalCount
}
}
`();
expect(notExistError).toMatchSnapshot();
});

it('supports after', async () => {
expect(
await gql`
Expand Down
56 changes: 56 additions & 0 deletions src/graphql/queries/__tests__/__snapshots__/ListArticles.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,50 @@ Object {
}
`;

exports[`ListArticles filters by userId, appId and fromUserOfArticleId 1`] = `
Object {
"data": Object {
"ListArticles": Object {
"edges": Array [
Object {
"node": Object {
"id": "listArticleTest2",
},
},
Object {
"node": Object {
"id": "listArticleTest1",
},
},
],
"totalCount": 2,
},
},
}
`;

exports[`ListArticles filters by userId, appId and fromUserOfArticleId 2`] = `
Object {
"data": Object {
"ListArticles": Object {
"edges": Array [
Object {
"node": Object {
"id": "listArticleTest2",
},
},
Object {
"node": Object {
"id": "listArticleTest1",
},
},
],
"totalCount": 2,
},
},
}
`;

exports[`ListArticles lists all articles 1`] = `
Object {
"data": Object {
Expand Down Expand Up @@ -216,3 +260,15 @@ Object {
},
}
`;

exports[`ListArticles throws error when author filter is not set correctly 1`] = `
Array [
[GraphQLError: Both appId and userId must be specified at once],
]
`;

exports[`ListArticles throws error when author filter is not set correctly 2`] = `
Array [
[GraphQLError: fromUserOfArticleId does not match any existing articles],
]
`;