当我在 ApolloGraphQL 中使用 TypeGraphQL 订阅时出错:UnsupportedProtocolError: Unsupported protocol "ws:&quo

Posted

技术标签:

【中文标题】当我在 ApolloGraphQL 中使用 TypeGraphQL 订阅时出错:UnsupportedProtocolError: Unsupported protocol "ws:"【英文标题】:Error when I use subscriptions in ApolloGraphQL with TypeGraphQL: UnsupportedProtocolError: Unsupported protocol "ws:" 【发布时间】:2021-12-13 03:34:59 【问题描述】:

我根本不知道发生了什么,有人可以帮助我吗?查询和突变就像一个魅力,但订阅不是。我需要在我的电脑上配置一些东西吗?我从未使用过 WebSocket,谢谢!

【问题讨论】:

同样你找到解决办法了吗? 【参考方案1】:

Apollo Server 3 移除了对订阅的内置支持。

从 Apollo Server 3 开始,"batteries-included" apollo-server package 不支持订阅。要启用订阅,您必须先切换到apollo-server-express package(或任何其他支持订阅的 Apollo Server integration package)。

import 'dotenv/config';
import cors from 'cors';
import  createServer  from "http";
import  execute, subscribe  from "graphql";
import  SubscriptionServer  from "subscriptions-transport-ws";
import  makeExecutableSchema  from "@graphql-tools/schema";
import  PubSub  from 'graphql-subscriptions';
import express from "express";
import  ApolloServer  from "apollo-server-express";
import resolvers from "./resolvers";
import typeDefs from "./typeDefs";

(async function () 
    const app = express();
    const pubsub = new PubSub();

    app.use(cors());

    app.use(express.json());
    app.use(express.urlencoded(
        extended: true
    ));

    // Routes
    app.get('/heathcheck', (req, res) => 
        res.status(200).send('heath check OK');
    );

    //Required logic for integrating with Express
    const httpServer = createServer(app);

    const schema = makeExecutableSchema(
        typeDefs,
        resolvers,
    );

    // Same ApolloServer initialization as before, plus the drain plugin.
    const server = new ApolloServer(
        typeDefs,
        resolvers,
        context: ( req, res : any) => ( req, res, pubsub ),
        schema,
        plugins: [
            async serverWillStart() 
                return 
                    async drainServer() 
                        subscriptionServer.close();
                    
                ;
            
        ],
    );
    const subscriptionServer = SubscriptionServer.create(
         schema, execute, subscribe ,
         server: httpServer, path: server.graphqlPath 
    );
    //// More required logic for integrating with Express
    await server.start();
    server.applyMiddleware(
        app,
    
        // By default, apollo-server hosts its GraphQL endpoint at the
        // server root. However, *other* Apollo Server packages host it at
        // /graphql. Optionally provide this to match apollo-server.
        path: '/',
        cors: false,
      );

    const PORT = Number(process.env.app_port) || 3030;
    // Modified server startup
    httpServer.listen(PORT, () =>
        console.log(`Server is now running on http://localhost:$PORT/graphql`)
    );
)();

完整项目:https://github.com/sun1211/graphql

【讨论】:

这根本没用,伙计。您刚刚从文档中复制粘贴。 你试过@Oliv吗?这个项目运行没有任何错误?你的 apollo 服务器是哪个版本的?我认为没有人愿意帮助您了解这些信息

以上是关于当我在 ApolloGraphQL 中使用 TypeGraphQL 订阅时出错:UnsupportedProtocolError: Unsupported protocol "ws:&quo的主要内容,如果未能解决你的问题,请参考以下文章

删除突变后不重新获取查询(Apollo Graphql & Nextjs)

ApolloGraphQL:找不到 schema.json。请明确指定。Android Studio

com.apollographql.apollo.exception.ApolloHttpException:HTTP 500

ApolloGraphql FileNotFoundException: source/apollo/generatedIR/debug/src (是一个目录)

Apollo GraphQL - 是不是可以执行请求不同字段的相同查询?

为啥 NextJS 中的 ApolloGraphQL 示例使用 getStaticProps 而不是 getServerSideProps