Skip to content

Commit a09f2c8

Browse files
committed
feat: can now sort aggregations on records
1 parent 1d48814 commit a09f2c8

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/schema/query/recordsAggregation.query.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { GraphQLError, GraphQLID, GraphQLInt, GraphQLNonNull } from 'graphql';
1+
import {
2+
GraphQLError,
3+
GraphQLID,
4+
GraphQLInt,
5+
GraphQLNonNull,
6+
GraphQLString,
7+
} from 'graphql';
28
import GraphQLJSON from 'graphql-type-json';
39
import mongoose from 'mongoose';
410
import { cloneDeep, get, isEqual, set, unset } from 'lodash';
@@ -70,6 +76,8 @@ export default {
7076
mapping: { type: GraphQLJSON },
7177
first: { type: GraphQLInt },
7278
skip: { type: GraphQLInt },
79+
sortField: { type: GraphQLString },
80+
sortOrder: { type: GraphQLString },
7381
},
7482
async resolve(parent, args, context) {
7583
// Make sure that the page size is not too important
@@ -508,6 +516,13 @@ export default {
508516
id: '$_id',
509517
},
510518
});
519+
if (args.sortField && args.sortOrder) {
520+
pipeline.push({
521+
$sort: {
522+
[args.sortField]: args.sortOrder === 'asc' ? 1 : -1,
523+
},
524+
});
525+
}
511526
pipeline.push({
512527
$facet: {
513528
items: [{ $skip: skip }, { $limit: first }],

0 commit comments

Comments
 (0)