Skip to content

Commit 0ec1dd5

Browse files
committed
update subscriptions
1 parent 09ab6ed commit 0ec1dd5

File tree

6 files changed

+137
-121
lines changed

6 files changed

+137
-121
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"dependencies": {
1010
"@prisma/client": "^2.0.0-beta.2",
1111
"bcryptjs": "2.4.3",
12-
"graphql-yoga": "1.17.4",
12+
"graphql-yoga": "1.18.3",
1313
"jsonwebtoken": "8.5.1"
1414
},
1515
"devDependencies": {

prisma/dev.db

0 Bytes
Binary file not shown.

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ const Subscription = require('./resolvers/Subscription')
66
const User = require('./resolvers/User')
77
const Link = require('./resolvers/Link')
88
const Vote = require('./resolvers/Vote')
9+
const { PubSub } = require('graphql-yoga')
10+
11+
const pubsub = new PubSub()
912

1013
const prisma = new PrismaClient({
1114
errorFormat: 'minimal',
@@ -27,6 +30,7 @@ const server = new GraphQLServer({
2730
return {
2831
...request,
2932
prisma,
33+
pubsub
3034
}
3135
},
3236
})

src/resolvers/Mutation.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ const { APP_SECRET, getUserId } = require('../utils')
44

55
function post(parent, args, context, info) {
66
const userId = getUserId(context)
7-
return context.prisma.link.create({
7+
8+
const newLink = context.prisma.link.create({
89
data: {
910
url: args.url,
1011
description: args.description,
1112
postedBy: { connect: { id: userId } },
1213
}
1314
})
15+
context.pubsub.publish("NEW_LINK", newLink)
16+
17+
return newLink
1418
}
1519

1620
async function signup(parent, args, context, info) {
@@ -59,12 +63,15 @@ async function vote(parent, args, context, info) {
5963
throw new Error(`Already voted for link: ${args.linkId}`)
6064
}
6165

62-
return context.prisma.vote.create({
66+
const newVote = context.prisma.vote.create({
6367
data: {
6468
user: { connect: { id: userId } },
6569
link: { connect: { id: Number(args.linkId) } },
6670
}
6771
})
72+
context.pubsub.publish("NEW_VOTE", newVote)
73+
74+
return newVote
6875
}
6976

7077
module.exports = {

src/resolvers/Subscription.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
function newLinkSubscribe(parent, args, context, info) {
2-
return context.prisma.$subscribe.link({ mutation_in: ['CREATED'] }).node()
2+
return context.pubsub.asyncIterator("NEW_LINK")
33
}
44

55
const newLink = {
@@ -10,7 +10,7 @@ const newLink = {
1010
}
1111

1212
function newVoteSubscribe(parent, args, context, info) {
13-
return context.prisma.$subscribe.vote({ mutation_in: ['CREATED'] }).node()
13+
return context.pubsub.asyncIterator("NEW_VOTE")
1414
}
1515

1616
const newVote = {
@@ -22,5 +22,5 @@ const newVote = {
2222

2323
module.exports = {
2424
newLink,
25-
newVote,
26-
}
25+
newVote
26+
}

0 commit comments

Comments
 (0)