Return graphql errors when exists (#5389)
- throw badRequest with graphql error messages when graphql request fails - clean some code Before <img width="1470" alt="image" src="https://github.com/twentyhq/twenty/assets/29927851/0b700d9a-2bbe-41f7-84a9-981dc7dd5344"> After 
This commit is contained in:
@ -1,87 +1,72 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { HttpService } from '@nestjs/axios';
|
||||
|
||||
import { Request } from 'express';
|
||||
import { AxiosResponse } from 'axios';
|
||||
|
||||
import { EnvironmentService } from 'src/engine/integrations/environment/environment.service';
|
||||
import { ApiRestQueryBuilderFactory } from 'src/engine/api/rest/api-rest-query-builder/api-rest-query-builder.factory';
|
||||
import { TokenService } from 'src/engine/core-modules/auth/services/token.service';
|
||||
import { ApiRestResponse } from 'src/engine/api/rest/types/api-rest-response.type';
|
||||
import { ApiRestQuery } from 'src/engine/api/rest/types/api-rest-query.type';
|
||||
import { getServerUrl } from 'src/utils/get-server-url';
|
||||
|
||||
@Injectable()
|
||||
export class ApiRestService {
|
||||
constructor(
|
||||
private readonly tokenService: TokenService,
|
||||
private readonly environmentService: EnvironmentService,
|
||||
private readonly apiRestQueryBuilderFactory: ApiRestQueryBuilderFactory,
|
||||
private readonly httpService: HttpService,
|
||||
) {}
|
||||
|
||||
async callGraphql(
|
||||
request: Request,
|
||||
data: ApiRestQuery,
|
||||
): Promise<ApiRestResponse> {
|
||||
async callGraphql(request: Request, data: ApiRestQuery) {
|
||||
const baseUrl = getServerUrl(
|
||||
request,
|
||||
this.environmentService.get('SERVER_URL'),
|
||||
);
|
||||
let response: AxiosResponse;
|
||||
|
||||
try {
|
||||
return await this.httpService.axiosRef.post(`${baseUrl}/graphql`, data, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: request.headers.authorization,
|
||||
response = await this.httpService.axiosRef.post(
|
||||
`${baseUrl}/graphql`,
|
||||
data,
|
||||
{
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: request.headers.authorization,
|
||||
},
|
||||
},
|
||||
});
|
||||
);
|
||||
} catch (err) {
|
||||
return {
|
||||
data: {
|
||||
error: `${err}. Please check your query.`,
|
||||
status: err.response.status,
|
||||
},
|
||||
};
|
||||
throw new BadRequestException(err.response.data);
|
||||
}
|
||||
|
||||
if (response.data.errors?.length) {
|
||||
throw new BadRequestException(response.data);
|
||||
}
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
async get(request: Request): Promise<ApiRestResponse> {
|
||||
try {
|
||||
const data = await this.apiRestQueryBuilderFactory.get(request);
|
||||
async get(request: Request) {
|
||||
const data = await this.apiRestQueryBuilderFactory.get(request);
|
||||
|
||||
return await this.callGraphql(request, data);
|
||||
} catch (err) {
|
||||
return { data: { error: err, status: err.status } };
|
||||
}
|
||||
return await this.callGraphql(request, data);
|
||||
}
|
||||
|
||||
async delete(request: Request): Promise<ApiRestResponse> {
|
||||
try {
|
||||
const data = await this.apiRestQueryBuilderFactory.delete(request);
|
||||
async delete(request: Request) {
|
||||
const data = await this.apiRestQueryBuilderFactory.delete(request);
|
||||
|
||||
return await this.callGraphql(request, data);
|
||||
} catch (err) {
|
||||
return { data: { error: err, status: err.status } };
|
||||
}
|
||||
return await this.callGraphql(request, data);
|
||||
}
|
||||
|
||||
async create(request: Request): Promise<ApiRestResponse> {
|
||||
try {
|
||||
const data = await this.apiRestQueryBuilderFactory.create(request);
|
||||
async create(request: Request) {
|
||||
const data = await this.apiRestQueryBuilderFactory.create(request);
|
||||
|
||||
return await this.callGraphql(request, data);
|
||||
} catch (err) {
|
||||
return { data: { error: err, status: err.status } };
|
||||
}
|
||||
return await this.callGraphql(request, data);
|
||||
}
|
||||
|
||||
async update(request: Request): Promise<ApiRestResponse> {
|
||||
try {
|
||||
const data = await this.apiRestQueryBuilderFactory.update(request);
|
||||
async update(request: Request) {
|
||||
const data = await this.apiRestQueryBuilderFactory.update(request);
|
||||
|
||||
return await this.callGraphql(request, data);
|
||||
} catch (err) {
|
||||
return { data: { error: err, status: err.status } };
|
||||
}
|
||||
return await this.callGraphql(request, data);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user