import { ApolloClient, DefaultOptions, InMemoryCache, NormalizedCacheObject } from '@apollo/client'; import { getUser, getOrganizations, getDeployments, getProjectMembers, searchProjects } from './queries'; import { GetDeploymentsResponse, GetOrganizationsResponse, GetProjectMembersResponse, SearchProjectsResponse, GetUserResponse, RemoveMemberResponse } from './types'; import { removeMember } from './mutations'; export interface GraphQLConfig { gqlEndpoint: string; } // TODO: check options const defaultOptions: DefaultOptions = { watchQuery: { fetchPolicy: 'no-cache', errorPolicy: 'ignore' }, query: { fetchPolicy: 'no-cache', errorPolicy: 'all' } }; export class GQLClient { private client: ApolloClient; constructor (config: GraphQLConfig) { this.client = new ApolloClient({ uri: config.gqlEndpoint, cache: new InMemoryCache(), defaultOptions }); } async getUser () : Promise { const { data } = await this.client.query({ query: getUser }); return data; } async getOrganizations () : Promise { const { data } = await this.client.query({ query: getOrganizations }); return data; } async getDeployments (projectId: string) : Promise { const { data } = await this.client.query({ query: getDeployments, variables: { projectId } }); return data; } async removeMember (memberId: string): Promise { const { data } = await this.client.mutate({ mutation: removeMember, variables: { memberId } }); return data; } async getProjectMembers (projectId: string) : Promise { const { data } = await this.client.query({ query: getProjectMembers, variables: { projectId } }); return data; } async searchProjects (searchText: string) : Promise { const { data } = await this.client.query({ query: searchProjects, variables: { searchText } }); return data; } }