2022-07-04 02:12:08 -07:00
|
|
|
/* eslint-disable n8n-nodes-base/node-filename-against-convention */
|
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,
|
2022-04-01 05:38:26 -07:00
|
|
|
JsonObject,
|
2021-04-16 09:33:36 -07:00
|
|
|
NodeApiError,
|
|
|
|
NodeOperationError,
|
2019-10-30 04:05:52 -07:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
import { RequestPromiseOptions } from 'request-promise-native';
|
|
|
|
|
|
|
|
export class GraphQL implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
|
|
|
displayName: 'GraphQL',
|
|
|
|
name: 'graphql',
|
2022-06-20 07:54:01 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
|
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',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
2021-05-19 18:04:43 -07:00
|
|
|
credentials: [
|
2022-01-15 11:05:15 -08:00
|
|
|
{
|
|
|
|
name: 'httpBasicAuth',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
|
|
|
'basicAuth',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'httpDigestAuth',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
|
|
|
'digestAuth',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-19 18:04:43 -07:00
|
|
|
{
|
|
|
|
name: 'httpHeaderAuth',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
|
|
|
'headerAuth',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-15 11:05:15 -08:00
|
|
|
{
|
|
|
|
name: 'httpQueryAuth',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
|
|
|
'queryAuth',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'oAuth1Api',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
|
|
|
'oAuth1',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'oAuth2Api',
|
|
|
|
required: true,
|
|
|
|
displayOptions: {
|
|
|
|
show: {
|
|
|
|
authentication: [
|
|
|
|
'oAuth2',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2021-05-19 18:04:43 -07:00
|
|
|
],
|
2019-10-30 04:05:52 -07:00
|
|
|
properties: [
|
2021-05-19 18:04:43 -07:00
|
|
|
{
|
|
|
|
displayName: 'Authentication',
|
|
|
|
name: 'authentication',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
2022-01-15 11:05:15 -08:00
|
|
|
{
|
|
|
|
name: 'Basic Auth',
|
|
|
|
value: 'basicAuth',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Digest Auth',
|
|
|
|
value: 'digestAuth',
|
|
|
|
},
|
2021-05-19 18:04:43 -07:00
|
|
|
{
|
|
|
|
name: 'Header Auth',
|
|
|
|
value: 'headerAuth',
|
|
|
|
},
|
2022-01-15 11:05:15 -08:00
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'None',
|
|
|
|
value: 'none',
|
2022-01-15 11:05:15 -08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth1',
|
|
|
|
value: 'oAuth1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'OAuth2',
|
|
|
|
value: 'oAuth2',
|
|
|
|
},
|
2021-05-19 18:04:43 -07:00
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
name: 'Query Auth',
|
|
|
|
value: 'queryAuth',
|
2021-05-19 18:04:43 -07:00
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'none',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The way to authenticate',
|
2021-05-19 18:04:43 -07:00
|
|
|
},
|
2019-10-30 04:05:52 -07:00
|
|
|
{
|
|
|
|
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',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The underlying HTTP request method to use',
|
2019-10-30 04:05:52 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Endpoint',
|
|
|
|
name: 'endpoint',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
|
|
|
placeholder: 'http://example.com/graphql',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The GraphQL endpoint',
|
2019-10-30 04:05:52 -07:00
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Ignore SSL Issues',
|
|
|
|
name: 'allowUnauthorizedCerts',
|
|
|
|
type: 'boolean',
|
|
|
|
default: false,
|
2022-05-06 14:01:25 -07:00
|
|
|
// eslint-disable-next-line n8n-nodes-base/node-param-description-wrong-for-ignore-ssl-issues
|
|
|
|
description: 'Whether to download the response even if SSL certificate validation is not possible',
|
2019-10-30 04:05:52 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Request Format',
|
|
|
|
name: 'requestFormat',
|
|
|
|
type: 'options',
|
|
|
|
required: true,
|
|
|
|
options: [
|
|
|
|
{
|
2022-06-03 10:23:49 -07:00
|
|
|
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',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The format in which the data gets returned from the URL',
|
2019-10-30 04:05:52 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
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',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Name of the property to which to write the response data',
|
2019-10-30 04:05:52 -07:00
|
|
|
},
|
2020-04-01 00:08:18 -07:00
|
|
|
|
|
|
|
// Header Parameters
|
|
|
|
{
|
|
|
|
displayName: 'Headers',
|
|
|
|
name: 'headerParametersUi',
|
|
|
|
placeholder: 'Add Header',
|
|
|
|
type: 'fixedCollection',
|
|
|
|
typeOptions: {
|
|
|
|
multipleValues: true,
|
|
|
|
},
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'The headers to send',
|
2020-04-01 00:08:18 -07:00
|
|
|
default: {},
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
name: 'parameter',
|
|
|
|
displayName: 'Header',
|
|
|
|
values: [
|
|
|
|
{
|
|
|
|
displayName: 'Name',
|
|
|
|
name: 'name',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Name of the header',
|
2020-04-01 00:08:18 -07:00
|
|
|
},
|
|
|
|
{
|
|
|
|
displayName: 'Value',
|
|
|
|
name: 'value',
|
|
|
|
type: 'string',
|
|
|
|
default: '',
|
2022-05-06 14:01:25 -07:00
|
|
|
description: 'Value to set for the header',
|
2020-04-01 00:08:18 -07:00
|
|
|
},
|
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();
|
2022-04-14 23:00:47 -07:00
|
|
|
let httpBasicAuth;
|
|
|
|
let httpDigestAuth;
|
|
|
|
let httpHeaderAuth;
|
|
|
|
let httpQueryAuth;
|
|
|
|
let oAuth1Api;
|
|
|
|
let oAuth2Api;
|
|
|
|
|
|
|
|
try {
|
|
|
|
httpBasicAuth = await this.getCredentials('httpBasicAuth');
|
|
|
|
} catch(error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
httpDigestAuth = await this.getCredentials('httpDigestAuth');
|
|
|
|
} catch(error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
httpHeaderAuth = await this.getCredentials('httpHeaderAuth');
|
|
|
|
} catch(error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
httpQueryAuth = await this.getCredentials('httpQueryAuth');
|
|
|
|
} catch(error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
oAuth1Api = await this.getCredentials('oAuth1Api');
|
|
|
|
} catch(error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
oAuth2Api = await this.getCredentials('oAuth2Api');
|
|
|
|
} catch(error) {
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
|
2019-10-30 04:05:52 -07:00
|
|
|
|
|
|
|
let requestOptions: OptionsWithUri & RequestPromiseOptions;
|
|
|
|
|
|
|
|
const returnItems: INodeExecutionData[] = [];
|
|
|
|
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
try {
|
|
|
|
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;
|
|
|
|
const responseFormat = this.getNodeParameter('responseFormat', 0) as string;
|
2019-10-30 04:05:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -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,
|
|
|
|
}), {});
|
2020-04-01 00:08:18 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
requestOptions = {
|
|
|
|
headers: {
|
|
|
|
'content-type': `application/${requestFormat}`,
|
|
|
|
...headerParameters,
|
|
|
|
},
|
|
|
|
method: requestMethod,
|
|
|
|
uri: endpoint,
|
|
|
|
simple: false,
|
|
|
|
rejectUnauthorized: !this.getNodeParameter('allowUnauthorizedCerts', itemIndex, false) as boolean,
|
|
|
|
};
|
2019-10-30 04:05:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
// Add credentials if any are set
|
2022-01-15 11:05:15 -08:00
|
|
|
if (httpBasicAuth !== undefined) {
|
|
|
|
requestOptions.auth = {
|
|
|
|
user: httpBasicAuth.user as string,
|
|
|
|
pass: httpBasicAuth.password as string,
|
|
|
|
};
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (httpHeaderAuth !== undefined) {
|
|
|
|
requestOptions.headers![httpHeaderAuth.name as string] = httpHeaderAuth.value;
|
|
|
|
}
|
2022-01-15 11:05:15 -08:00
|
|
|
if (httpQueryAuth !== undefined) {
|
|
|
|
if (!requestOptions.qs) {
|
|
|
|
requestOptions.qs = {};
|
|
|
|
}
|
|
|
|
requestOptions.qs![httpQueryAuth.name as string] = httpQueryAuth.value;
|
|
|
|
}
|
|
|
|
if (httpDigestAuth !== undefined) {
|
|
|
|
requestOptions.auth = {
|
|
|
|
user: httpDigestAuth.user as string,
|
|
|
|
pass: httpDigestAuth.password as string,
|
|
|
|
sendImmediately: false,
|
|
|
|
};
|
|
|
|
}
|
2021-05-19 18:04:43 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const gqlQuery = this.getNodeParameter('query', itemIndex, '') as string;
|
|
|
|
if (requestMethod === 'GET') {
|
2022-01-15 11:05:15 -08:00
|
|
|
if (!requestOptions.qs) {
|
|
|
|
requestOptions.qs = {};
|
|
|
|
}
|
|
|
|
requestOptions.qs.query = gqlQuery;
|
2021-07-19 23:58:54 -07:00
|
|
|
} else {
|
|
|
|
if (requestFormat === 'json') {
|
|
|
|
requestOptions.body = {
|
|
|
|
query: gqlQuery,
|
|
|
|
variables: this.getNodeParameter('variables', itemIndex, {}) as object,
|
|
|
|
operationName: this.getNodeParameter('operationName', itemIndex) as string,
|
|
|
|
};
|
|
|
|
if (typeof requestOptions.body.variables === 'string') {
|
|
|
|
try {
|
|
|
|
requestOptions.body.variables = JSON.parse(requestOptions.body.variables || '{}');
|
|
|
|
} catch (error) {
|
2022-07-12 08:51:01 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Using variables failed:\n' + requestOptions.body.variables + '\n\nWith error message:\n' + error, { itemIndex });
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
2019-10-30 06:02:14 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (requestOptions.body.operationName === '') {
|
|
|
|
requestOptions.body.operationName = null;
|
|
|
|
}
|
|
|
|
requestOptions.json = true;
|
|
|
|
} else {
|
|
|
|
requestOptions.body = gqlQuery;
|
2019-10-30 04:05:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 11:05:15 -08:00
|
|
|
let response;
|
|
|
|
// Now that the options are all set make the actual http request
|
|
|
|
if (oAuth1Api !== undefined) {
|
|
|
|
response = await this.helpers.requestOAuth1.call(this, 'oAuth1Api', requestOptions);
|
|
|
|
} else if (oAuth2Api !== undefined) {
|
|
|
|
response = await this.helpers.requestOAuth2.call(this, 'oAuth2Api', requestOptions, { tokenType: 'Bearer' });
|
|
|
|
} else {
|
|
|
|
response = await this.helpers.request(requestOptions);
|
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (responseFormat === 'string') {
|
|
|
|
const dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;
|
2019-10-30 04:05:52 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
returnItems.push({
|
|
|
|
json: {
|
|
|
|
[dataPropertyName]: response,
|
|
|
|
},
|
|
|
|
});
|
2019-12-19 14:15:19 -08:00
|
|
|
} else {
|
2021-07-19 23:58:54 -07:00
|
|
|
if (typeof response === 'string') {
|
|
|
|
try {
|
2022-04-01 05:38:26 -07:00
|
|
|
response = JSON.parse(response);
|
2021-07-19 23:58:54 -07:00
|
|
|
} catch (error) {
|
2022-07-12 08:51:01 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'Response body is not valid JSON. Change "Response Format" to "String"', { itemIndex });
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
}
|
2022-04-01 05:38:26 -07:00
|
|
|
|
|
|
|
if (response.errors) {
|
|
|
|
const message = response.errors?.map((error: IDataObject) => error.message).join(', ') || 'Unexpected error';
|
|
|
|
throw new NodeApiError(this.getNode(), response.errors, { message });
|
|
|
|
}
|
|
|
|
|
|
|
|
returnItems.push({ json: response });
|
2021-07-19 23:58:54 -07:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
2022-04-01 05:38:26 -07:00
|
|
|
returnItems.push({ json: { error: (error as JsonObject).message } });
|
2021-07-19 23:58:54 -07:00
|
|
|
continue;
|
2019-10-30 04:05:52 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
throw error;
|
2019-10-30 04:05:52 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.prepareOutputData(returnItems);
|
|
|
|
}
|
|
|
|
}
|