I’m currently working on a project where I have an asynchronous function, fetchSomeData
, which internally makes a call to an asynchronous GraphQL data fetching function, fetchGraphQLData
. The fetchGraphQLData
function returns a promise containing data, loading status, and potential errors.
import { ApolloClient, InMemoryCache, createHttpLink } from '@apollo/client';
const DEFAULT_URI = 'https://example.com/graphql';
const getGraphQLClient = (uri = DEFAULT_URI) => {
const httpLink = createHttpLink({
uri,
});
const client = new ApolloClient({
link: httpLink,
cache: new InMemoryCache(),
});
return client;
};
export default getGraphQLClient;
export const fetchGraphQLData = async <T,>({
query,
variables,
}: QueryOptions): Promise<TQueryResult<T>> => {
const { data, loading, error } = await getGraphQLClient().query<T>({
query,
variables,
});
return { data, loading, error };
};
I’m wondering if using await
for the fetchGraphQLData
call is necessary in this context, or if there’s a more efficient or best-practice way to handle this scenario.
const fetchSomeData = async (): Promise<TQueryResult<TSomeData>> => {
return fetchGraphQLData<TSomeData>({
query: someQuery,
});;
};
Can someone shed some light on the correct usage of await in this context and any potential pitfalls I should be aware of?