Skip to content

Commit 918571b

Browse files
authored
Merge pull request #42 from ric2k1/version-upgrade
2 parents 6f147cb + f074a19 commit 918571b

File tree

8 files changed

+6737
-10428
lines changed

8 files changed

+6737
-10428
lines changed

backend/package.json

+13-9
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111
"author": "",
1212
"license": "ISC",
1313
"dependencies": {
14-
"graphql-yoga": "^1.17.4"
14+
"graphql": "^16.6.0",
15+
"graphql-ws": "^5.11.2",
16+
"graphql-yoga": "^3.1.1",
17+
"ws": "^8.11.0"
1518
},
1619
"devDependencies": {
17-
"@babel/cli": "^7.8.4",
18-
"@babel/core": "^7.9.6",
19-
"@babel/node": "^7.8.7",
20-
"@babel/plugin-proposal-class-properties": "^7.8.3",
21-
"@babel/plugin-proposal-object-rest-spread": "^7.9.6",
22-
"@babel/plugin-transform-arrow-functions": "^7.8.3",
23-
"@babel/preset-env": "^7.9.6",
24-
"nodemon": "^2.0.4"
20+
"@babel/cli": "^7.19.3",
21+
"@babel/core": "^7.20.5",
22+
"@babel/node": "^7.20.5",
23+
"@babel/plugin-proposal-class-properties": "^7.18.6",
24+
"@babel/plugin-proposal-object-rest-spread": "^7.20.2",
25+
"@babel/plugin-transform-arrow-functions": "^7.18.6",
26+
"@babel/preset-env": "^7.20.2",
27+
"nodemon": "^2.0.20",
28+
"uuidv4": "^6.2.13"
2529
}
2630
}

backend/src/index.js

+69-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { GraphQLServer, PubSub } from 'graphql-yoga';
1+
import { createPubSub, createSchema, createYoga } from 'graphql-yoga'
2+
import { createServer } from 'node:http'
3+
import { useServer } from 'graphql-ws/lib/use/ws'
4+
import { WebSocketServer } from 'ws'
5+
import * as fs from 'fs'
26
import db from './db';
37
import Query from './resolvers/Query';
48
import Mutation from './resolvers/Mutation';
@@ -7,24 +11,74 @@ import User from './resolvers/User';
711
import Post from './resolvers/Post';
812
import Comment from './resolvers/Comment';
913

10-
const pubsub = new PubSub();
11-
12-
const server = new GraphQLServer({
13-
typeDefs: './src/schema.graphql',
14-
resolvers: {
15-
Query,
16-
Mutation,
17-
Subscription,
18-
User,
19-
Post,
20-
Comment,
21-
},
14+
const pubsub = createPubSub();
15+
16+
const yoga = createYoga({
17+
schema: createSchema({
18+
typeDefs: fs.readFileSync(
19+
'./src/schema.graphql',
20+
'utf-8'
21+
),
22+
resolvers: {
23+
Query,
24+
Mutation,
25+
Subscription,
26+
User,
27+
Post,
28+
Comment,
29+
},
30+
}),
2231
context: {
2332
db,
2433
pubsub,
2534
},
35+
// graphqlEndpoint: '/', // uncomment this to send the app to: 4000/
36+
graphiql: {
37+
subscriptionsProtocol: 'WS',
38+
},
2639
});
2740

28-
server.start({ port: process.env.PORT | 5000 }, () => {
29-
console.log(`The server is up on port ${process.env.PORT | 5000}!`);
41+
const server = createServer(yoga)
42+
43+
const wsServer = new WebSocketServer({
44+
server: server,
45+
path: yoga.graphqlEndpoint,
46+
})
47+
48+
useServer(
49+
{
50+
execute: (args) => args.rootValue.execute(args),
51+
subscribe: (args) => args.rootValue.subscribe(args),
52+
onSubscribe: async (ctx, msg) => {
53+
const { schema, execute, subscribe, contextFactory, parse, validate } =
54+
yoga.getEnveloped({
55+
...ctx,
56+
req: ctx.extra.request,
57+
socket: ctx.extra.socket,
58+
params: msg.payload
59+
})
60+
61+
const args = {
62+
schema,
63+
operationName: msg.payload.operationName,
64+
document: parse(msg.payload.query),
65+
variableValues: msg.payload.variables,
66+
contextValue: await contextFactory(),
67+
rootValue: {
68+
execute,
69+
subscribe
70+
}
71+
}
72+
73+
const errors = validate(args.schema, args.document)
74+
if (errors.length) return errors
75+
return args
76+
},
77+
},
78+
wsServer,
79+
)
80+
81+
const port = process.env.PORT || 4000;
82+
server.listen({port}, () => {
83+
console.log(`The server is up on port ${port}!`);
3084
});

backend/src/resolvers/Mutation.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import uuidv4 from 'uuid/v4';
1+
import {v4 as uuidv4} from 'uuid';
22

33
const Mutation = {
44
createUser(parent, args, { db }, info) {

backend/src/resolvers/Subscription.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ const Subscription = {
99
throw new Error('Post not found');
1010
}
1111

12-
return pubsub.asyncIterator(`comment ${postId}`);
12+
return pubsub.subscribe(`comment ${postId}`);
1313
},
1414
},
1515
post: {
1616
subscribe(parent, args, { pubsub }, info) {
17-
return pubsub.asyncIterator('post');
17+
return pubsub.subscribe('post');
1818
},
1919
},
2020
};

0 commit comments

Comments
 (0)