【问题标题】:Subscription URL doesn't work in Apollo Graphql Subscription Server订阅 URL 在 Apollo Graphql 订阅服务器中不起作用
【发布时间】:2020-03-24 15:59:40
【问题描述】:

我有一个使用 apollo-server-express 的 graphql 服务器。下面是一个基本的graphql请求响应模式的代码。

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: async ({ connection }) => {
    if (connection) {
      return {
        ...connection.context,
      };
    }
    return null;
  },
});

server.applyMiddleware({ app, path: '/graphql' });

它工作正常。你可以看到它使用applyMiddleware 来使用快递服务器。我可以在http://localhost:8080/graphql url 上打开 graphiql 并从那里进行测试。后来我在我的代码中添加了订阅支持:

const { SubscriptionServer } = require('subscriptions-transport-ws');
const { createServer } = require('http');
const ws = createServer(app);
SubscriptionServer.create({...}, {
    server: ws,
    path: '/subscriptions',
  });

完成此操作后,我的应用功能可以正常工作,但我无法在 graphiql 操场上对其进行测试。它抱怨订阅 URL 总是http://localhost:8080/graphql 而不是http://localhost:8080/subscriptions

我发现有人说要使用这段代码:

app.use(‘/graphiql’, graphiqlExpress({
  endpointURL: ‘/graphql’,
  subscriptionsEndpoint: `ws://localhost:3000/subscriptions`,
}));

但我的应用正在使用applyMiddleware。我想知道如何使它适用于我的情况。

【问题讨论】:

  • 请注意,GraphQL Playground 基于 GraphiQL,但它们不是一回事。 Apollo Server 曾经包含一个 GraphiQL 端点,但现在改用 Playground。

标签: node.js graphql apollo


【解决方案1】:

GraphQL Playground 端点的选项被传递给 ApolloServer 构造函数,如 in the docs 所示。

const server = new ApolloServer({
  playground: {
    subscriptionEndpoint: 'your custom subscription endpoint',
    // other GraphQL Playground options
  },
  ...
});

可用选项显示在here

【讨论】:

    猜你喜欢
    • 2017-08-12
    • 2023-03-16
    • 2017-09-19
    • 2020-10-08
    • 2019-05-13
    • 2019-01-27
    • 2017-12-05
    • 2022-12-12
    • 2020-12-03
    相关资源
    最近更新 更多