Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add OAuth, basicAuth, digestAuth and queryAuth support to GraphQL node #2673

Merged
merged 2 commits into from
Jan 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 112 additions & 4 deletions packages/nodes-base/nodes/GraphQL/GraphQL.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,28 @@ export class GraphQL implements INodeType {
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'httpBasicAuth',
required: true,
displayOptions: {
show: {
authentication: [
'basicAuth',
],
},
},
},
{
name: 'httpDigestAuth',
required: true,
displayOptions: {
show: {
authentication: [
'digestAuth',
],
},
},
},
{
name: 'httpHeaderAuth',
required: true,
Expand All @@ -36,17 +58,70 @@ export class GraphQL implements INodeType {
},
},
},
{
name: 'httpQueryAuth',
required: true,
displayOptions: {
show: {
authentication: [
'queryAuth',
],
},
},
},
{
name: 'oAuth1Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth1',
],
},
},
},
{
name: 'oAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Basic Auth',
value: 'basicAuth',
},
{
name: 'Digest Auth',
value: 'digestAuth',
},
{
name: 'Header Auth',
value: 'headerAuth',
},
{
name: 'Query Auth',
value: 'queryAuth',
},
{
name: 'OAuth1',
value: 'oAuth1',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
{
name: 'None',
value: 'none',
Expand Down Expand Up @@ -229,7 +304,12 @@ export class GraphQL implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {

const items = this.getInputData();
const httpBasicAuth = await this.getCredentials('httpBasicAuth');
const httpDigestAuth = await this.getCredentials('httpDigestAuth');
const httpHeaderAuth = await this.getCredentials('httpHeaderAuth');
const httpQueryAuth = await this.getCredentials('httpQueryAuth');
const oAuth1Api = await this.getCredentials('oAuth1Api');
const oAuth2Api = await this.getCredentials('oAuth2Api');

let requestOptions: OptionsWithUri & RequestPromiseOptions;

Expand Down Expand Up @@ -260,15 +340,35 @@ export class GraphQL implements INodeType {
};

// Add credentials if any are set
if (httpBasicAuth !== undefined) {
requestOptions.auth = {
user: httpBasicAuth.user as string,
pass: httpBasicAuth.password as string,
};
}
if (httpHeaderAuth !== undefined) {
requestOptions.headers![httpHeaderAuth.name as string] = httpHeaderAuth.value;
}
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,
};
}

const gqlQuery = this.getNodeParameter('query', itemIndex, '') as string;
if (requestMethod === 'GET') {
requestOptions.qs = {
query: gqlQuery,
};
if (!requestOptions.qs) {
requestOptions.qs = {};
}
requestOptions.qs.query = gqlQuery;
} else {
if (requestFormat === 'json') {
requestOptions.body = {
Expand All @@ -292,7 +392,15 @@ export class GraphQL implements INodeType {
}
}

const response = await this.helpers.request(requestOptions);
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);
}
if (responseFormat === 'string') {
const dataPropertyName = this.getNodeParameter('dataPropertyName', 0) as string;

Expand Down