spring-graphql
spring-graphql copied to clipboard
QueryDSL datafetcher should support default arguments
Given a UsefulThingRepository
which implements QuerydslPredicateExecutor<UsefulThing>
.
The UsefulThing
entity has an owner
field.
When a user executes the query { usefulThing { name } }
query, they should only get the items they own.
This works when owner
is provided as a query argument.
However, in this case it makes sense to add the WHERE clause by default instead of relying on a query argument.
I would expect something like this work to add a default WHERE
clause on the owner
field.
var many = QuerydslDataFetcher.builder(repository)
.customizer((bindings, root) -> bindings.bind(QUsefulThing.usefulThing.owner)
.first((path, value) -> path.eq(ssoCallerResolver.get().username).many();
Note that in this example the ssoCallerResolver.get().username
is just an example of something that would give the current user form the request.
In QuerydslPredicateBuilder
on line 92 there is a check on values
. Since the value is not user provided, this will be empty, and it won't build a predicate.
QuerydslBindings
are designed to be fed with request parameters and all operations following bindings.bind(…)
expect request values to be consumed by the following methods.
What you're looking for is providing a default value that is either provided by a supplier function or held statically and then applied to the Querydsl binding.
I filed https://github.com/spring-projects/spring-data-commons/issues/3017 to track the concern in Spring Data.
Your code shows, contrary to asking for defaulting, to override a request value. Care to clarify what the actual request is?