Skip to content

Commit

Permalink
fix(GraphQL Node): Fix request format JSON error (#8646)
Browse files Browse the repository at this point in the history
  • Loading branch information
maspio authored and netroy committed Feb 16, 2024
1 parent c4e259b commit 1e0370c
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/nodes-base/nodes/GraphQL/GraphQL.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -449,6 +448,7 @@ export class GraphQL implements INodeType {
jsonBody.operationName = null;
}
requestOptions.json = true;
requestOptions.body = jsonBody;
} else {
requestOptions.body = gqlQuery;
}
Expand Down
54 changes: 54 additions & 0 deletions packages/nodes-base/nodes/GraphQL/test/GraphQL.node.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
42 changes: 42 additions & 0 deletions packages/nodes-base/nodes/GraphQL/test/workflow.json
Original file line number Diff line number Diff line change
@@ -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": {}
}
9 changes: 9 additions & 0 deletions packages/workflow/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1e0370c

Please sign in to comment.