I have a apollo resovler to connect a user to a sesion:
connectUserToSession: async (_, args) => {
const session = await prisma.session.update({
where: {
id: Number(args.sessionId)
},
data: {
players: {
connect: {
id: Number(args.userId)
}
}
},
include: {
players: true,
host: true
}
})
pubsub.publish('USER_JOINED_SESSION', { userJoinedSession: session })
return session
},
When this resolves a user is successfully connected to a session and the publish
is also fired.
But the userJoinedSession
subscription doesn’t work as expected:
userJoinedSession: {
subscribe: withFilter(
() => pubsub.asyncIterator('USER_JOINED_SESSION'),
(payload, variables) => {
console.log('/// START SUBSCRIPTION ////')
console.log(payload)
console.log('///')
console.log(variables)
console.log('/// END SUBSCRIPTION ////')
},
),
}
As you can see in the logs below the payload
does have the info regarding the joined session, but the variables
value is an empty object.
/// START SUBSCRIPTION ////
{
userJoinedSession: {
id: 671,
name: 'Talk about dogs',
code: '878614',
userId: 690,
players: [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object]
],
host: { id: 690, name: 'asdasdas', messageId: null, sessionId: 671 }
}
}
///
{}
/// END SUBSCRIPTION ////
/// START SUBSCRIPTION ////
{
userJoinedSession: {
id: 671,
name: 'Talk about dogs',
code: '878614',
userId: 690,
players: [
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object],
[Object], [Object], [Object]
],
host: { id: 690, name: 'asdasdas', messageId: null, sessionId: 671 }
}
}
///
{}
/// END SUBSCRIPTION ////
variables is an object containing all arguments the client provided when initiating their subscription.
So it looks like no arguments are provided when this subscription is initiated, but the docs don’t go in depth into how this should be done.
The goal of the code is to only show a notification to users who are connected to the session a new user joined.