From f4b91914767588fa6c3810ad80160140f1dbd185 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Tue, 16 May 2017 14:19:40 -0600 Subject: [PATCH] Added author_id to comment count query --- app.js | 8 ++- graph/loaders/comments.js | 8 ++- graph/resolvers/root_query.js | 6 +- graph/subscriptions.js | 21 +++--- graph/typeDefs.graphql | 6 +- views/graphiql.ejs | 119 ++++++++++++++++++++++++++++++++++ 6 files changed, 148 insertions(+), 20 deletions(-) create mode 100644 views/graphiql.ejs diff --git a/app.js b/app.js index f5243f81a..9aca8b0ef 100644 --- a/app.js +++ b/app.js @@ -69,9 +69,11 @@ app.use('/api/v1/graph/ql', apollo.graphqlExpress(createGraphOptions)); if (app.get('env') !== 'production') { // Interactive graphiql interface. - app.use('/api/v1/graph/iql', apollo.graphiqlExpress({ - endpointURL: '/api/v1/graph/ql' - })); + app.use('/api/v1/graph/iql', (req, res) => { + res.render('graphiql', { + endpointURL: '/api/v1/graph/ql' + }); + }); // GraphQL documention. app.get('/admin/docs', (req, res) => { diff --git a/graph/loaders/comments.js b/graph/loaders/comments.js index ccee16ca7..96f0e8f73 100644 --- a/graph/loaders/comments.js +++ b/graph/loaders/comments.js @@ -120,7 +120,7 @@ const getParentCountByAssetIDPersonalized = async (context, {assetId, excludeIgn const ignoredUsers = freshUser.ignoresUsers; query.author_id = {$nin: ignoredUsers}; } - + return CommentModel.where(query).count(); }; @@ -191,7 +191,7 @@ const getCountByParentIDPersonalized = async (context, {id, excludeIgnored}) => * @return {Promise} resolves to the counts of the comments from the * query */ -const getCommentCountByQuery = (context, {ids, statuses, asset_id, parent_id}) => { +const getCommentCountByQuery = (context, {ids, statuses, asset_id, parent_id, author_id}) => { let query = CommentModel.find(); if (ids) { @@ -210,6 +210,10 @@ const getCommentCountByQuery = (context, {ids, statuses, asset_id, parent_id}) = query = query.where({parent_id}); } + if (author_id) { + query = query.where({author_id}); + } + return CommentModel .find(query) .count(); diff --git a/graph/resolvers/root_query.js b/graph/resolvers/root_query.js index 7fd072854..4f79b1c18 100644 --- a/graph/resolvers/root_query.js +++ b/graph/resolvers/root_query.js @@ -34,7 +34,7 @@ const RootQuery = { comment(_, {id}, {loaders: {Comments}}) { return Comments.get.load(id); }, - async commentCount(_, {query: {action_type, statuses, asset_id, parent_id}}, {user, loaders: {Actions, Comments}}) { + async commentCount(_, {query: {action_type, statuses, asset_id, parent_id, author_id}}, {user, loaders: {Actions, Comments}}) { if (user == null || !user.hasRoles('ADMIN')) { return null; } @@ -43,10 +43,10 @@ const RootQuery = { let ids = await Actions.getByTypes({action_type, item_type: 'COMMENTS'}); // Perform the query using the available resolver. - return Comments.getCountByQuery({ids, statuses, asset_id, parent_id}); + return Comments.getCountByQuery({ids, statuses, asset_id, parent_id, author_id}); } - return Comments.getCountByQuery({statuses, asset_id, parent_id}); + return Comments.getCountByQuery({statuses, asset_id, parent_id, author_id}); }, assetMetrics(_, {from, to, sort, limit = 10}, {user, loaders: {Metrics: {Assets}}}) { diff --git a/graph/subscriptions.js b/graph/subscriptions.js index 369b5c058..2eb676cc4 100644 --- a/graph/subscriptions.js +++ b/graph/subscriptions.js @@ -1,6 +1,7 @@ const {SubscriptionManager} = require('graphql-subscriptions'); const {SubscriptionServer} = require('subscriptions-transport-ws'); const _ = require('lodash'); +const debug = require('debug')('talk:graph:subscriptions'); const pubsub = require('./pubsub'); const schema = require('./schema'); @@ -9,24 +10,22 @@ const plugins = require('../services/plugins'); const {deserializeUser} = require('../services/subscriptions'); -// Core setup functions -let setupFunctions = { - commentAdded: (options, args) => ({ - commentAdded: { - filter: (comment) => comment.asset_id === args.asset_id - }, - }), -}; - /** * Plugin support requires that we merge in existing setupFunctions with our new * plugin based ones. This allows plugins to extend existing setupFunctions as well * as provide new ones. */ -setupFunctions = plugins.get('server', 'setupFunctions').reduce((acc, {setupFunctions}) => { +const setupFunctions = plugins.get('server', 'setupFunctions').reduce((acc, {plugin, setupFunctions}) => { + debug(`added plugin '${plugin.name}'`); return _.merge(acc, setupFunctions); -}, setupFunctions); +}, { + commentAdded: (options, args) => ({ + commentAdded: { + filter: (comment) => comment.asset_id === args.asset_id + }, + }), +}); /** * This creates a new subscription manager. diff --git a/graph/typeDefs.graphql b/graph/typeDefs.graphql index d4f035da6..b11889865 100644 --- a/graph/typeDefs.graphql +++ b/graph/typeDefs.graphql @@ -194,6 +194,10 @@ input CommentCountQuery { # type. action_type: ACTION_TYPE + # author_id allows the querying of comment counts based on the author of the + # comments. + author_id: ID + # Filter by a specific tag name. tag: [String] } @@ -779,7 +783,7 @@ type EditCommentResponse implements Response { comment: Comment # An array of errors relating to the mutation that occured. - errors: [UserError] + errors: [UserError] } # All mutations for the application are defined on this object. diff --git a/views/graphiql.ejs b/views/graphiql.ejs new file mode 100644 index 000000000..5f1759c0d --- /dev/null +++ b/views/graphiql.ejs @@ -0,0 +1,119 @@ + + + + + + GraphiQL + + + + + + + + + + + + \ No newline at end of file