fix kriti template generation for array type

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/10865
GitOrigin-RevId: ae4b0646dce567e15b03050bc424e17e51f10737
This commit is contained in:
Varun Choudhary 2024-06-13 16:50:51 +05:30 committed by hasura-bot
parent 8dae5ae742
commit 81049db4d8
2 changed files with 5 additions and 4 deletions

View File

@ -90,7 +90,7 @@ describe('generateAction', () => {
),
headers: [],
queryParams:
'{{ concat ([concat({{ range _, x := $body.input?.tags }} "tags={{x}}&" {{ end }}), "limit={{$body.input?.limit}}&"]) }}',
'{{ concat ([{{ if empty($body.input?.tags) }} [] {{ else }} concat({{ range _, x := $body.input?.tags }} "tags={{x}}&" {{ end }}) {{ end }}, "limit={{$body.input?.limit}}&"]) }}',
});
});
@ -111,13 +111,14 @@ describe('generateQueryParams', () => {
it('should generate query params with one non-array param and one array param', async () => {
const queryParams = await generateQueryParams([status, tags]);
expect(queryParams).toBe(
'{{ concat (["status={{$body.input?.status}}&", concat({{ range _, x := $body.input?.tags }} "tags={{x}}&" {{ end }})]) }}'
'{{ concat (["status={{$body.input?.status}}&", {{ if empty($body.input?.tags) }} [] {{ else }} concat({{ range _, x := $body.input?.tags }} "tags={{x}}&" {{ end }}) {{ end }}]) }}'
);
});
it('should generate query params with one non-array param and one array param (reversed)', async () => {
const queryParams = await generateQueryParams([tags, status]);
console.log(queryParams);
expect(queryParams).toBe(
'{{ concat ([concat({{ range _, x := $body.input?.tags }} "tags={{x}}&" {{ end }}), "status={{$body.input?.status}}&"]) }}'
'{{ concat ([{{ if empty($body.input?.tags) }} [] {{ else }} concat({{ range _, x := $body.input?.tags }} "tags={{x}}&" {{ end }}) {{ end }}, "status={{$body.input?.status}}&"]) }}'
);
});
});

View File

@ -74,7 +74,7 @@ export const generateQueryParams = (parameters: OperationParameters) => {
if (isThereArray) {
const stringParams = parameters.map(param => {
if (isSchemaObject(param?.schema) && param.schema.type === 'array') {
return `concat({{ range _, x := $body.input?.${param.name} }} "${param.name}={{x}}&" {{ end }})`;
return `{{ if empty($body.input?.${param.name}) }} [] {{ else }} concat({{ range _, x := $body.input?.${param.name} }} "${param.name}={{x}}&" {{ end }}) {{ end }}`;
}
return `"${param.name}={{$body.input?.${param.name}}}&"`;
});