jli*_*ann 3 subscription websocket express apollo nuxt.js
我已经设法在 Nuxtjs 中拥有一个 Express + Apollo 后端作为服务器中间件。一切工作正常(身份验证、缓存、数据源、查询、突变),但现在我试图让订阅(websockets)运行,这给我带来了困难。
我尝试了这个示例https://www.apollographql.com/docs/apollo-server/data/subscriptions/#subscriptions-with-additional-middleware但即使让 httpServer 监听也不起作用。
这是我通过 nuxt.config.js 需要的 API 文件'~/api/index'
:
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
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的游乐场给了我这个错误:"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
}
Run Code Online (Sandbox Code Playgroud)
解析器:
type Subscription {
postAdded: Post
}
type Post {
author: String
comment: String
}
type Query {
posts: [Post]
}
type Mutation {
addPost(author: String, comment: String): Post
}
Run Code Online (Sandbox Code Playgroud)
第一个问题,先谢谢你了!:)
小智 5
也可以更容易一点
1.
yarn add apollo-server-express
Run Code Online (Sandbox Code Playgroud)
或者
npm install apollo-server-express
Run Code Online (Sandbox Code Playgroud)
yarn add apollo-server-express
Run Code Online (Sandbox Code Playgroud)
npm install apollo-server-express
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2411 次 |
最近记录: |