-
Notifications
You must be signed in to change notification settings - Fork 317
Consider adding defaultValue to @Argument #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
@QueryMapping
public long idLongOrDefault(@Argument Optional<Long> id) {
return id.orElse(-1);
} So as to allow
Your proposal would introduce extra complexity, since there would be two places to define default values (the schema, and in your code). |
@jord1e yes it's indeed in 2 places, but with different behaviour. As I said I can easily workaround it by using Thinking of it, would it maybe be possible to use custom argument resolvers in GraphQL controllers? |
This is currently not possible, the resolvers are hardcoded. Lines 122 to 148 in 5919178
Better yet type Query {
test(id: Int): Int!
} @QueryMapping
public int test(@Argument Optional<Integer> id) {
return id.orElse(-1);
} works See Line 93 in 5919178
|
This feature Edit: "Use @argument to access an argument for the field that maps to the handler method. You can declare such a method parameter to be of any type." is a little vague considering using {
"errors": [
{
"message": "Failed to convert value of type 'java.lang.Integer' to required type 'java.util.OptionalInt'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.Integer' to required type 'java.util.OptionalInt': no matching editors or conversion strategy found",
"locations": [
{
"line": 1,
"column": 2
}
],
"path": [
"test"
],
"extensions": {
"classification": "INTERNAL_ERROR"
}
}
],
"data": null
} Edit 2: Found the list of supported "any type": |
Thx for analyzing this 👍 So besides my proposal for |
Explaining the bigger picture for my case and what I'm trying to achieve in a more declarative way: In my GraphQL schema I have several collections fields which all return a
So it the The I have many more similar collections and I want to avoid repetitively having to code checking for null, min value, max value etc. I would love to define a custom argument handler like:
Note that the My my names used above are confusing as it is basically a copy from Spring Data.... |
Just a clarification question for now. In the snippets, the query returns |
Besides the GraphQL
Note this has a bit of similarity with the Relay connection spec, but is different. It uses a simpler limit/offset paging model sufficient and more easier in my case. |
It would be nice to understand Spring's team take on this (without having an immediate implementation). |
There are several requests in this, so let me start with the original question only for now. Initially we had both You raise a good point that a default value in the schema does not seem to apply to Perhaps we can consider some boolean flag on |
Do you mean: type query {
a(myArg: Int = 5): Int!
} with a resolver @QueryMapping
public int a(@Argument(fallbackIfNull = true) int myArg) {
return myArg;
} Which would return I would strongly favour just using Integer, or adding an argument resolver for OptionalInt. I really don't find this an ideal situation, why would you want this in the first place?. There is even a difference between As far as I am aware no other JVM or GraphQL framework implements this functionality. |
Thx @rstoyanchev for replying.
Clients passing Other question (and yes I understand in this threads many questions / alternatives are discussed 🙈 ) what is your take on the ability to provide / register custom custom argument resolvers as in normal MVC controllers? E.g. to do something like:
or even:
|
Custom argument resolvers is just something that hasn't come up but nothing against it. It does feel though like |
@jord1e, I understand no value vs This undermines the value of declaring a default value in the schema, especially for Java applications where there isn't a distinction between I'm now thinking that |
Team decision: In terms of options on how we can help to handle the |
I agree and it's a difficult "problem". |
Note reading this comment from @leebyron : graphql/graphql-js#210 (comment)
graphql-js seems to use the default value from the GraphQL schema when |
That comment is from 2015 when no See graphql/graphql-js#544 which negates the behavior you (and then Lee) are describing This test case may also be of your interest: Logic for seperate |
Similar as for traditional controllers the
@RequestParam
annotation supports adefaultValue
, it would be nice to have this for the GraphQL@Argument
annotation as well.Rationale:
Imagine this
Query
definition:here default values are specified in the GraphQL schema, and if not provided by the user the defaults are used automatically.
But the above
Season
type could have the following field:here the
first
argument has no default value in the GraphQL schema, basically meaning there is no limit and by default all standings are returned unless the limit is explicitly set by the client.Now having a
SchemaMapping
for this like:will fail as
null
is an illegal argument for thefirst
argument.It would be nice to define the
SchemaMapping
like:See the arbitrary
99999
as default value which in this proposal will be used when the argument is not passed, or set to null by the client.I don't want the
99999
in the GraphQL schema itself as it is an arbitrary value without a real meaning,unlike
offset = 0
orfirst = 10
in the query mapping.Without a
defaultValue
I can solve it on other ways to:99999
to the GraphQL schema. But even then a client could passnull
which will break the controller@Argument Integer first
and test myself onnull
; this will also make sure the controller won't break.The text was updated successfully, but these errors were encountered: