mirror of
https://github.com/wassname/talk.git
synced 2026-09-10 12:43:11 +08:00
Initial subscription impl
This commit is contained in:
+4
-1
@@ -31,7 +31,7 @@ const decorateContextPlugins = (context, contextPlugins) => contextPlugins.reduc
|
||||
* Stores the request context.
|
||||
*/
|
||||
class Context {
|
||||
constructor({user = null}) {
|
||||
constructor({user = null}, pubsub) {
|
||||
|
||||
// Load the current logged in user to `user`, otherwise this'll be null.
|
||||
if (user) {
|
||||
@@ -46,6 +46,9 @@ class Context {
|
||||
|
||||
// Decorate the plugin context.
|
||||
this.plugins = decorateContextPlugins(this, contextPlugins);
|
||||
|
||||
// Bind the publish/subscribe to the context.
|
||||
this.pubsub = pubsub;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+28
-1
@@ -1,7 +1,15 @@
|
||||
const schema = require('./schema');
|
||||
const Context = require('./context');
|
||||
|
||||
const {connectionOptions} = require('../services/redis');
|
||||
const {RedisPubSub} = require('graphql-redis-subscriptions');
|
||||
const {SubscriptionManager} = require('graphql-subscriptions');
|
||||
const {SubscriptionServer} = require('subscriptions-transport-ws');
|
||||
|
||||
const pubsub = new RedisPubSub(connectionOptions);
|
||||
|
||||
module.exports = {
|
||||
pubsub,
|
||||
createGraphOptions: (req) => ({
|
||||
|
||||
// Schema is created already, so just include it.
|
||||
@@ -9,6 +17,25 @@ module.exports = {
|
||||
|
||||
// Load in the new context here, this'll create the loaders + mutators for
|
||||
// the lifespan of this request.
|
||||
context: new Context(req)
|
||||
context: new Context(req, pubsub)
|
||||
}),
|
||||
createSubscriptionManager: (server, path) => new SubscriptionServer({
|
||||
subscriptionManager: new SubscriptionManager({
|
||||
schema,
|
||||
pubsub,
|
||||
setupFunctions: {
|
||||
commentAdded: (options, args) => ({
|
||||
commentAdded: {
|
||||
filter: (comment) => comment.asset_id === args.asset_id
|
||||
},
|
||||
}),
|
||||
}
|
||||
}),
|
||||
// onConnect: (connectionParams, webSocket) => {
|
||||
// console.log(webSocket.upgradeReq.headers);
|
||||
// }
|
||||
}, {
|
||||
server,
|
||||
path
|
||||
})
|
||||
};
|
||||
|
||||
@@ -16,7 +16,7 @@ const Wordlist = require('../../services/wordlist');
|
||||
* @param {String} [status='NONE'] the status of the new comment
|
||||
* @return {Promise} resolves to the created comment
|
||||
*/
|
||||
const createComment = ({user, loaders: {Comments}}, {body, asset_id, parent_id = null}, status = 'NONE') => {
|
||||
const createComment = ({user, loaders: {Comments}, pubsub}, {body, asset_id, parent_id = null}, status = 'NONE') => {
|
||||
|
||||
let tags = [];
|
||||
if (user.hasRoles('ADMIN') || user.hasRoles('MODERATOR')) {
|
||||
@@ -44,6 +44,9 @@ const createComment = ({user, loaders: {Comments}}, {body, asset_id, parent_id =
|
||||
Comments.parentCountByAssetID.incr(asset_id);
|
||||
}
|
||||
Comments.countByAssetID.incr(asset_id);
|
||||
|
||||
// Publish the newly added comment via the subscription.
|
||||
pubsub.publish('commentAdded', comment);
|
||||
}
|
||||
|
||||
return comment;
|
||||
|
||||
@@ -16,6 +16,7 @@ const LikeAction = require('./like_action');
|
||||
const RootMutation = require('./root_mutation');
|
||||
const RootQuery = require('./root_query');
|
||||
const Settings = require('./settings');
|
||||
const Subscription = require('./subscription');
|
||||
const UserError = require('./user_error');
|
||||
const User = require('./user');
|
||||
const ValidationUserError = require('./validation_user_error');
|
||||
@@ -39,6 +40,7 @@ let resolvers = {
|
||||
RootMutation,
|
||||
RootQuery,
|
||||
Settings,
|
||||
Subscription,
|
||||
UserError,
|
||||
User,
|
||||
ValidationUserError,
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
const Subscription = {
|
||||
commentAdded(comment) {
|
||||
return comment;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = Subscription;
|
||||
@@ -706,6 +706,14 @@ type RootMutation {
|
||||
removeCommentTag(id: ID!, tag: String!): RemoveCommentTagResponse
|
||||
}
|
||||
|
||||
################################################################################
|
||||
## Subscriptions
|
||||
################################################################################
|
||||
|
||||
type Subscription {
|
||||
commentAdded(asset_id: ID!): Comment
|
||||
}
|
||||
|
||||
################################################################################
|
||||
## Schema
|
||||
################################################################################
|
||||
@@ -713,4 +721,5 @@ type RootMutation {
|
||||
schema {
|
||||
query: RootQuery
|
||||
mutation: RootMutation
|
||||
subscription: Subscription
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user