lucas
10/15/2020, 2:28 PMexport const ChannelSubscription = subscriptionField('channel', {
type: 'Channel',
subscribe(_root, _args, ctx) {
return ctx.pubsub.asyncIterator('channel')
},
resolve(payload) {
return payload
},
})
t.field('createChannel', {
type: 'Channel',
nullable: true,
args: {
receiverId: stringArg({ nullable: false }),
},
resolve: async (_parent, { receiverId }, ctx) => {
const userId = getUserId(ctx)
const channel = await ctx.prisma.channel.create({
data: {
users: {
connect: [{ id: userId }, { id: receiverId }],
},
visibles: {
create: [{ toUserId: userId }],
},
},
})
ctx.pubsub.publish('channel', channel)
return channel
},
})
Will Fischer
10/15/2020, 2:38 PM...
subscribe(_root, _args, ctx) {
return ctx.pubsub.asyncIterator('channel')
},
resolve(_root, _args, ctx) {
const userId = getUserId(ctx)
return ctx.db.channel.findMany({ where: { visibles: {toUserId: userId} } });
}
You can also, of course, just return the latest one, or conditionally return the parent if it’s userId matches the authenticated userWill Fischer
10/15/2020, 2:39 PMWill Fischer
10/15/2020, 2:43 PMlucas
10/15/2020, 2:51 PMWill Fischer
10/15/2020, 2:54 PMt.field
section is part of your subscriptionfield, but should probably a mutation extension type. Is that just an artifact of how you pasted it in?Will Fischer
10/15/2020, 2:55 PM