gpt4 book ai didi

apollo - 设置带有subscriptions-transport-ws的Apollo服务器?

转载 作者:行者123 更新时间:2023-12-04 13:47:16 26 4
gpt4 key购买 nike

看来我已经根据http://dev.apollodata.com/tools/apollo-server/setup.html上的Apollo文档设置了服务器。在我的server/main.js文件中:

//SET UP APOLLO INCLUDING APOLLO PUBSUB
const executableSchema = makeExecutableSchema({
typeDefs: Schema,
resolvers: Resolvers,
connectors: Connectors,
logger: console,
});

const GRAPHQL_PORT = 8080;
const graphQLServer = express();

// `context` must be an object and can't be undefined when using connectors
graphQLServer.use('/graphql', bodyParser.json(), apolloExpress({
schema: executableSchema,
context: {}, //at least(!) an empty object
}));

graphQLServer.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
}));

graphQLServer.listen(GRAPHQL_PORT, () => console.log(
`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`
));
//SET UP APOLLO INCLUDING APOLLO PUBSUB

它在终端日志中打印出“GraphQL Server现在正在 http://localhost:8080/graphql上运行”,指示服务器已成功初始化。

但是,当我运行此代码时,在main_layout组件的顶部:
import { Client } from 'subscriptions-transport-ws';
const wsClient = new Client('ws://localhost:8080');

...我收到以下控制台消息:

WebSocket connection to 'ws://localhost:8080/' failed: Connection closed before receiving a handshake response



我想念什么?

最佳答案

您需要创建一个专用的websocket服务器。它将在其他端口上运行,并且在subscriptions-transport-ws软件包中提供了用于设置它的代码。

看一下GitHunt-API示例中的以下代码:
https://github.com/apollostack/GitHunt-API/blob/master/api/index.js#L101-L134

您还将看到此代码依赖于称为SubscriptionManager的类。它也是apollo团队提供的名为graphql-subscriptions的包中的类,您可以在此处找到如何使用它的示例:
https://github.com/apollostack/GitHunt-API/blob/master/api/subscriptions.js

关于apollo - 设置带有subscriptions-transport-ws的Apollo服务器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765513/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com