From 333e201e5ef1a4734a0f0a9b0b144c633ffc0a9f Mon Sep 17 00:00:00 2001 From: Mohab Yaser Date: Mon, 25 Mar 2024 21:47:02 +0200 Subject: [PATCH] Created a subscription to check when a user's status change --- graphql/resolvers.js | 12 ++++++++++++ graphql/schema.js | 1 + 2 files changed, 13 insertions(+) diff --git a/graphql/resolvers.js b/graphql/resolvers.js index 3714d37..4c69222 100644 --- a/graphql/resolvers.js +++ b/graphql/resolvers.js @@ -9,6 +9,7 @@ const Group = require("../models/group.js"); const Landmark = require("../models/landmark.js"); const { User } = require("../models/user.js"); const { MongoServerError } = require("mongodb"); +const pubsub = require("../pubsub.js"); const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // even if we generate 10 IDs per hour, @@ -73,7 +74,10 @@ const resolvers = { password: await bcrypt.hash(credentials.password, 10), }), }); + const userObj = await newUser.save(); + pubsub.publish("USER_STATUS_CHANGED", { userId: user.id, user }); + return userObj; }, @@ -98,6 +102,8 @@ const resolvers = { anon = false; } + pubsub.publish("USER_STATUS_CHANGED", { userId: user.id, user }); + return jwt.sign( { "https://beacon.ccextractor.org": { @@ -314,6 +320,12 @@ const resolvers = { (payload, variables) => payload.groupID === variables.groupID ), }, + userStatusChanged: { + subscribe: withFilter( + (_, __, { pubsub }) => pubsub.asyncIterator(["USER_STATUS_CHANGED"]), + (payload, variables) => payload.userId === variables.userId + ), + }, }, }), }; diff --git a/graphql/schema.js b/graphql/schema.js index c0d0662..2948cde 100644 --- a/graphql/schema.js +++ b/graphql/schema.js @@ -129,6 +129,7 @@ const typeDefs = gql` userLocation(id: ID!): User beaconJoined(id: ID!): User groupJoined(groupID: ID!): User + userStatusChanged(userId: ID!): User } schema {