1

我使用 Apollo Server、Sequelize(用于 ORM)、MySQL(DB)和 Express(Web 服务器)制作了一个 GraphQL 后端。

我还添加了订阅,问题就在那里。我什至无法使用 websocket 测试器到达 WS 端点。

有人可以查看我的代码并告诉我问题所在吗?我查看了文档和其他 stackoverflow 问题,但找不到任何解决方案。

代码:https ://github.com/seklyza/graphqlsubscriptions

谢谢大家

4

2 回答 2

0

我认为您必须为使用express服务器的应用程序和 websocket 的应用程序制作 2 个服务器。它可能看起来像这样。

GraphQL 快递服务器:

...

graphQLServer = express();

const GRAPHQL_PORT = 4000;

graphQLServer.use('/graphql', bodyParser.json(), graphqlExpress((request) => {
  return {
    schema: executableSchema,
  };
}));

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

graphQLServer.listen(GRAPHQL_PORT, () => {
  console.log(`GraphQL Server is now running on http://localhost:${GRAPHQL_PORT}/graphql`); // eslint-disable-line no-console
});

...

用于订阅的 websocket 服务器:

...

const WS_PORT = 8080;

const websocketServer = createServer((request, response) => {
  response.writeHead(404);
  response.end();
});

websocketServer.listen(WS_PORT, () => console.log( // eslint-disable-line no-console
  `Websocket Server is now running on http://localhost:${WS_PORT}`
));

const subscriptionManager = new SubscriptionManager({
  schema: executableSchema,
  pubsub: pubsub,
  setupFunctions: { /* your subscription channels */ },
});

subscriptionServer = new SubscriptionServer({
  subscriptionManager: subscriptionManager
}, {
  server: websocketServer,
  path: '/',
});

...

您需要某种出版物订阅服务,我们使用pubSub. 它包含在服务器文件中,如下所示:

import {
  PubSub
} from 'graphql-subscriptions';

const pubsub = new PubSub();

export {
  pubsub
};

于 2017-03-21T09:50:03.730 回答
0

您可以创建一些 Web 套接字服务器包装器,该包装器实现start负责创建和运行 的方法WSServer,以及SubscriptionServer使用SubscriptionManager

// in subscription.js
import { PubSub, SubscriptionManager } from 'graphql-subscriptions';

const pubSub = new PubSub();

let subscriptionManagerOptions = {
    schema: schema, // this is your graphql schema
    setupFunctions: {
        // here come your setup functions
    },
    pubSub: pubSub
};

const subscriptionManager = new SubscriptionManager(subscriptionManagerOptions);

export { pubSub, subscriptionManager };

创建完成后subscriptionManager,我们现在可以实现WSServer

import { createServer } from 'http';
import { SubscriptionServer } from 'subscription-transport-ws';
import { subscriptionManager } from './subscription';

const webSocketServerWrapper = {
    start: function(port){

        const webSocketServer = createServer((request, response) => {
            response.writeHead(404);
            response.end();
        });    

        webSocketServer.listen(port, () => {
            console.log('WSServer listening on port ' + port);
        });

        new SubscriptionServer({
            subscriptionManager,
            onSubscribe: (message, options, request) => {
                return Promise.resolve(Object.assign({}, options, {}));
            }
        }, webSocketServer);

    }
};

export default webSocketServerWrapper;

webSocketServerWrapper现在您可以像在初始化文件中一样导入index.js并简单地运行webSocketServerWrapper.start(PORT);

在这里,我写的第二个答案,您可以找到负责创建示例订阅以及如何处理它的代码。

于 2017-03-21T12:54:22.487 回答