【发布时间】: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。