storj/web/satellite/src/api/baseGql.ts
Egon Elbre ad0b19fb02 web/satellite: fix lint issues
Fix svg.d.ts definition.

Disable no-explicit-any in src/api, because wrangling all the GraphQL
result types properly is not that nice. We can either fix this later
manually, generate GraphQL types or remove the GraphQL endpoints.

Add annotations to src/store/. Currently it still uses any in places and
also defines more types than absolutely necessary. This is an
unfortunate side-effect of the vuex api. There does seem to be an
alternative package that handles them, but to minimize the number of
changes, we'll currently use these types. Due to those decisions it's
also not easily possible to have types instead of any in multiple
places.

StripeCardInput currently uses any, however, if we find the proper
declarations, we can replace them later.

Change-Id: I2ec8bf7fdd8023129d1f8739ce2b6d97de2a58d0
2021-08-25 06:01:19 +00:00

65 lines
1.8 KiB
TypeScript

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
import gql from 'graphql-tag';
import { apollo } from '@/utils/apollo';
import { GraphQLError } from "graphql";
/**
* BaseGql is a graphql utility which allows to perform queries and mutations.
*/
export class BaseGql {
/**
* performs qraphql query.
*
* @param query - qraphql query
* @param variables - variables to bind in query. null by default.
* @throws Error
*/
protected async query(query: string, variables: any = null): Promise<any> { // eslint-disable-line @typescript-eslint/no-explicit-any
const response = await apollo.query(
{
query: gql(query),
variables,
fetchPolicy: 'no-cache',
errorPolicy: 'all',
},
);
if (response.errors) {
throw new Error(this.combineErrors(response.errors));
}
return response;
}
/**
* performs qraphql mutation.
*
* @param query - qraphql query
* @param variables - variables to bind in query. null by default.
* @throws Error
*/
protected async mutate(query: string, variables: any = null): Promise<any> { // eslint-disable-line @typescript-eslint/no-explicit-any
const response = await apollo.mutate(
{
mutation: gql(query),
variables,
fetchPolicy: 'no-cache',
errorPolicy: 'all',
},
);
if (response.errors) {
throw new Error(this.combineErrors(response.errors));
}
return response;
}
private combineErrors(gqlError: readonly GraphQLError[]): string {
return gqlError.map(err => err).join('\n');
}
}