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 { Query } from './Query'
|
||||||
import { Mutation } from './Mutation'
|
import { Mutation } from './Mutation'
|
||||||
|
import { Subscription } from './Subscriptions'
|
||||||
|
|
||||||
import { User } from './User'
|
import { User } from './User'
|
||||||
import { Group } from './Group'
|
import { Group } from './Group'
|
||||||
|
@ -9,6 +10,7 @@ import { WifiDevice } from './WifiDevice'
|
||||||
const resolvers = {
|
const resolvers = {
|
||||||
Query,
|
Query,
|
||||||
Mutation,
|
Mutation,
|
||||||
|
Subscription,
|
||||||
User,
|
User,
|
||||||
Group,
|
Group,
|
||||||
ResetToken,
|
ResetToken,
|
||||||
|
|
|
@ -15,7 +15,7 @@ class AuthDirective extends SchemaDirectiveVisitor {
|
||||||
|
|
||||||
const authorizationHeader = context.req
|
const authorizationHeader = context.req
|
||||||
? context.req.headers.authorization
|
? context.req.headers.authorization
|
||||||
: context.connection.context.Authorization // TODO: check if work with subscriptions
|
: context.connection.context.authorization
|
||||||
|
|
||||||
if (authorizationHeader) {
|
if (authorizationHeader) {
|
||||||
const token = authorizationHeader.replace('Bearer ', '')
|
const token = authorizationHeader.replace('Bearer ', '')
|
||||||
|
|
|
@ -16,10 +16,16 @@ const server = new ApolloServer({
|
||||||
: true,
|
: true,
|
||||||
typeDefs,
|
typeDefs,
|
||||||
resolvers,
|
resolvers,
|
||||||
|
subscriptions: {
|
||||||
|
onConnect: connectionParams => ({
|
||||||
|
authorization: connectionParams.authorization
|
||||||
|
})
|
||||||
|
},
|
||||||
schemaDirectives,
|
schemaDirectives,
|
||||||
context: ({ req }) => {
|
context: ({ req, connection }) => {
|
||||||
return {
|
return {
|
||||||
ad,
|
ad,
|
||||||
|
connection,
|
||||||
req
|
req
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -46,6 +46,10 @@ const typeDefs = gql`
|
||||||
updateUserIdMappings: String! @auth(roles: ["superAdmin"])
|
updateUserIdMappings: String! @auth(roles: ["superAdmin"])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Subscription {
|
||||||
|
userPresenceUpdated: Int! @auth(roles: ["watcher"])
|
||||||
|
}
|
||||||
|
|
||||||
directive @auth(roles: [String!]) on FIELD_DEFINITION
|
directive @auth(roles: [String!]) on FIELD_DEFINITION
|
||||||
|
|
||||||
"A mix between the database User and the Active Directory User"
|
"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 prisma from '../prisma'
|
||||||
|
|
||||||
|
import { pubsub, USER_PRESENCE_UPDATED } from '../pubsub'
|
||||||
|
|
||||||
const DEBOUNCE_TIME_MS = 10000
|
const DEBOUNCE_TIME_MS = 10000
|
||||||
|
|
||||||
let working = false
|
let working = false
|
||||||
|
@ -65,7 +67,10 @@ async function updateDevicesInfo() {
|
||||||
working = false
|
working = false
|
||||||
}, DEBOUNCE_TIME_MS)
|
}, DEBOUNCE_TIME_MS)
|
||||||
|
|
||||||
// TODO: pubsub
|
pubsub.publish(USER_PRESENCE_UPDATED, {
|
||||||
|
userPresenceUpdated: onlineDevices.length
|
||||||
|
})
|
||||||
|
|
||||||
return onlineDevices.length
|
return onlineDevices.length
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log('Error updating DB: ', 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": {
|
"dependencies": {
|
||||||
"@mdi/font": "^5.6.55",
|
"@mdi/font": "^5.6.55",
|
||||||
|
"apollo-link-ws": "^1.0.20",
|
||||||
|
"apollo-utilities": "^1.3.4",
|
||||||
"core-js": "^3.6.5",
|
"core-js": "^3.6.5",
|
||||||
"date-fns": "^2.16.1",
|
"date-fns": "^2.16.1",
|
||||||
"qrcode.vue": "^1.7.0",
|
"qrcode.vue": "^1.7.0",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user