2019-08-14 19:11:18 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
import gql from 'graphql-tag';
|
2019-09-09 11:33:39 +01:00
|
|
|
|
2019-10-28 13:27:12 +00:00
|
|
|
import { apollo } from '@/utils/apollo';
|
2021-08-24 14:10:11 +01:00
|
|
|
import { GraphQLError } from "graphql";
|
2019-08-14 19:11:18 +01:00
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* BaseGql is a graphql utility which allows to perform queries and mutations.
|
2019-08-14 19:11:18 +01:00
|
|
|
*/
|
|
|
|
export class BaseGql {
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* performs qraphql query.
|
2019-08-14 19:11:18 +01:00
|
|
|
*
|
|
|
|
* @param query - qraphql query
|
|
|
|
* @param variables - variables to bind in query. null by default.
|
|
|
|
* @throws Error
|
|
|
|
*/
|
2021-08-24 14:10:11 +01:00
|
|
|
protected async query(query: string, variables: any = null): Promise<any> { // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
|
|
const response = await apollo.query(
|
2019-08-14 19:11:18 +01:00
|
|
|
{
|
|
|
|
query: gql(query),
|
|
|
|
variables,
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
2019-09-13 15:58:18 +01:00
|
|
|
},
|
2019-08-14 19:11:18 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
throw new Error(this.combineErrors(response.errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-02-14 15:35:10 +00:00
|
|
|
* performs qraphql mutation.
|
2019-08-14 19:11:18 +01:00
|
|
|
*
|
|
|
|
* @param query - qraphql query
|
|
|
|
* @param variables - variables to bind in query. null by default.
|
|
|
|
* @throws Error
|
|
|
|
*/
|
2021-08-24 14:10:11 +01:00
|
|
|
protected async mutate(query: string, variables: any = null): Promise<any> { // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
|
|
const response = await apollo.mutate(
|
2019-08-14 19:11:18 +01:00
|
|
|
{
|
|
|
|
mutation: gql(query),
|
|
|
|
variables,
|
|
|
|
fetchPolicy: 'no-cache',
|
|
|
|
errorPolicy: 'all',
|
2019-09-13 15:58:18 +01:00
|
|
|
},
|
2019-08-14 19:11:18 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
throw new Error(this.combineErrors(response.errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:10:11 +01:00
|
|
|
private combineErrors(gqlError: readonly GraphQLError[]): string {
|
2020-02-21 11:47:53 +00:00
|
|
|
return gqlError.map(err => err).join('\n');
|
2019-08-14 19:11:18 +01:00
|
|
|
}
|
|
|
|
}
|