使用subscriptions-transport-ws设置Apollo Server?

Vik*_*ikR 6 apollo apollo-server apollostack

看来我根据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
Run Code Online (Sandbox Code Playgroud)

它打印出"GraphQL Server现在在http:// localhost:8080/graphql上运行 "到终端日志,表明服务器已成功初始化.

但是在我的main_layout组件的顶部,当我运行此代码时:

import { Client } from 'subscriptions-transport-ws';
const wsClient = new Client('ws://localhost:8080');
Run Code Online (Sandbox Code Playgroud)

...我收到此控制台消息:

与'ws:// localhost:8080 /'的WebSocket连接失败:在收到握手响应之前连接已关闭

我错过了什么?

dav*_*aha 4

您需要创建一个专用的 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