【发布时间】:2020-05-13 01:55:12
【问题描述】:
我已经设法在 Nuxtjs 中拥有一个 express + Apollo 后端作为 serverMiddleware。 一切正常(身份验证、缓存、数据源、查询、突变),但现在我正试图让订阅(websockets)运行,这让我很难过。
我试过这个例子https://www.apollographql.com/docs/apollo-server/data/subscriptions/#subscriptions-with-additional-middleware,但即使让httpServer监听也没有用。
这是我通过 nuxt.config.js 使用 '~/api/index' 需要的 API 文件:
module.exports = async () => {
const app = require('express')()
const server = await require("./apollo")() // apollo-server-express w/ typeDefs and resolvers
// apply Apollo to Express
server.applyMiddleware({ app });
console.log(`???? ApolloServer ready at ${server.graphqlPath}`);
const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);
console.log(`???? ApolloSubscriptions ready at ${server.subscriptionsPath}`);
return {
path: '/api',
handler: httpServer
}
}
现在我的游乐场给了我这个错误:"Could not connect to websocket endpoint ws://192.168.150.98:3000/api/graphql. Please check if the endpoint url is correct."
类型定义:
type Subscription {
postAdded: Post
}
type Post {
author: String
comment: String
}
type Query {
posts: [Post]
}
type Mutation {
addPost(author: String, comment: String): Post
}
解析器:
Query: {
posts(root, args, context) {
return Posts;
}
}
Mutation: {
addPost(root, args, context) {
pubsub.publish(POST_ADDED, { postAdded: args });
return Posts.add(args);
}
},
Subscription: {
postAdded: {
// Additional event labels can be passed to asyncIterator creation
subscribe: () => pubsub.asyncIterator([POST_ADDED]),
},
}
这里的第一个问题,提前谢谢你! :)
【问题讨论】:
标签: express websocket nuxt.js apollo subscription