vega-frontend-monorepo/apps/simple-trading-app/src/app/lib/apollo-client.tsx
macqbat f0e37e4a2f
feat: [console-lite] - fixes in order close out calcs (#934)
* feat: [console-lite] - fixes in order close out calcs

* feat: [console-lite] - fixes in order close out calcs - adjust int test

* feat: [console-lite] - fixes in order close out calcs - adjust int test

* feat: [console-lite] - fixes in order close out calcs - adjust int test

* feat: [console-lite] - fixes in order close out calcs - add dash instead 0

* feat: [console-lite] - fixes in order close out calcs - add dash instead 0

Co-authored-by: maciek <maciek@vegaprotocol.io>
2022-08-04 13:21:28 +02:00

84 lines
1.9 KiB
TypeScript

import {
ApolloClient,
from,
HttpLink,
InMemoryCache,
split,
} from '@apollo/client';
import { onError } from '@apollo/client/link/error';
import { RetryLink } from '@apollo/client/link/retry';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { createClient as createWSClient } from 'graphql-ws';
import { getMainDefinition } from '@apollo/client/utilities';
export function createClient(base?: string) {
if (!base) {
throw new Error('Base must be passed into createClient!');
}
const gqlPath = 'query';
const urlHTTP = new URL(gqlPath, base);
const urlWS = new URL(gqlPath, base);
// Replace http with ws, preserving if its a secure connection eg. https => wss
urlWS.protocol = urlWS.protocol.replace('http', 'ws');
const cache = new InMemoryCache({
typePolicies: {
Party: {
keyFields: false,
},
Query: {},
Account: {
keyFields: false,
fields: {
balanceFormatted: {},
},
},
Node: {
keyFields: false,
},
},
});
const retryLink = new RetryLink({
delay: {
initial: 300,
max: 10000,
jitter: true,
},
});
const httpLink = new HttpLink({
uri: urlHTTP.href,
credentials: 'same-origin',
});
const wsLink = new GraphQLWsLink(
createWSClient({
url: urlWS.href,
})
);
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
httpLink
);
const errorLink = onError(({ graphQLErrors, networkError }) => {
console.log(graphQLErrors);
console.log(networkError);
});
return new ApolloClient({
connectToDevTools: process.env['NODE_ENV'] === 'development',
link: from([errorLink, retryLink, splitLink]),
cache,
});
}