2024-01-23 11:47:02 +00:00
|
|
|
import { ApolloClient, DefaultOptions, InMemoryCache, NormalizedCacheObject } from '@apollo/client';
|
2024-01-17 08:52:15 +00:00
|
|
|
|
2024-01-24 14:47:43 +00:00
|
|
|
import { getUser, getOrganizations, getDeployments, getProjectMembers, searchProjects } from './queries';
|
|
|
|
import { GetDeploymentsResponse, GetOrganizationsResponse, GetProjectMembersResponse, SearchProjectsResponse, GetUserResponse, RemoveMemberResponse } from './types';
|
2024-01-23 11:47:02 +00:00
|
|
|
import { removeMember } from './mutations';
|
2024-01-17 08:52:15 +00:00
|
|
|
|
|
|
|
export interface GraphQLConfig {
|
|
|
|
gqlEndpoint: string;
|
|
|
|
}
|
|
|
|
|
2024-01-23 11:47:02 +00:00
|
|
|
// TODO: check options
|
|
|
|
const defaultOptions: DefaultOptions = {
|
|
|
|
watchQuery: {
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'ignore'
|
|
|
|
},
|
|
|
|
query: {
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-01-17 08:52:15 +00:00
|
|
|
export class GQLClient {
|
|
|
|
private client: ApolloClient<NormalizedCacheObject>;
|
|
|
|
|
|
|
|
constructor (config: GraphQLConfig) {
|
|
|
|
this.client = new ApolloClient({
|
|
|
|
uri: config.gqlEndpoint,
|
2024-01-23 11:47:02 +00:00
|
|
|
cache: new InMemoryCache(),
|
|
|
|
defaultOptions
|
2024-01-17 08:52:15 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-01-23 11:47:02 +00:00
|
|
|
async getUser () : Promise<GetUserResponse> {
|
2024-01-17 08:52:15 +00:00
|
|
|
const { data } = await this.client.query({
|
|
|
|
query: getUser
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2024-01-18 04:34:02 +00:00
|
|
|
|
2024-01-23 11:47:02 +00:00
|
|
|
async getOrganizations () : Promise<GetOrganizationsResponse> {
|
2024-01-18 04:34:02 +00:00
|
|
|
const { data } = await this.client.query({
|
|
|
|
query: getOrganizations
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2024-01-18 12:20:50 +00:00
|
|
|
|
2024-01-23 11:47:02 +00:00
|
|
|
async getDeployments (projectId: string) : Promise<GetDeploymentsResponse> {
|
2024-01-18 12:20:50 +00:00
|
|
|
const { data } = await this.client.query({
|
|
|
|
query: getDeployments,
|
|
|
|
variables: {
|
|
|
|
projectId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2024-01-23 11:47:02 +00:00
|
|
|
|
|
|
|
async removeMember (memberId: string): Promise<RemoveMemberResponse> {
|
|
|
|
const { data } = await this.client.mutate({
|
|
|
|
mutation: removeMember,
|
|
|
|
variables: {
|
|
|
|
memberId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async getProjectMembers (projectId: string) : Promise<GetProjectMembersResponse> {
|
|
|
|
const { data } = await this.client.query({
|
|
|
|
query: getProjectMembers,
|
|
|
|
variables: {
|
|
|
|
projectId
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2024-01-24 14:47:43 +00:00
|
|
|
|
|
|
|
async searchProjects (searchText: string) : Promise<SearchProjectsResponse> {
|
|
|
|
const { data } = await this.client.query({
|
|
|
|
query: searchProjects,
|
|
|
|
variables: {
|
|
|
|
searchText
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
2024-01-17 08:52:15 +00:00
|
|
|
}
|