import { GraphQLClient } from 'graphql-request'; import { Variables } from 'graphql-request/dist/types'; import Config from '../config'; import { ConfigStoreService } from '../services'; class GraphQLService { protected client: GraphQLClient; constructor() { this.client = new GraphQLClient(Config.SERVER_URL); } public async query(query: string, variables?: Variables): Promise { this.setHeader(); return this.client.request(query, variables); } public async mutation(mutation: string, variables?: Variables) { this.setHeader(); return this.client.request(mutation, variables); } public getToken() { return ConfigStoreService.get('token'); } private setHeader() { const token = this.getToken(); if (token) { this.client.setHeader('Authorization', `Bearer ${token}`); } } } export default new GraphQLService();