forked from wednesday-solutions/react-graphql-ts-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphqlUtils.ts
31 lines (28 loc) · 820 Bytes
/
graphqlUtils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import ApolloClient, { DocumentNode, InMemoryCache } from 'apollo-boost';
import camelCase from 'lodash-es/camelCase';
import { mapKeysDeep } from '.';
export const client = new ApolloClient({
uri: 'https://spacex-production.up.railway.app',
cache: new InMemoryCache()
});
export interface GqlQueryReponse<Data> {
data?: Data;
error?: any;
ok: boolean;
}
export const getQueryResponse = <Data, Variables>(
query: DocumentNode,
variables?: Variables
): Promise<GqlQueryReponse<Data>> => {
return client
.query<Data, Variables>({ query, variables })
.then((res) => {
if (res.errors) {
return { error: res.errors, ok: false };
}
return { data: mapKeysDeep(res.data, camelCase), ok: true };
})
.catch((err) => {
return { error: err, ok: false };
});
};