Skip to content

Commit

Permalink
[UXE-6214] fix: improve GraphQL type handling for complex filter quer…
Browse files Browse the repository at this point in the history
…ies (#2182)
  • Loading branch information
lucasmendes21 authored Feb 19, 2025
1 parent aac149f commit c2c4042
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 17 additions & 3 deletions src/helpers/convert-gql.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ const convertGQL = (filter, table) => {
if (!table) throw new Error('Table parameter is required')

let variables = {}
const fields = filter?.fields || []
const filterQuery = buildFilterQuery(filter, variables)
const fieldsFormat = table.fields.map((field) => `\t\t${field}`).join('\n')
const filterParameter = formatFilterParameter(variables)
const filterParameter = formatFilterParameter(variables, fields)
variables = formatValueContainOperator(variables)

const queryConfig = {
Expand Down Expand Up @@ -216,8 +217,21 @@ const buildFilterQueriesWithPrefix = (filter, variables, prefix) => {
* @param {Object} variables
* @returns {String} formattedFilterParameter
*/
const formatFilterParameter = (variables) => {
return Object.keys(variables).map((key) => `\t$${key}: ${getGraphQLType(variables[key])}!`)
const formatFilterParameter = (variables, fields) => {
return Object.keys(variables).map((key) => {
let type
if (key.startsWith('and_')) {
const fieldKey = key.replace('and_', '')
const field = fields.find(
({ valueField, operator }) => `${valueField}${operator}` === fieldKey
)
type = field && field?.type ? field.type : getGraphQLType(variables[key])
} else {
type = getGraphQLType(variables[key])
}

return `\t$${key}: ${type}!`
})
}

export default convertGQL
6 changes: 4 additions & 2 deletions src/tests/helpers/convert-gql.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ describe('convertGQL', () => {
{ value: 'value3', label: 'test1' },
{ value: 'value4', label: 'test2' }
]
}
},
fields: [{ valueField: 'value2', operator: 'Eq', value: 'value2' }]
}

const { sut } = makeSut()
Expand All @@ -40,7 +41,8 @@ describe('convertGQL', () => {
and_field2: 'value2',
in_field3: ['value3', 'value4'],
tsRange_begin: '2022-01-01',
tsRange_end: '2022-01-31'
tsRange_end: '2022-01-31',
and_value2Eq: 'value2'
}
})
})
Expand Down

0 comments on commit c2c4042

Please sign in to comment.