-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraphqlUtils.ts
More file actions
32 lines (29 loc) · 846 Bytes
/
graphqlUtils.ts
File metadata and controls
32 lines (29 loc) · 846 Bytes
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
32
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/graphql/',
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 };
} else {
return { data: mapKeysDeep(res.data, camelCase), ok: true };
}
})
.catch((err) => {
return { error: err, ok: false };
});
};