import ApolloClient, { Operation, ApolloQueryResult, FetchResult, FetchPolicy } from 'apollo-boost'; import fetch from 'isomorphic-fetch'; const Client = new ApolloClient({ fetch, uri: process.env.FLOOMX_API_URL_X, request: async (operation: Operation): Promise => { operation.setContext({ headers: { 'X-FloomX-Api': process.env.GATSBY_FLOOMX_TOKEN } }); }, onError: ({graphQLErrors, networkError, operation, forward}): void => { if (graphQLErrors) { for (const err of graphQLErrors) { // handle errors differently based on its error code console.log(err); } } if (networkError) { // handle errors differently based on its error code console.log(networkError); } } }); export class GraphQL { static query(query: any, variables: object = {}, fetchPolicy: FetchPolicy = 'cache-first') : Promise> { return Client.query({ query, variables, fetchPolicy }); } static mutate( mutation: any, variables: object = {}, optimisticResponse: object = {} ): Promise> { return Client.mutate({ mutation, variables, optimisticResponse }); } } export default GraphQL;