Added support to Subscriptions

This commit is contained in:
Douglas Barone 2020-12-03 17:27:03 -04:00
parent 3f1355c8b8
commit fea7befa8a
9 changed files with 20729 additions and 27 deletions

7
server/src/pubsub.js Normal file
View 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 }

View 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 }

View File

@ -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,

View File

@ -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 ', '')

View File

@ -16,10 +16,16 @@ const server = new ApolloServer({
: true,
typeDefs,
resolvers,
subscriptions: {
onConnect: connectionParams => ({
authorization: connectionParams.authorization
})
},
schemaDirectives,
context: ({ req }) => {
context: ({ req, connection }) => {
return {
ad,
connection,
req
}
},

View File

@ -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"

View File

@ -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
@ -65,7 +67,10 @@ async function updateDevicesInfo() {
working = false
}, DEBOUNCE_TIME_MS)
// TODO: pubsub
pubsub.publish(USER_PRESENCE_UPDATED, {
userPresenceUpdated: onlineDevices.length
})
return onlineDevices.length
} catch (e) {
console.log('Error updating DB: ', e)

20711
web/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -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",