n8n/packages/nodes-base/nodes/GraphQL/GraphQL.node.ts

282 lines
6.5 KiB
TypeScript
Raw Normal View History

2019-10-30 04:05:52 -07:00
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
2019-10-30 04:05:52 -07:00
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { OptionsWithUri } from 'request';
import { RequestPromiseOptions } from 'request-promise-native';
export class GraphQL implements INodeType {
description: INodeTypeDescription = {
displayName: 'GraphQL',
name: 'graphql',
icon: 'file:graphql.png',
2019-10-30 04:05:52 -07:00
group: ['input'],
version: 1,
description: 'Makes a GraphQL request and returns the received data',
defaults: {
name: 'GraphQL',
color: '#E10098',
},
inputs: ['main'],
outputs: ['main'],
properties: [
{
displayName: 'HTTP Request Method',
name: 'requestMethod',
type: 'options',
options: [
{
name: 'GET',
value: 'GET',
2019-10-30 04:05:52 -07:00
},
{
name: 'POST',
value: 'POST',
2019-10-30 04:05:52 -07:00
},
],
default: 'POST',
description: 'The underlying HTTP request method to use.',
},
{
displayName: 'Endpoint',
name: 'endpoint',
type: 'string',
default: '',
placeholder: 'http://example.com/graphql',
description: 'The GraphQL endpoint.',
required: true,
},
{
displayName: 'Ignore SSL Issues',
name: 'allowUnauthorizedCerts',
type: 'boolean',
default: false,
description: 'Still fetch the response even if SSL certificate validation is not possible.',
},
{
displayName: 'Request Format',
name: 'requestFormat',
type: 'options',
required: true,
options: [
{
name: 'GraphQL (raw)',
value: 'graphql',
2019-10-30 04:05:52 -07:00
},
{
name: 'JSON',
value: 'json',
2019-10-30 04:05:52 -07:00
},
],
displayOptions: {
show: {
requestMethod: [
'POST',
2019-10-30 04:05:52 -07:00
],
},
},
default: 'graphql',
description: 'The format for the query payload',
},
{
displayName: 'Query',
name: 'query',
type: 'json',
default: '',
description: 'GraphQL query',
required: true,
},
{
displayName: 'Variables',
name: 'variables',
type: 'json',
2019-10-30 06:02:14 -07:00
default: '',
2019-10-30 04:05:52 -07:00
description: 'Query variables',
displayOptions: {
show: {
requestFormat: [
'json',
2019-10-30 04:05:52 -07:00
],
requestMethod: [
'POST',
2019-10-30 04:05:52 -07:00
],
},
},
},
{
displayName: 'Operation Name',
name: 'operationName',
type: 'string',
default: '',
description: 'Name of operation to execute',
displayOptions: {
show: {
requestFormat: [
'json',
2019-10-30 04:05:52 -07:00
],
requestMethod: [
'POST',
2019-10-30 04:05:52 -07:00
],
},
},
},
{
displayName: 'Response Format',
name: 'responseFormat',
type: 'options',
options: [
{
name: 'JSON',
value: 'json',
2019-10-30 04:05:52 -07:00
},
{
name: 'String',
value: 'string',
2019-10-30 04:05:52 -07:00
},
],
default: 'json',
description: 'The format in which the data gets returned from the URL.',
},
{
displayName: 'Response Data Property Name',
name: 'dataPropertyName',
type: 'string',
2019-10-31 09:26:05 -07:00
default: 'data',
2019-10-30 04:05:52 -07:00
required: true,
displayOptions: {
show: {
responseFormat: [
'string',
],
},
},
description: 'Name of the property to which to write the response data.',
},
// Header Parameters
{
displayName: 'Headers',
name: 'headerParametersUi',
placeholder: 'Add Header',
type: 'fixedCollection',
typeOptions: {
multipleValues: true,
},
description: 'The headers to send.',
default: {},
options: [
{
name: 'parameter',
displayName: 'Header',
values: [
{
displayName: 'Name',
name: 'name',
type: 'string',
default: '',
description: 'Name of the header.',
},
{
displayName: 'Value',
name: 'value',
type: 'string',
default: '',
description: 'Value to set for the header.',
},
]
},
],
},
2019-10-30 04:05:52 -07:00
]
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
let requestOptions: OptionsWithUri & RequestPromiseOptions;
const returnItems: INodeExecutionData[] = [];
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
const requestMethod = this.getNodeParameter('requestMethod', itemIndex, 'POST') as string;
const endpoint = this.getNodeParameter('endpoint', itemIndex, '') as string;
const requestFormat = this.getNodeParameter('requestFormat', itemIndex, 'graphql') as string;
2019-10-30 04:05:52 -07:00
const responseFormat = this.getNodeParameter('responseFormat', 0) as string;
2020-04-19 09:43:45 -07:00
const { parameter }: { parameter?: Array<{ name: string, value: string }> } = this
.getNodeParameter('headerParametersUi', itemIndex, {}) as IDataObject;
const headerParameters = (parameter || []).reduce((result, item) => ({
...result,
[item.name]: item.value
}), {});
2019-10-30 04:05:52 -07:00
requestOptions = {
headers: {
'content-type': `application/${requestFormat}`,
...headerParameters
2019-10-30 04:05:52 -07:00
},
method: requestMethod,
uri: endpoint,
simple: false,
rejectUnauthorized: !this.getNodeParameter('allowUnauthorizedCerts', itemIndex, false) as boolean,
};
const gqlQuery = this.getNodeParameter('query', itemIndex, '') as string;
if (requestMethod === 'GET') {
requestOptions.qs = {
query: gqlQuery
2019-10-30 04:05:52 -07:00
};
} else {
if (requestFormat === 'json') {
requestOptions.body = {
query: gqlQuery,
variables: this.getNodeParameter('variables', itemIndex, {}) as object,
operationName: this.getNodeParameter('operationName', itemIndex, null) as string,
};
if (typeof requestOptions.body.variables === 'string') {
2019-10-30 06:02:14 -07:00
try {
requestOptions.body.variables = JSON.parse(requestOptions.body.variables);
2019-10-30 06:02:14 -07:00
} catch {
requestOptions.body.variables = {};
}
2019-10-30 04:05:52 -07:00
}
if (requestOptions.body.operationName === '') {
2019-10-30 04:05:52 -07:00
requestOptions.body.operation = null;
}
requestOptions.json = true;
} else {
requestOptions.body = gqlQuery;
}
}
const response = await this.helpers.request(requestOptions);
if (responseFormat === 'string') {
const dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
returnItems.push({
json: {
[dataPropertyName]: response,
}
});
} else {
if (typeof response === 'string') {
try {
returnItems.push({ json: JSON.parse(response) });
} catch (e) {
throw new Error('Response body is not valid JSON. Change "Response Format" to "String"');
}
} else {
returnItems.push({ json: response });
2019-10-30 04:05:52 -07:00
}
}
}
return this.prepareOutputData(returnItems);
}
}