-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,48 @@ | ||
import { InMemoryCache } from 'apollo-cache-inmemory'; | ||
import { ApolloClient } from 'apollo-client'; | ||
import { getMainDefinition } from 'apollo-utilities'; | ||
import { HttpLink } from 'apollo-link-http'; | ||
import { WebSocketLink } from 'apollo-link-ws'; | ||
import { ApolloLink, split } from 'apollo-link'; | ||
|
||
const httpUri = process.env.REACT_APP_SERVER_URL + '/graphql'; | ||
const wsUri = httpUri.replace(/^https?/, 'ws'); | ||
|
||
const httpLink = new HttpLink({ | ||
uri: httpUri, | ||
}); | ||
|
||
const wsLink = new WebSocketLink({ | ||
uri: wsUri, | ||
options: { | ||
// Automatic reconnect in case of connection error | ||
reconnect: true, | ||
}, | ||
}); | ||
|
||
/** | ||
* Fix error typing in `split` method in `apollo-link` | ||
* Related issue https://github.com/apollographql/apollo-client/issues/3090 | ||
*/ | ||
export interface Definition { | ||
kind: string; | ||
operation?: string; | ||
} | ||
const terminatingLink = split( | ||
({ query }) => { | ||
const { kind, operation }: Definition = getMainDefinition(query); | ||
// If this is a subscription query, use wsLink, otherwise use httpLink | ||
return kind === 'OperationDefinition' && operation === 'subscription'; | ||
}, | ||
wsLink, | ||
httpLink | ||
); | ||
|
||
const link = ApolloLink.from([terminatingLink]); | ||
|
||
const inMemoryCache = new InMemoryCache(); | ||
|
||
export default new ApolloClient({ | ||
link: httpLink, | ||
link, | ||
cache: inMemoryCache, | ||
}); |