-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #248 from cofacts/contributions
include contributions in GetUser query
- Loading branch information
Showing
10 changed files
with
374 additions
and
1 deletion.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
src/graphql/dataLoaders/__fixtures__/contributionsLoaderFactory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
export default { | ||
'/replyrequests/doc/replyrequest1': { | ||
userId: 'user1', | ||
createdAt: '2019-01-01T00:00:00.000+08:00', | ||
}, | ||
'/replyrequests/doc/replyrequest2': { | ||
userId: 'user1', | ||
createdAt: '2020-02-01T00:00:00.000+08:00', | ||
}, | ||
'/replyrequests/doc/replyrequest3': { | ||
userId: 'user2', | ||
createdAt: '2020-02-01T00:00:00.000+08:00', | ||
}, | ||
'/replyrequests/doc/replyrequest4': { | ||
userId: 'user1', | ||
createdAt: '2020-02-02T00:00:00.000+08:00', | ||
}, | ||
'/replyrequests/doc/replyrequest5': { | ||
userId: 'user1', | ||
createdAt: '2020-02-03T00:00:00.000+08:00', | ||
}, | ||
'/replyrequests/doc/replyrequest6': { | ||
userId: 'user1', | ||
createdAt: '2020-02-03T00:00:00.000+08:00', | ||
}, | ||
'/replyrequests/doc/replyrequest7': { | ||
userId: 'user1', | ||
createdAt: '2020-02-04T00:00:00.000+08:00', | ||
}, | ||
|
||
'/replies/doc/reply1': { | ||
userId: 'user1', | ||
createdAt: '2019-01-01T00:00:00.000+08:00', | ||
}, | ||
'/replies/doc/reply2': { | ||
userId: 'user2', | ||
createdAt: '2020-01-01T00:00:00.000+08:00', | ||
}, | ||
'/replies/doc/reply3': { | ||
userId: 'user2', | ||
createdAt: '2020-02-03T00:00:00.000+08:00', | ||
}, | ||
'/replies/doc/reply4': { | ||
userId: 'user1', | ||
createdAt: '2020-02-01T00:00:00.000+08:00', | ||
}, | ||
'/replies/doc/reply5': { | ||
userId: 'user1', | ||
createdAt: '2020-02-04T00:00:00.000+08:00', | ||
}, | ||
'/replies/doc/reply6': { | ||
userId: 'user1', | ||
createdAt: '2020-02-09T00:00:00.000+08:00', | ||
}, | ||
'/replies/doc/reply7': { | ||
userId: 'user1', | ||
createdAt: '2020-02-09T00:00:00.000+08:00', | ||
}, | ||
|
||
'/articlereplyfeedbacks/doc/f1': { | ||
userId: 'user1', | ||
createdAt: '2019-01-01T00:00:00.000+08:00', | ||
}, | ||
'/articlereplyfeedbacks/doc/f2': { | ||
userId: 'user2', | ||
createdAt: '2020-01-01T00:00:00.000+08:00', | ||
}, | ||
'/articlereplyfeedbacks/doc/f3': { | ||
userId: 'user2', | ||
createdAt: '2020-02-03T00:00:00.000+08:00', | ||
}, | ||
'/articlereplyfeedbacks/doc/f4': { | ||
userId: 'user1', | ||
createdAt: '2020-02-01T00:00:00.000+08:00', | ||
}, | ||
'/articlereplyfeedbacks/doc/f5': { | ||
userId: 'user1', | ||
createdAt: '2020-02-04T00:00:00.000+08:00', | ||
}, | ||
'/articlereplyfeedbacks/doc/f6': { | ||
userId: 'user1', | ||
createdAt: '2020-02-06T00:00:00.000+08:00', | ||
}, | ||
'/articlereplyfeedbacks/doc/f7': { | ||
userId: 'user1', | ||
createdAt: '2020-02-08T00:00:00.000+08:00', | ||
}, | ||
}; |
63 changes: 63 additions & 0 deletions
63
src/graphql/dataLoaders/__tests__/__snapshots__/contirubtionsLoaderFactory.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`contributionsLoaderFactory should load data for the date range specified 1`] = ` | ||
Array [ | ||
Object { | ||
"count": 1, | ||
"date": "2020-02-02", | ||
}, | ||
Object { | ||
"count": 2, | ||
"date": "2020-02-03", | ||
}, | ||
Object { | ||
"count": 3, | ||
"date": "2020-02-04", | ||
}, | ||
Object { | ||
"count": 1, | ||
"date": "2020-02-06", | ||
}, | ||
Object { | ||
"count": 1, | ||
"date": "2020-02-08", | ||
}, | ||
Object { | ||
"count": 2, | ||
"date": "2020-02-09", | ||
}, | ||
] | ||
`; | ||
|
||
exports[`contributionsLoaderFactory should load last year of data for given userId 1`] = ` | ||
Array [ | ||
Object { | ||
"count": 3, | ||
"date": "2020-02-01", | ||
}, | ||
Object { | ||
"count": 1, | ||
"date": "2020-02-02", | ||
}, | ||
Object { | ||
"count": 2, | ||
"date": "2020-02-03", | ||
}, | ||
Object { | ||
"count": 3, | ||
"date": "2020-02-04", | ||
}, | ||
Object { | ||
"count": 1, | ||
"date": "2020-02-06", | ||
}, | ||
Object { | ||
"count": 1, | ||
"date": "2020-02-08", | ||
}, | ||
Object { | ||
"count": 2, | ||
"date": "2020-02-09", | ||
}, | ||
] | ||
`; |
42 changes: 42 additions & 0 deletions
42
src/graphql/dataLoaders/__tests__/contirubtionsLoaderFactory.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { loadFixtures, unloadFixtures } from 'util/fixtures'; | ||
import DataLoaders from '../index'; | ||
import fixtures from '../__fixtures__/contributionsLoaderFactory'; | ||
import MockDate from 'mockdate'; | ||
|
||
const loader = new DataLoaders(); | ||
MockDate.set(1609430400000); // 2021-01-01T00:00:00.000+08:00 | ||
|
||
describe('contributionsLoaderFactory', () => { | ||
beforeAll(() => loadFixtures(fixtures)); | ||
|
||
it('should load last year of data for given userId', async () => { | ||
const res = await loader.contributionsLoader.load({ | ||
userId: 'user1', | ||
}); | ||
expect(res).toMatchSnapshot(); | ||
}); | ||
|
||
it('should load data for the date range specified', async () => { | ||
expect( | ||
await loader.contributionsLoader.load({ | ||
userId: 'user1', | ||
dateRange: { | ||
gte: '2020-02-01T00:00:00.000Z', | ||
lte: '2020-03-01T00:00:00.000Z', | ||
}, | ||
}) | ||
).toMatchSnapshot(); | ||
}); | ||
|
||
it('should throw error if userId is not present', async () => { | ||
let error; | ||
try { | ||
await loader.contributionsLoader.load({}); | ||
} catch (e) { | ||
error = e.message; | ||
} | ||
expect(error).toBe('userId is required'); | ||
}); | ||
|
||
afterAll(() => unloadFixtures(fixtures)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { subDays, startOfWeek } from 'date-fns'; | ||
import DataLoader from 'dataloader'; | ||
import client from 'util/client'; | ||
import { getRangeFieldParamFromArithmeticExpression } from 'graphql/util'; | ||
|
||
const defaultDuration = 365; | ||
|
||
export default () => | ||
new DataLoader( | ||
async statsQueries => { | ||
const body = []; | ||
const defaultEndDate = new Date(); | ||
const defaultStartDate = startOfWeek( | ||
subDays(defaultEndDate, defaultDuration) | ||
); | ||
const defaultDateRange = { | ||
gt: defaultStartDate, | ||
lte: defaultEndDate, | ||
}; | ||
statsQueries.forEach(({ userId, dateRange = defaultDateRange }) => { | ||
if (!userId) throw new Error('userId is required'); | ||
body.push({ | ||
index: ['replyrequests', 'replies', 'articlereplyfeedbacks'], | ||
type: 'doc', | ||
}); | ||
body.push({ | ||
query: { | ||
bool: { | ||
must: [ | ||
{ term: { userId } }, | ||
{ | ||
range: { | ||
createdAt: getRangeFieldParamFromArithmeticExpression( | ||
dateRange | ||
), | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
size: 0, | ||
aggs: { | ||
contributions: { | ||
date_histogram: { | ||
field: 'createdAt', | ||
interval: 'day', | ||
min_doc_count: 1, | ||
format: 'yyyy-MM-dd', | ||
time_zone: '+08:00', | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
return (await client.msearch({ | ||
body, | ||
})).body.responses.map( | ||
({ | ||
aggregations: { | ||
contributions: { buckets }, | ||
}, | ||
}) => | ||
buckets | ||
? buckets.map(bucket => ({ | ||
date: bucket.key_as_string, | ||
count: bucket.doc_count, | ||
})) | ||
: [] | ||
); | ||
}, | ||
{ | ||
cacheKeyFn: ({ userId, dateRange }) => | ||
`${userId}/${JSON.stringify(dateRange)}`, | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { GraphQLInt, GraphQLObjectType, GraphQLString } from 'graphql'; | ||
|
||
export default new GraphQLObjectType({ | ||
name: 'Contribution', | ||
fields: () => ({ | ||
date: { type: GraphQLString }, | ||
count: { type: GraphQLInt }, | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.