ИП
Size: a a a
ИП
S
K
ИП
ИП
import { onError } from 'apollo-link-error'
import { ApolloLink, split, execute } from 'apollo-link'
import { setContext } from 'apollo-link-context'
import { HttpLink } from 'apollo-link-http'
import { WebSocketLink } from 'apollo-link-ws'
import { getMainDefinition } from 'apollo-utilities'
import noop from 'lodash/noop'
window.useHttp = (bool=true) => {
if (bool)
localStorage.setItem('useHttp', '1')
else
localStorage.removeItem('useHttp')
location.reload()
}
export default class Client {
ws = null
link = null
constructor(uri, Authorization, onFirstConnect=noop, onDisconnect=noop, onReconnect=noop) {
const http = localStorage.getItem('useHttp')
this.ws = new WebSocketLink({
uri: uri.replace('http', 'ws'),
options: {
reconnect: true,
connectionParams: {
Authorization,
},
},
})
this.ws.subscriptionClient.onConnected(onFirstConnect)
this.ws.subscriptionClient.onDisconnected(onDisconnect)
this.ws.subscriptionClient.onReconnected(onReconnect)
let link = this.ws
if (http) {
const httpLink = new HttpLink({uri})
const authLink = setContext((_, {headers}) => {
return {
headers: {
...headers,
Authorization,
},
}
}).concat(httpLink)
link = split(
({query}) => {
const {kind, operation} = getMainDefinition(query)
return kind === 'OperationDefinition' && operation === 'subscription'
},
this.ws,
authLink,
)
}
this.link = ApolloLink.from([
onError(({graphQLErrors, networkError}) => {
graphQLErrors && graphQLErrors.map(({message, locations, path}) =>
// eslint-disable-next-line no-console
console.error(
`[GraphQL error]: Message: ${message},\n` +
`Path: ${path} ${JSON.stringify(locations)}`
)
)
// eslint-disable-next-line no-console
networkError && console.log(`[Network error]: ${networkError}`)
}),
link,
])
}
execute(query, variables) {
return new Promise((resolve, reject) =>
execute(this.link, {query, variables}).subscribe({next: resolve, error: reject})
)
}
subscribe(query, variables, cb) {
execute(this.link, {query, variables}).subscribe({next: cb})
}
close() {
this.ws.subscriptionClient.unsubscribeAll()
this.ws.subscriptionClient.close()
}
}
ИП
ИП
ИП
M
M
A
MS
ИП
MG
A
RR
ИП