Node.js 订阅URL不';t在Apollo Graphql订阅服务器中工作
我有一个graphql服务器,它使用apollo server express。下面是基本graphql请求-响应模式的代码Node.js 订阅URL不';t在Apollo Graphql订阅服务器中工作,node.js,graphql,apollo,Node.js,Graphql,Apollo,我有一个graphql服务器,它使用apollo server express。下面是基本graphql请求-响应模式的代码 const server = new ApolloServer({ typeDefs, resolvers, context: async ({ connection }) => { if (connection) { return { ...connection.context, }; }
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({ connection }) => {
if (connection) {
return {
...connection.context,
};
}
return null;
},
});
server.applyMiddleware({ app, path: '/graphql' });
它很好用。您可以看到它使用applyMiddleware
来使用express server。我能够在http://localhost:8080/graphql
url并从那里测试它。后来我在代码中添加了订阅支持:
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游乐场端点的选项被传递给ApolloServer构造函数,如图所示
可用选项如图所示。请注意,GraphQL游乐场基于GraphiQL,但它们不是一回事。Apollo服务器过去包含GraphiQL端点,但现在改用Playerd。
const server = new ApolloServer({
playground: {
subscriptionEndpoint: 'your custom subscription endpoint',
// other GraphQL Playground options
},
...
});