ToolJet/plugins/packages/graphql/lib/index.ts
Maurits Lourens 60f515d19c
Feature/2395 - add eslint to plugins (#2402)
* merge develop

* Add eslint dependencies, configs and scripts to plugins project

* run lint with Github action

* ignore tests and dist folders

* fun eslint with --fix and manual fixes, renamed __tests_ to __tests__

* add plugins packages folder to lint-staged config

* fix lint issue
2022-03-10 12:29:48 +05:30

45 lines
1.3 KiB
TypeScript

import { HTTPError } from 'got';
import { QueryError, QueryResult, QueryService } from '@tooljet-plugins/common';
import got from 'got';
import { SourceOptions, QueryOptions } from './types';
export default class GraphqlQueryService implements QueryService {
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, 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,
};
}
}