mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
fix(GraphQL Node): Throw error if GraphQL variables are not objects or strings (#11904)
This commit is contained in:
@@ -418,40 +418,49 @@ export class GraphQL implements INodeType {
|
||||
|
||||
const gqlQuery = this.getNodeParameter('query', itemIndex, '') as string;
|
||||
if (requestMethod === 'GET') {
|
||||
if (!requestOptions.qs) {
|
||||
requestOptions.qs = {};
|
||||
}
|
||||
requestOptions.qs = requestOptions.qs ?? {};
|
||||
requestOptions.qs.query = gqlQuery;
|
||||
} else {
|
||||
if (requestFormat === 'json') {
|
||||
const jsonBody = {
|
||||
...requestOptions.body,
|
||||
query: gqlQuery,
|
||||
variables: this.getNodeParameter('variables', itemIndex, {}) as object,
|
||||
operationName: this.getNodeParameter('operationName', itemIndex) as string,
|
||||
};
|
||||
if (typeof jsonBody.variables === 'string') {
|
||||
try {
|
||||
jsonBody.variables = JSON.parse(jsonBody.variables || '{}');
|
||||
} catch (error) {
|
||||
throw new NodeOperationError(
|
||||
this.getNode(),
|
||||
'Using variables failed:\n' +
|
||||
(jsonBody.variables as string) +
|
||||
'\n\nWith error message:\n' +
|
||||
(error as string),
|
||||
{ itemIndex },
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (requestFormat === 'json') {
|
||||
const variables = this.getNodeParameter('variables', itemIndex, {});
|
||||
|
||||
let parsedVariables;
|
||||
if (typeof variables === 'string') {
|
||||
try {
|
||||
parsedVariables = JSON.parse(variables || '{}');
|
||||
} catch (error) {
|
||||
throw new NodeOperationError(
|
||||
this.getNode(),
|
||||
`Using variables failed:\n${variables}\n\nWith error message:\n${error}`,
|
||||
{ itemIndex },
|
||||
);
|
||||
}
|
||||
if (jsonBody.operationName === '') {
|
||||
jsonBody.operationName = null;
|
||||
}
|
||||
requestOptions.json = true;
|
||||
requestOptions.body = jsonBody;
|
||||
} else if (typeof variables === 'object' && variables !== null) {
|
||||
parsedVariables = variables;
|
||||
} else {
|
||||
requestOptions.body = gqlQuery;
|
||||
throw new NodeOperationError(
|
||||
this.getNode(),
|
||||
`Using variables failed:\n${variables}\n\nGraphQL variables should be either an object or a string.`,
|
||||
{ itemIndex },
|
||||
);
|
||||
}
|
||||
|
||||
const jsonBody = {
|
||||
...requestOptions.body,
|
||||
query: gqlQuery,
|
||||
variables: parsedVariables,
|
||||
operationName: this.getNodeParameter('operationName', itemIndex) as string,
|
||||
};
|
||||
|
||||
if (jsonBody.operationName === '') {
|
||||
jsonBody.operationName = null;
|
||||
}
|
||||
|
||||
requestOptions.json = true;
|
||||
requestOptions.body = jsonBody;
|
||||
} else {
|
||||
requestOptions.body = gqlQuery;
|
||||
}
|
||||
|
||||
let response;
|
||||
@@ -509,22 +518,19 @@ export class GraphQL implements INodeType {
|
||||
throw new NodeApiError(this.getNode(), response.errors as JsonObject, { message });
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
const errorData = this.helpers.returnJsonArray({
|
||||
$error: error,
|
||||
json: this.getInputData(itemIndex),
|
||||
itemIndex,
|
||||
});
|
||||
const exectionErrorWithMetaData = this.helpers.constructExecutionMetaData(errorData, {
|
||||
itemData: { item: itemIndex },
|
||||
});
|
||||
returnItems.push(...exectionErrorWithMetaData);
|
||||
continue;
|
||||
if (!this.continueOnFail()) {
|
||||
throw error;
|
||||
}
|
||||
throw error;
|
||||
|
||||
const errorData = this.helpers.returnJsonArray({
|
||||
error: error.message,
|
||||
});
|
||||
const exectionErrorWithMetaData = this.helpers.constructExecutionMetaData(errorData, {
|
||||
itemData: { item: itemIndex },
|
||||
});
|
||||
returnItems.push(...exectionErrorWithMetaData);
|
||||
}
|
||||
}
|
||||
|
||||
return [returnItems];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user