feat: least surprise in failures

This change covers two possible failures:

* a developer requesting a chain that hasn't been configured
* network failures on subgraphs

If a developer forgets to add to the client map, this will throw an
error instead of defaulting to ethereum results. This helps the
developer make changes correctly.

If a network failure occurs or a developer makes a mistake, the entire
membership structure is not affected via `Promise.allSettled`. This avoids
confusing errors for users.
This commit is contained in:
Matthew Cantelon
2021-06-10 07:19:20 -07:00
committed by Hammad Jutt
parent 3dda8aa7ca
commit 1063e245c8
2 changed files with 17 additions and 8 deletions

View File

@@ -26,16 +26,20 @@ export const getDaoHausMemberships: QueryResolvers['getDaoHausMemberships'] = as
const membershipsOn = addChain(memberAddress);
const memberships = await Promise.all([
const memberships = await Promise.allSettled([
membershipsOn('ethereum'),
membershipsOn('polygon'),
membershipsOn('xdai'),
]);
const members: Member[] = memberships.reduce(
(allMembers, chainMembers) => [...allMembers, ...chainMembers],
<Member[]>[],
);
const members: Member[] = memberships.reduce((allMembers, chainMembers) => {
if (chainMembers.status === 'rejected') {
console.error('Pulling memberships failed:', chainMembers.reason);
return allMembers;
}
return [...allMembers, ...chainMembers.value];
}, <Member[]>[]);
return members;
};

View File

@@ -3,13 +3,18 @@ import { GraphQLClient } from 'graphql-request';
import { CONFIG } from '../config';
import { getSdk, Sdk } from './autogen/daohaus-sdk';
const defaultGqlClient = new GraphQLClient(CONFIG.daoHausGraphqlURL);
const clients = new Map([
['polygon', new GraphQLClient(CONFIG.daoHausPolygonGraphqlURL)],
['xdai', new GraphQLClient(CONFIG.daoHausXdaiGraphqlURL)],
['ethereum', defaultGqlClient],
['ethereum', new GraphQLClient(CONFIG.daoHausGraphqlURL)],
]);
export function getClient(chain: string): Sdk {
return getSdk(clients.get(chain) || defaultGqlClient);
const gqlClient = clients.get(chain);
if (!gqlClient)
throw new Error(
`The '${chain}' chain is unrecognized, unable to create GQL Client`,
);
return getSdk(gqlClient);
}