Added support to Subscriptions
This commit is contained in:
parent
3f1355c8b8
commit
fea7befa8a
7
server/src/pubsub.js
Normal file
7
server/src/pubsub.js
Normal file
|
@ -0,0 +1,7 @@
|
|||
import { PubSub } from 'apollo-server'
|
||||
|
||||
const USER_PRESENCE_UPDATED = 'USER_PRESENCE_UPDATED'
|
||||
|
||||
const pubsub = new PubSub()
|
||||
|
||||
export { pubsub, USER_PRESENCE_UPDATED }
|
11
server/src/resolvers/Subscriptions.js
Normal file
11
server/src/resolvers/Subscriptions.js
Normal file
|
@ -0,0 +1,11 @@
|
|||
import { pubsub, USER_PRESENCE_UPDATED } from '../pubsub'
|
||||
|
||||
import { updateDevicesInfo } from '../utils/wifiUtils'
|
||||
|
||||
const Subscription = {
|
||||
userPresenceUpdated: {
|
||||
subscribe: () => pubsub.asyncIterator([USER_PRESENCE_UPDATED])
|
||||
}
|
||||
}
|
||||
|
||||
export { Subscription }
|
|
@ -1,5 +1,6 @@
|
|||
import { Query } from './Query'
|
||||
import { Mutation } from './Mutation'
|
||||
import { Subscription } from './Subscriptions'
|
||||
|
||||
import { User } from './User'
|
||||
import { Group } from './Group'
|
||||
|
@ -9,6 +10,7 @@ import { WifiDevice } from './WifiDevice'
|
|||
const resolvers = {
|
||||
Query,
|
||||
Mutation,
|
||||
Subscription,
|
||||
User,
|
||||
Group,
|
||||
ResetToken,
|
||||
|
|
|
@ -15,7 +15,7 @@ class AuthDirective extends SchemaDirectiveVisitor {
|
|||
|
||||
const authorizationHeader = context.req
|
||||
? context.req.headers.authorization
|
||||
: context.connection.context.Authorization // TODO: check if work with subscriptions
|
||||
: context.connection.context.authorization
|
||||
|
||||
if (authorizationHeader) {
|
||||
const token = authorizationHeader.replace('Bearer ', '')
|
||||
|
|
|
@ -16,13 +16,19 @@ const server = new ApolloServer({
|
|||
: true,
|
||||
typeDefs,
|
||||
resolvers,
|
||||
subscriptions: {
|
||||
onConnect: connectionParams => ({
|
||||
authorization: connectionParams.authorization
|
||||
})
|
||||
},
|
||||
schemaDirectives,
|
||||
context: ({ req }) => {
|
||||
context: ({ req, connection }) => {
|
||||
return {
|
||||
ad,
|
||||
connection,
|
||||
req
|
||||
}
|
||||
},
|
||||
},
|
||||
plugins: [
|
||||
responseCachePlugin({
|
||||
sessionId: requestContext =>
|
||||
|
|
|
@ -46,6 +46,10 @@ const typeDefs = gql`
|
|||
updateUserIdMappings: String! @auth(roles: ["superAdmin"])
|
||||
}
|
||||
|
||||
type Subscription {
|
||||
userPresenceUpdated: Int! @auth(roles: ["watcher"])
|
||||
}
|
||||
|
||||
directive @auth(roles: [String!]) on FIELD_DEFINITION
|
||||
|
||||
"A mix between the database User and the Active Directory User"
|
||||
|
|
|
@ -3,6 +3,8 @@ import { getOnlineWifiDevices as getOnlineCiscoDevices } from './ciscoController
|
|||
|
||||
import prisma from '../prisma'
|
||||
|
||||
import { pubsub, USER_PRESENCE_UPDATED } from '../pubsub'
|
||||
|
||||
const DEBOUNCE_TIME_MS = 10000
|
||||
|
||||
let working = false
|
||||
|
@ -64,8 +66,11 @@ async function updateDevicesInfo() {
|
|||
setTimeout(() => {
|
||||
working = false
|
||||
}, DEBOUNCE_TIME_MS)
|
||||
|
||||
pubsub.publish(USER_PRESENCE_UPDATED, {
|
||||
userPresenceUpdated: onlineDevices.length
|
||||
})
|
||||
|
||||
// TODO: pubsub
|
||||
return onlineDevices.length
|
||||
} catch (e) {
|
||||
console.log('Error updating DB: ', e)
|
||||
|
|
20711
web/package-lock.json
generated
20711
web/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
|
@ -10,6 +10,8 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@mdi/font": "^5.6.55",
|
||||
"apollo-link-ws": "^1.0.20",
|
||||
"apollo-utilities": "^1.3.4",
|
||||
"core-js": "^3.6.5",
|
||||
"date-fns": "^2.16.1",
|
||||
"qrcode.vue": "^1.7.0",
|
||||
|
|
Loading…
Reference in New Issue
Block a user