Я пытаюсь преобразовать подписку клиента Apollo 1.x в 2.x, но все еще не работает
Существующий код.
subscription-transport-ws: 0.8.3 apollo-клиент: 1.9.3
import { ApolloAuthProvider } from '../auth'
import { SubscriptionClient, addGraphQLSubscriptions } from 'subscriptions-transport-ws'
import ApolloClient, { createNetworkInterface } from 'apollo-client'
networkInterface.use([{
applyMiddleware (req, next) {
if (!req.options.headers) {
req.options.headers = {}
}
req.options.headers.authorization = 'Basic xxxx'
next()
}
}])
const wsClient = new
SubscriptionClient(ApolloAuthProvider.APISocketEndPoint, {
reconnect: true,
connectionParams: {
'Authorization' : 'Basic xxxx'
}
})
const networkInterfaceWithSubscriptions = addGraphQLSubscriptions(
networkInterface,
wsClient
)
export default (store) => (
new ApolloClient({
networkInterface: networkInterfaceWithSubscriptions
})
)
Новый код:
import { ApolloClient } from 'apollo-client'
import { setContext } from 'apollo-link-context'
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { ApolloAuthProvider } from '../auth'
import { ApolloLink } from 'apollo-link'
import { SubscriptionClient } from 'subscriptions-transport-ws'
import { getMainDefinition } from 'apollo-utilities'
import { WebSocketLink } from 'apollo-link-ws'
const getHttpClientConfig = () => {
let config = {
uri: ApolloAuthProvider.APIEndPoint
}
if (ApolloAuthProvider.isNeedAuthentication()) {
config.credentials = 'include'
config.headers = ApolloAuthProvider.getHeader()
}
return config
}
const httpLink = new createHttpLink(getHttpClientConfig())
const wsClient = new
SubscriptionClient(ApolloAuthProvider.APISocketEndPoint, {
reconnect: true,
connectionParams: {
'Authorization' : 'Basic xxxx'
}
})
const webSocketLink = new WebSocketLink(wsClient)
const requestLink = ({ queryOrMutationLink, subscriptionLink }) =>
ApolloLink.split(
({ query }) => {
const { kind, operation } = getMainDefinition(query)
return kind === 'OperationDefinition' && operation === 'subscription'
},
subscriptionLink,
queryOrMutationLink,
)
const router = ApolloLink.from([
requestLink({
queryOrMutationLink: httpLink,
subscriptionLink: webSocketLink,
}),
])
export default (store) => (
new ApolloClient({
link: router,
cache: new InMemoryCache()
})
)
ожидается: нормально работает фактический результат: client.js: 426 Соединение WebSocket с 'wss: //' не удалось: HTTPАутентификация не удалась;действительные учетные данные недоступны