fix: create clients once

Suggestion from @dan13ram on  PR to cache the clients and avoid
recreating on every request.
This commit is contained in:
Matthew Cantelon
2021-06-10 07:02:04 -07:00
committed by Hammad Jutt
parent 6c20a59549
commit 3dda8aa7ca
2 changed files with 11 additions and 11 deletions

View File

@@ -1,8 +1,8 @@
import { clientFactory } from '../../../../lib/daoHausClient';
import { getClient } from '../../../../lib/daoHausClient';
import { Member, QueryResolvers } from '../../autogen/types';
const addChain = (memberAddress: string) => async (chain: string) => {
const client = clientFactory(chain);
const client = getClient(chain);
const members = <Member[]>(
(await client.GetDaoHausMemberships({ memberAddress })).members
);

View File

@@ -3,13 +3,13 @@ import { GraphQLClient } from 'graphql-request';
import { CONFIG } from '../config';
import { getSdk, Sdk } from './autogen/daohaus-sdk';
export function clientFactory(chain: string): Sdk {
switch (chain) {
case 'polygon':
return getSdk(new GraphQLClient(CONFIG.daoHausPolygonGraphqlURL));
case 'xdai':
return getSdk(new GraphQLClient(CONFIG.daoHausXdaiGraphqlURL));
default:
return getSdk(new GraphQLClient(CONFIG.daoHausGraphqlURL));
}
const defaultGqlClient = new GraphQLClient(CONFIG.daoHausGraphqlURL);
const clients = new Map([
['polygon', new GraphQLClient(CONFIG.daoHausPolygonGraphqlURL)],
['xdai', new GraphQLClient(CONFIG.daoHausXdaiGraphqlURL)],
['ethereum', defaultGqlClient],
]);
export function getClient(chain: string): Sdk {
return getSdk(clients.get(chain) || defaultGqlClient);
}