Skip to content

Commit 07052c4

Browse files
committed
fix: Vue CLI UI graphql subscription server error
Fixes #7221 `subscriptions-transport-ws` is also deprecated, we need to move to `graphql-ws` one day. But better deprecatedthan broken.
1 parent a5a893e commit 07052c4

File tree

3 files changed

+297
-215
lines changed

3 files changed

+297
-215
lines changed

packages/@vue/cli-ui/graphql-server.js

+46-17
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const { ApolloServer, gql } = require('apollo-server-express')
88
const { PubSub } = require('graphql-subscriptions')
99
const merge = require('deepmerge')
1010

11+
const { SubscriptionServer } = require('subscriptions-transport-ws')
12+
const { makeExecutableSchema } = require('@graphql-tools/schema')
13+
const { execute, subscribe } = require('graphql')
14+
1115
function defaultValue (provided, value) {
1216
return provided == null ? value : provided
1317
}
@@ -27,6 +31,7 @@ module.exports = async (options, cb = null) => {
2731

2832
// Express app
2933
const app = express()
34+
const httpServer = http.createServer(app)
3035

3136
// Customize those files
3237
let typeDefs = load(options.paths.typeDefs)
@@ -64,12 +69,16 @@ module.exports = async (options, cb = null) => {
6469

6570
typeDefs = processSchema(typeDefs)
6671

72+
// eslint-disable-next-line prefer-const
73+
let subscriptionServer
74+
6775
let apolloServerOptions = {
6876
typeDefs,
6977
resolvers,
7078
schemaDirectives,
7179
dataSources,
7280
tracing: true,
81+
cache: 'bounded',
7382
cacheControl: true,
7483
engine: !options.integratedEngine,
7584
// Resolvers context from POST
@@ -89,23 +98,15 @@ module.exports = async (options, cb = null) => {
8998
return contextData
9099
},
91100
// Resolvers context from WebSocket
92-
subscriptions: {
93-
path: options.subscriptionsPath,
94-
onConnect: async (connection, websocket) => {
95-
let contextData = {}
96-
try {
97-
contextData = await autoCall(context, {
98-
connection,
99-
websocket
100-
})
101-
contextData = Object.assign({}, contextData, { pubsub })
102-
} catch (e) {
103-
console.error(e)
104-
throw e
101+
plugins: [{
102+
async serverWillStart () {
103+
return {
104+
async drainServer () {
105+
subscriptionServer.close()
106+
}
105107
}
106-
return contextData
107108
}
108-
}
109+
}]
109110
}
110111

111112
// Automatic mocking
@@ -146,6 +147,36 @@ module.exports = async (options, cb = null) => {
146147

147148
// Apollo Server
148149
const server = new ApolloServer(apolloServerOptions)
150+
151+
const schema = makeExecutableSchema({
152+
typeDefs: apolloServerOptions.typeDefs,
153+
resolvers: apolloServerOptions.resolvers,
154+
schemaDirectives: apolloServerOptions.schemaDirectives
155+
})
156+
157+
subscriptionServer = SubscriptionServer.create({
158+
schema,
159+
execute,
160+
subscribe,
161+
onConnect: async (connection, websocket) => {
162+
let contextData = {}
163+
try {
164+
contextData = await autoCall(context, {
165+
connection,
166+
websocket
167+
})
168+
contextData = Object.assign({}, contextData, { pubsub })
169+
} catch (e) {
170+
console.error(e)
171+
throw e
172+
}
173+
return contextData
174+
}
175+
}, {
176+
server: httpServer,
177+
path: options.subscriptionsPath
178+
})
179+
149180
await server.start()
150181

151182
// Express middleware
@@ -160,9 +191,7 @@ module.exports = async (options, cb = null) => {
160191
})
161192

162193
// Start server
163-
const httpServer = http.createServer(app)
164194
httpServer.setTimeout(options.timeout)
165-
server.installSubscriptionHandlers(httpServer)
166195

167196
httpServer.listen({
168197
host: options.host || 'localhost',

packages/@vue/cli-ui/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"@achrinza/node-ipc": "^9.2.5",
3838
"@akryum/winattr": "^3.0.0",
39+
"@graphql-tools/schema": "^8.5.0",
3940
"@vue/cli-shared-utils": "^5.0.7",
4041
"apollo-server-express": "^3.9.0",
4142
"clone": "^2.1.1",
@@ -60,6 +61,7 @@
6061
"prismjs": "^1.23.0",
6162
"rss-parser": "^3.11.0",
6263
"shortid": "^2.2.15",
64+
"subscriptions-transport-ws": "^0.11.0",
6365
"typescript": "~4.5.5"
6466
},
6567
"devDependencies": {
@@ -91,7 +93,6 @@
9193
"start-server-and-test": "^1.12.0",
9294
"stylus": "^0.55.0",
9395
"stylus-loader": "^6.1.0",
94-
"subscriptions-transport-ws": "^0.9.18",
9596
"validate-npm-package-name": "^3.0.0",
9697
"vue": "^2.6.14",
9798
"vue-apollo": "^3.0.7",

0 commit comments

Comments
 (0)