From 70c188aa1b136fe769b15a3840d332464b20299a Mon Sep 17 00:00:00 2001 From: Aya Tanikawa <15815271+ayatnkw@users.noreply.github.com> Date: Tue, 22 Oct 2024 14:41:15 +0300 Subject: [PATCH] fix(GraphQL Node): Change default request format to json instead of graphql --- .../nodes-base/nodes/GraphQL/GraphQL.node.ts | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/nodes-base/nodes/GraphQL/GraphQL.node.ts b/packages/nodes-base/nodes/GraphQL/GraphQL.node.ts index 07d07011b4..1cd2d5b77a 100644 --- a/packages/nodes-base/nodes/GraphQL/GraphQL.node.ts +++ b/packages/nodes-base/nodes/GraphQL/GraphQL.node.ts @@ -175,12 +175,16 @@ export class GraphQL implements INodeType { required: true, options: [ { - name: 'GraphQL (Raw)', - value: 'graphql', + name: 'JSON (Recommended)', + value: 'json', + description: + 'JSON object with query, variables, and operationName properties. The standard and most widely supported format for GraphQL requests.', }, { - name: 'JSON', - value: 'json', + name: 'GraphQL (Raw)', + value: 'graphql', + description: + 'Raw GraphQL query string. Not all servers support this format. Use JSON for better compatibility.', }, ], displayOptions: { @@ -188,23 +192,26 @@ export class GraphQL implements INodeType { requestMethod: ['POST'], }, }, - default: 'graphql', - description: 'The format for the query payload', + default: 'json', + description: 'The request format for the query payload', }, { displayName: 'Query', name: 'query', - type: 'json', + type: 'string', default: '', description: 'GraphQL query', required: true, + typeOptions: { + rows: 6, + }, }, { displayName: 'Variables', name: 'variables', type: 'json', default: '', - description: 'Query variables', + description: 'Query variables as JSON object', displayOptions: { show: { requestFormat: ['json'], @@ -350,11 +357,7 @@ export class GraphQL implements INodeType { 'POST', ) as IHttpRequestMethods; const endpoint = this.getNodeParameter('endpoint', itemIndex, '') as string; - const requestFormat = this.getNodeParameter( - 'requestFormat', - itemIndex, - 'graphql', - ) as string; + const requestFormat = this.getNodeParameter('requestFormat', itemIndex, 'json') as string; const responseFormat = this.getNodeParameter('responseFormat', 0) as string; const { parameter }: { parameter?: Array<{ name: string; value: string }> } = this.getNodeParameter('headerParametersUi', itemIndex, {}) as IDataObject; @@ -484,7 +487,20 @@ export class GraphQL implements INodeType { } else { if (typeof response === 'string') { try { - response = JSON.parse(response); + if (typeof response === 'string') { + response = JSON.parse(response) as IDataObject; + } + + // Check for errors in the response + if (response.errors && Array.isArray(response.errors)) { + // If the request format is 'graphql', throw an error suggesting to try JSON + if (requestFormat === 'graphql') { + throw new NodeOperationError( + this.getNode(), + 'Error in GraphQL request. Please try using the Request format "JSON" instead.', + ); + } + } } catch (error) { throw new NodeOperationError( this.getNode(),