2019-10-30 04:05:52 -07:00
|
|
|
import { IExecuteFunctions } from 'n8n-core';
|
|
|
|
import {
|
2020-04-01 00:08:18 -07:00
|
|
|
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',
|
2019-10-31 03:45:50 -07:00
|
|
|
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',
|
2019-10-31 03:45:50 -07:00
|
|
|
value: 'GET',
|
2019-10-30 04:05:52 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'POST',
|
2019-10-31 03:45:50 -07:00
|
|
|
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)',
|
2019-10-31 03:45:50 -07:00
|
|
|
value: 'graphql',
|
2019-10-30 04:05:52 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'JSON',
|
2019-10-31 03:45:50 -07:00
|
|
|
value: 'json',
|
2019-10-30 04:05:52 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
requestMethod: [
|
2019-10-31 03:45:50 -07:00
|
|
|
'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: [
|
2019-10-31 03:45:50 -07:00
|
|
|
'json',
|
2019-10-30 04:05:52 -07:00
|
|
|
],
|
|
|
|
requestMethod: [
|
2019-10-31 03:45:50 -07:00
|
|
|
'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: [
|
2019-10-31 03:45:50 -07:00
|
|
|
'json',
|
2019-10-30 04:05:52 -07:00
|
|
|
],
|
|
|
|
requestMethod: [
|
2019-10-31 03:45:50 -07:00
|
|
|
'POST',
|
2019-10-30 04:05:52 -07:00
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Response Format',
|
|
|
|
name: 'responseFormat',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'JSON',
|
2019-10-31 03:45:50 -07:00
|
|
|
value: 'json',
|
2019-10-30 04:05:52 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'String',
|
2019-10-31 03:45:50 -07:00
|
|
|
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.',
|
|
|
|
},
|
2020-04-01 00:08:18 -07:00
|
|
|
|
|
|
|
// 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.',
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
],
|
2020-04-01 00:08:18 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
],
|
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;
|
2019-10-31 03:45:50 -07:00
|
|
|
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
|
2020-04-01 00:08:18 -07:00
|
|
|
.getNodeParameter('headerParametersUi', itemIndex, {}) as IDataObject;
|
|
|
|
const headerParameters = (parameter || []).reduce((result, item) => ({
|
|
|
|
...result,
|
2020-10-22 06:46:03 -07:00
|
|
|
[item.name]: item.value,
|
2020-04-01 00:08:18 -07:00
|
|
|
}), {});
|
|
|
|
|
2019-10-30 04:05:52 -07:00
|
|
|
requestOptions = {
|
|
|
|
headers: {
|
|
|
|
'content-type': `application/${requestFormat}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
...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') {
|
2019-10-31 03:45:50 -07:00
|
|
|
requestOptions.qs = {
|
2020-10-22 06:46:03 -07:00
|
|
|
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,
|
2021-03-08 09:45:35 -08:00
|
|
|
operationName: this.getNodeParameter('operationName', itemIndex) as string,
|
2019-10-30 04:05:52 -07:00
|
|
|
};
|
2019-10-31 03:45:50 -07:00
|
|
|
if (typeof requestOptions.body.variables === 'string') {
|
2019-10-30 06:02:14 -07:00
|
|
|
try {
|
2020-11-20 07:51:46 -08:00
|
|
|
requestOptions.body.variables = JSON.parse(requestOptions.body.variables || '{}');
|
2020-11-18 14:14:41 -08:00
|
|
|
} catch (e) {
|
|
|
|
throw new Error('Using variables failed:\n' + requestOptions.body.variables + '\n\nWith error message:\n' + e);
|
2019-10-30 06:02:14 -07:00
|
|
|
}
|
2019-10-30 04:05:52 -07:00
|
|
|
}
|
2019-10-31 03:45:50 -07:00
|
|
|
if (requestOptions.body.operationName === '') {
|
2020-06-03 06:25:43 -07:00
|
|
|
requestOptions.body.operationName = null;
|
2019-10-30 04:05:52 -07:00
|
|
|
}
|
|
|
|
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,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2019-10-30 04:05:52 -07:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (typeof response === 'string') {
|
2019-12-19 14:15:19 -08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|