0
我正在尝试通过firebase函数运行graphql订阅,我进行了以下设置:
import * as functions from 'firebase-functions'; import { ApolloServer, gql } from 'apollo-server-express';
import { PubSub } from 'apollo-server-express';
const pubsub = new PubSub();
const typeDefs = gql` type Post { author: String comment: String } type Query { hello: String posts: [Post] } type Subscription { postAdded: Post }
type Mutation { addPost(author: String, comment: String): Post }
`;
const POST_ADDED = 'POST_ADDED';
class Poster { public posts() { return [] }
public addPost(args: any) {
}
}
const postController = new Poster();
const resolvers = { Subscription: { postAdded: { // Additional event labels can be passed to asyncIterator creation subscribe: () => pubsub.asyncIterator([POST_ADDED]), }, }, Query: { hello: () => 'Hello world!', posts(root: any, args: any, context: any) { return postController.posts(); }, }, Mutation: { addPost(root: any, args: any, context: any) { pubsub.publish(POST_ADDED, { postAdded: args }); return postController.addPost(args); }, },
};
const server = new ApolloServer({ typeDefs, resolvers, subscriptions: { onConnect: (connectionParams, webSocket) => {
},
},
});
const express = require("express")
/* Express */ const app = express() server.applyMiddleware({ app });
app.get("*", (request:any, response:any) => { response.send("Hello from Express on Firebase!") }) app.listen({ port: 4000 }, () => console.log( Server ready at http://localhost:4000${server.graphqlPath}
) )
const api = functions.https.onRequest(app)
module.exports = { api } 当我尝试使用游乐场通过此查询进行连接时:
subscription { posts { author comment } } 我收到一个错误:
"error": "Could not connect to websocket endpoint ws://localhost:5001/graphql. Please check if the endpoint url is correct." 我是否在exprses服务器中缺少某些配置,或者firebase不能正确支持套接字?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。