fix(GraphQL Node): Fix request format JSON error (#8646)

This commit is contained in:
Marcus 2024-02-15 17:49:18 +01:00 committed by GitHub
parent c4e259bcfc
commit bd4b50cf08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 108 additions and 3 deletions

View file

@ -424,9 +424,8 @@ export class GraphQL implements INodeType {
requestOptions.qs.query = gqlQuery;
} else {
if (requestFormat === 'json') {
const jsonBody = requestOptions.body as IDataObject;
requestOptions.body = {
...jsonBody,
const jsonBody = {
...requestOptions.body,
query: gqlQuery,
variables: this.getNodeParameter('variables', itemIndex, {}) as object,
operationName: this.getNodeParameter('operationName', itemIndex) as string,
@ -449,6 +448,7 @@ export class GraphQL implements INodeType {
jsonBody.operationName = null;
}
requestOptions.json = true;
requestOptions.body = jsonBody;
} else {
requestOptions.body = gqlQuery;
}

View file

@ -0,0 +1,54 @@
import type { WorkflowTestData } from '@test/nodes/types';
import { executeWorkflow } from '@test/nodes/ExecuteWorkflow';
import * as Helpers from '@test/nodes/Helpers';
describe('GraphQL Node', () => {
const mockResponse = {
data: {
nodes: {},
},
};
const tests: WorkflowTestData[] = [
{
description: 'should run Request Format JSON',
input: {
workflowData: Helpers.readJsonFileSync('nodes/GraphQL/test/workflow.json'),
},
output: {
nodeExecutionOrder: ['Start'],
nodeData: {
'Fetch Request Format JSON': [
[
{
json: mockResponse,
},
],
],
},
},
nock: {
baseUrl: 'https://api.n8n.io',
mocks: [
{
method: 'post',
path: '/graphql',
statusCode: 200,
responseBody: mockResponse,
},
],
},
},
];
const nodeTypes = Helpers.setup(tests);
test.each(tests)('$description', async (testData) => {
const { result } = await executeWorkflow(testData, nodeTypes);
const resultNodeData = Helpers.getResultNodeData(result, testData);
resultNodeData.forEach(({ nodeName, resultData }) =>
expect(resultData).toEqual(testData.output.nodeData[nodeName]),
);
expect(result.finished).toEqual(true);
});
});

View file

@ -0,0 +1,42 @@
{
"meta": {
"templateCredsSetupCompleted": true,
"instanceId": "104a4d08d8897b8bdeb38aaca515021075e0bd8544c983c2bb8c86e6a8e6081c"
},
"nodes": [
{
"parameters": {},
"id": "fb826323-2e48-4f11-bb0e-e12de32e22ee",
"name": "When clicking \"Test workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [180, 160]
},
{
"parameters": {
"endpoint": "https://api.n8n.io/graphql",
"requestFormat": "json",
"query": "query {\n nodes(pagination: { limit: 1 }) {\n data {\n id\n attributes {\n name\n displayName\n description\n group\n codex\n createdAt\n }\n }\n }\n}"
},
"name": "Fetch Request Format JSON",
"type": "n8n-nodes-base.graphql",
"typeVersion": 1,
"position": [420, 160],
"id": "7f8ceaf4-b82f-48d5-be0b-9fe3bfb35ee4"
}
],
"connections": {
"When clicking \"Test workflow\"": {
"main": [
[
{
"node": "Fetch Request Format JSON",
"type": "main",
"index": 0
}
]
]
}
},
"pinData": {}
}

View file

@ -2125,6 +2125,15 @@ export interface WorkflowTestData {
[key: string]: any[][];
};
};
nock: {
baseUrl: string;
mocks: Array<{
method: string;
path: string;
statusCode: number;
responseBody: any;
}>;
};
trigger?: {
mode: WorkflowExecuteMode;
input: INodeExecutionData;