From 24391de61d9d185ee5003d9c778b8d1e9c164c22 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Mon, 18 Sep 2017 11:06:31 -0300 Subject: [PATCH] userCount --- graph/loaders/users.js | 31 ++++++++++++++++++++++++++++++- graph/resolvers/root_query.js | 9 +++++++++ graph/typeDefs.graphql | 13 +++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/graph/loaders/users.js b/graph/loaders/users.js index b943c60ed..aa74aad29 100644 --- a/graph/loaders/users.js +++ b/graph/loaders/users.js @@ -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) } }); diff --git a/graph/resolvers/root_query.js b/graph/resolvers/root_query.js index 13e41a312..c42b7e9c2 100644 --- a/graph/resolvers/root_query.js +++ b/graph/resolvers/root_query.js @@ -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; diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index 916c0ade5..3191ddf06 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -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