订阅未使用 ApolloServer 连接

Subscription not connecting using ApolloServer

我正在尝试订阅 运行 ApolloServer (v 2.2.2)。我有一个突然停止工作的设置。当我尝试连接到 graphiql/Playground 中的订阅时,出现错误:

{
  "error": "Could not connect to websocket endpoint ws://localhost:4000/graphql. Please check if the endpoint url is correct."
}

因为我的应用程序中有 rest-endpoints 我需要 express 但我无法从下面得到最小的例子 运行:

import http from 'http';
import { ApolloServer, PubSub } from 'apollo-server-express';
import express from 'express';

const pubsub = new PubSub();

// The DB
const messages = [];

const typeDefs = `
type Query {
  messages: [String!]!
}
type Mutation {
  addMessage(message: String!): [String!]!
}
type Subscription {
  newMessage: String!
}

schema {
  query: Query
  mutation: Mutation
  subscription: Subscription
}
`;

const resolvers = {
  Query: {
    messages() {
      return messages;
    }
  },
  Mutation: {
    addMessage(root, { message }) {
      let entry = JSON.stringify({ id: messages.length, message: message });
      messages.push(entry);
      pubsub.publish('newMessage', { entry: entry });
      return messages;
    },
  },
  Subscription: {
    newMessage: {
      resolve: (message) => {
        return message.entry;
      },
      subscribe: () => pubsub.asyncIterator('newMessage'),
    },
  },
};

const app = express();

const PORT = 4000;

const server = new ApolloServer({
  typeDefs,
  resolvers,
  subscriptions: {
    onConnect: () => console.log('Connected to websocket'),
  }
});

server.applyMiddleware({ app })

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);

httpServer.listen(PORT, () => {
  console.log(` Server ready at http://localhost:${PORT}${server.graphqlPath}`)
  console.log(` Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`)
})

其他端点工作正常,但无法创建 WebSocket。据我了解,我不应该使用不同的服务器或端口(参见 https://www.ably.io/concepts/websockets). I've tinkered with SubsciptionServer but this should be handled by installSubscriptionHandlers (here's the code)。

事实证明 Firefox 存在 websockets 问题(请参阅 this bug report 即使在假定的修复之后也重新出现)。

在 Firefox 中,它在启动一个新的浏览器后可以直接工作,但在一些热重载后它停止工作。以下内容有助于重新开始,但不会解决重新加载问题:

const wsLink = new WebSocketLink({
  uri: SUBSCRIPTION_URI,
  options: {
    reconnect: true,
    timeout: 20000,
    lazy: true,
  },
});

window.addEventListener('beforeunload', () => {
  // @ts-ignore - the function is private in typescript
  wsLink.subscriptionClient.close();
});

我认为这个错误与这个问题有关:"websocket was interrupted while page is loading" on Firefox for Socket.io

如果您想测试不同的解决方案,我创建了一个示例存储库:https://github.com/gforge/subscription_example 既可以单独使用,也可以与 Docker 容器一起使用。

很多时间过去了,现在我遇到了同样的问题,我找到了解决方案。

import { createServer } from 'http';
const app = express();
const server = new ApolloServer({});
server.applyMiddleware({ app });
const httpServer = createServer(app);
server.installSubscriptionHandlers(httpServer);
server.listen()

适合我