ToolJet/server/plugins/datasources/graphql/index.ts
Arpit 26c9cc655c
Fix linting errors across the app (#785)
* eslint-setup: rules for frontend and server

* setup pre-commit:hook

* frontend:eslint fixes

* frontend eslint errors and warning fixed

* eslint:fix for ./server

* fix server/test: expectatin string lint/error

* pre-commit:updated

* removed unwanted install cmd from docker file

* recommended settings and extension for vscode

* husky prepare script added

* updated extension recommendations

* added prettier as recommended extension

* added pre-commit to package.json

* remove .prettierrc file

* resolve changes

* resolve changes
2021-09-21 19:18:28 +05:30

48 lines
1.4 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { HTTPError } from 'got';
import { QueryError } from 'src/modules/data_sources/query.error';
import { QueryResult } from 'src/modules/data_sources/query_result.type';
import { QueryService } from 'src/modules/data_sources/query_service.interface';
const got = require('got');
@Injectable()
export default class GraphqlQueryService implements QueryService {
async run(sourceOptions: any, queryOptions: any, dataSourceId: string): Promise<QueryResult> {
let result = {};
const url = sourceOptions.url;
const query = queryOptions.query;
const headers = Object.fromEntries(sourceOptions['headers']);
const searchParams = Object.fromEntries(sourceOptions['url_params']);
// Remove invalid headers from the headers object
Object.keys(headers).forEach((key) => (headers[key] === '' ? delete headers[key] : {}));
const json = {
query,
};
try {
const response = await got(url, {
method: 'post',
headers,
searchParams,
json,
});
result = JSON.parse(response.body);
} catch (error) {
console.log(error);
if (error instanceof HTTPError) {
result = {
code: error.code,
};
}
throw new QueryError('Query could not be completed', error.message, result);
}
return {
status: 'ok',
data: result,
};
}
}