#apollo-server
#apollo-сервер
Вопрос:
Apollo Server 2.0 поставляется со встроенным сервером, как описано здесь. Это означает, что при его настройке не требуется экспресс-интеграция, поэтому реализация выглядит примерно так:
const { ApolloServer, gql } = require('apollo-server');
// Construct a schema, using GraphQL schema language
const typeDefs = gql`
type Query {
announcement: String
}
`;
// Provide resolver functions for your schema fields
const resolvers = {
Query: {
announcement: () =>
`Say hello to the new Apollo Server! A production ready GraphQL server with an incredible getting started experience.`
}
};
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Я внедряю подписки на свое приложение. Как мне заставить приложение использовать защищенный протокол WebSocket с подписками? Возможно ли это вообще с использованием встроенного сервера?
По умолчанию сервер не использует WSS:
server.listen({ port: PORT }).then(({ url, subscriptionsUrl }) => console.log(url, subscriptionsUrl));
выдает http://localhost:4000/
и ws://localhost:4000/graphql
.
В процессе разработки я добился, чтобы мое приложение работало нормально, но при развертывании в рабочей среде я начал получать эти ошибки в консоли:
Mixed Content: The page at 'https://example.com/' was loaded over HTTPS,
but attempted to connect to the insecure WebSocket endpoint
'ws://example.com/graphql'. This request has been blocked; this
endpoint must be available over WSS.
и
Uncaught DOMException: Failed to construct 'WebSocket': An insecure
WebSocket connection may not be initiated from a page loaded over HTTPS.
Ответ №1:
Решено. По-видимому, было достаточно настроить URL WebSocket для начала с wss://
вместо ws://
.