I believe the goal was to map more of the DQL functionality to the GraphQL generated schema. The has filter was first available in DQL (has function docs). So when this filter was brought into GraphQL, it was introduced with very similar syntax, e.g. every has function only accepts a single parameter and multiple has functions could be connected with AND, OR, and NOT logic. This provides a simpler GraphQL to DQL rewriting script.
You could somewhat easily create a script to rewrite your two examples into acceptable syntax:
const hasArrayFormatter = (fields) => {
if (!Array.isArray(fields) || fields.length < 1) return {}
fields = fields.map(field => {
return { has: field }
})
return {
and: fields
}
}
const hasArray = ["name","age"]
const formattedHas = hasArrayFormatter(hasArray)
response: { and: [ { has: "name" }, { has: "age" } ] }
const hasPropsFormatter = (fields) => {
if (typeof fields !== 'object' || fields === null || Object.keys(fields).length === 0) return {}
const filters = []
for(const [field, required] of Object.entries(fields)) {
if (required) {
filters.push({ has: field })
} else {
filters.push({ not: { has: field } })
}
}
return { and: filters }
}
const hasProps = { name: true, age: false }
const formattedHas = hasPropsFormatter(hasProps)
response: { and: [ { has: "name" }, { not: { has: "age" } } ] }
You could then plug these straight into your query:
const query = gql`
query Students($filter: StudentFilter) {
queryStudent(filter: $filter) {
tid
age
name
}
}
`
// ...
const {data} = useQuery(query, { variables: { filter: formattedHas } })
// ...