fix(GraphQL Node): Change default request format to json instead of graphql

This commit is contained in:
Aya Tanikawa 2024-10-22 14:41:15 +03:00
parent 72e6343964
commit 70c188aa1b

View file

@ -175,12 +175,16 @@ export class GraphQL implements INodeType {
required: true, required: true,
options: [ options: [
{ {
name: 'GraphQL (Raw)', name: 'JSON (Recommended)',
value: 'graphql', value: 'json',
description:
'JSON object with query, variables, and operationName properties. The standard and most widely supported format for GraphQL requests.',
}, },
{ {
name: 'JSON', name: 'GraphQL (Raw)',
value: 'json', value: 'graphql',
description:
'Raw GraphQL query string. Not all servers support this format. Use JSON for better compatibility.',
}, },
], ],
displayOptions: { displayOptions: {
@ -188,23 +192,26 @@ export class GraphQL implements INodeType {
requestMethod: ['POST'], requestMethod: ['POST'],
}, },
}, },
default: 'graphql', default: 'json',
description: 'The format for the query payload', description: 'The request format for the query payload',
}, },
{ {
displayName: 'Query', displayName: 'Query',
name: 'query', name: 'query',
type: 'json', type: 'string',
default: '', default: '',
description: 'GraphQL query', description: 'GraphQL query',
required: true, required: true,
typeOptions: {
rows: 6,
},
}, },
{ {
displayName: 'Variables', displayName: 'Variables',
name: 'variables', name: 'variables',
type: 'json', type: 'json',
default: '', default: '',
description: 'Query variables', description: 'Query variables as JSON object',
displayOptions: { displayOptions: {
show: { show: {
requestFormat: ['json'], requestFormat: ['json'],
@ -350,11 +357,7 @@ export class GraphQL implements INodeType {
'POST', 'POST',
) as IHttpRequestMethods; ) as IHttpRequestMethods;
const endpoint = this.getNodeParameter('endpoint', itemIndex, '') as string; const endpoint = this.getNodeParameter('endpoint', itemIndex, '') as string;
const requestFormat = this.getNodeParameter( const requestFormat = this.getNodeParameter('requestFormat', itemIndex, 'json') as string;
'requestFormat',
itemIndex,
'graphql',
) as string;
const responseFormat = this.getNodeParameter('responseFormat', 0) as string; const responseFormat = this.getNodeParameter('responseFormat', 0) as string;
const { parameter }: { parameter?: Array<{ name: string; value: string }> } = const { parameter }: { parameter?: Array<{ name: string; value: string }> } =
this.getNodeParameter('headerParametersUi', itemIndex, {}) as IDataObject; this.getNodeParameter('headerParametersUi', itemIndex, {}) as IDataObject;
@ -484,7 +487,20 @@ export class GraphQL implements INodeType {
} else { } else {
if (typeof response === 'string') { if (typeof response === 'string') {
try { 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) { } catch (error) {
throw new NodeOperationError( throw new NodeOperationError(
this.getNode(), this.getNode(),