userCount

This commit is contained in:
Belen Curcio
2017-09-18 11:06:31 -03:00
parent 18b8a18f1d
commit 24391de61d
3 changed files with 52 additions and 1 deletions
+30 -1
View File
@@ -107,6 +107,34 @@ const getUsersByQuery = async ({user, loaders: {Actions}}, {ids, limit, cursor,
};
};
/**
* Retrieves the count of users based on the passed in query.
* @param {Object} context graph context
* @param {Object} query query to execute against the users collection
* to compute the counts
* @return {Promise} resolves to the counts of the users from the
* query
*/
const getCountByQuery = async ({loaders: {Actions}}, {action_type}) => {
let query = UserModel.find();
if (action_type) {
const userIds = await Actions.getByTypes({action_type, item_type: 'USERS'});
query = query.find({
id: {
$in: userIds
}
});
}
return UserModel
.find(query)
.count();
};
/**
* Creates a set of loaders based on a GraphQL context.
* @param {Object} context the context of the GraphQL request
@@ -115,6 +143,7 @@ const getUsersByQuery = async ({user, loaders: {Actions}}, {ids, limit, cursor,
module.exports = (context) => ({
Users: {
getByQuery: (query) => getUsersByQuery(context, query),
getByID: new DataLoader((ids) => genUserByIDs(context, ids))
getByID: new DataLoader((ids) => genUserByIDs(context, ids)),
getCountByQuery: (query) => getCountByQuery(context, query)
}
});
+9
View File
@@ -50,6 +50,15 @@ const RootQuery = {
return Comments.getCountByQuery(query);
},
async userCount(_, {query}, {user, loaders: {Users}}) {
if (user == null || !user.can(SEARCH_OTHER_USERS)) {
return null;
}
return Users.getCountByQuery(query);
},
assetMetrics(_, query, {user, loaders: {Metrics: {Assets}}}) {
if (user == null || !user.can(SEARCH_ASSETS)) {
return null;
+13
View File
@@ -337,6 +337,15 @@ input CommentCountQuery {
tags: [String!]
}
# UserCountQuery allows the ability to query user counts by specific
# methods.
input UserCountQuery {
# comments returned will only be ones which have at least one action of this
# type.
action_type: ACTION_TYPE
}
type EditInfo {
edited: Boolean!
editableUntil: Date
@@ -833,6 +842,10 @@ type RootQuery {
# expensive as it is not batched. Requires the `ADMIN` role.
commentCount(query: CommentCountQuery!): Int
# Return the count of users satisfied by the query. Note that this edge is
# expensive as it is not batched. Requires the `ADMIN` role.
userCount(query: UserCountQuery!): Int
# The currently logged in user based on the request. Requires any logged in
# role.
me: User