Apollo GraphQL:如何设置安全Websockets?

时间:2017-09-08 04:41:17

标签: websocket graphql apollo apollo-client apollo-server

我正在设置我的开发系统以使用https,Chrome正在抱怨我的websocket无法开始安全:

  

VM4965:161混合内容:“https://mywebsite.io/”的页面是   通过HTTPS加载,但尝试连接到不安全的WebSocket   端点'ws://mywebsite.io:4000 / subscriptions'。这个要求有   被封锁了;此端点必须通过WSS提供。

这是我目前基于Apollo文档的WS服务器端设置:

const localHostString = 'mywebsite.io'; 
const pubsub = new PubSub();

// additional context you use for your resolvers, if any
const context = {connectors: connectors};

//SET UP APOLLO QUERY / MUTATIONS / PUBSUB
//start a graphql server with Express handling a possible Meteor current user
createApolloServer({
    schema,
    context
});

const METEOR_PORT = 3000;
const GRAPHQL_PORT = 4000;
const server = express();

server.use('*', cors({ origin: `https://${localHostString}:${METEOR_PORT}` }));

server.use('/graphql', bodyParser.json(), graphqlExpress({
    schema,
    context
}));

server.use('/graphiql', graphiqlExpress({
    endpointURL: '/graphql',
    subscriptionsEndpoint: `ws://${localHostString}:${GRAPHQL_PORT}/subscriptions`
}));

// Wrap the Express server
const ws = createServer(server);
ws.listen(GRAPHQL_PORT, () => {
    console.log(`GraphQL Server is now running on http://${localHostString}:${GRAPHQL_PORT}`);
    console.log(`GraphiQL available at http://${localHostString}:${GRAPHQL_PORT}/graphiql`);
    // Set up the WebSocket for handling GraphQL subscriptions
    new SubscriptionServer({
        execute,
        subscribe,
        schema
    }, {
        server: ws,
        path: '/subscriptions',
    });
});

如何更新此内容以便使用WSS而不是WS websockets?

提前致谢所有信息。

1 个答案:

答案 0 :(得分:2)

看来你在做

subscriptionsEndpoint: `ws://${localHostString}:${GRAPHQL_PORT}/subscriptions

也许可以将ws更改为wss。即:

subscriptionsEndpoint: `wss://${localHostString}:${GRAPHQL_PORT}/subscriptions