You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Dec 19, 2023. It is now read-only.
I'm new to spring boot and graphql, and I'm trying to write a directive that checks that the argument is within a certain numerical range. I wrote a field directive that does this that's very similar to the example in the documentation but when I try to access the values from the SchemaDirectiveWiringEnvironment the value stored in element is null. Also, when I debug the program, it only hits the breakpoint I set in onArgument once, rather than every time I run a query that should be using it.
Here's the code I have in my range directive class (please ignore all the weird type casting as right now I'm just trying to get something working):
@Override
public GraphQLArgument onArgument(SchemaDirectiveWiringEnvironment<GraphQLArgument> schemaDirectiveWiringEnv) {
Double min = (Double) schemaDirectiveWiringEnv.getDirective().getArgument("min").getValue();
Double max = (Double) schemaDirectiveWiringEnv.getDirective().getArgument("max").getValue();
GraphQLArgument argument = schemaDirectiveWiringEnv.getElement();
if (argument.getValue() != null) {
if (Double.parseDouble( (String) argument.getValue()) < min || Double.parseDouble( (String)argument.getValue()) > max)
throw new InvalidArgumentException("Argument value is out of range. The range is " + min + " to " + max + ".", argument.getValue().toString());
}
return argument.transform(builder -> builder
.build()
);
}
and I use the argument directive in a graphqls file here:
extend type Query {
# Find all model features associated with an account
findCardAccountFeaturesByCardAccountId(cardAccountId: String! @range(min: 0.00, max: 1.00)): CardAccountFeatures!
}
Can anyone advise on how to access the argument values properly or provide some resources on writing directives for arguments?
The text was updated successfully, but these errors were encountered:
An argument ONLY has a value when its used in a schema definition language (SDL) context as the arguments to SDL directives. The method should not be called in a query context, but rather the AST / variables map should be used to obtain an arguments value.
I have no idea about how to do the later, but there is an alternative solution. You can use an additional FIELD_DEFINITION directive to lookup the arguments' directives. @bbakerman provided an example here.
@oliemansm Sorry for commenting on this closed issue, but the problem I see with this implementation is that, if you have two range directives on two different arguments, this will be triggered a total of 4 times. onField will be called twice and the check will be applied to all arguments. Is there a way of avoiding this?
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I'm new to spring boot and graphql, and I'm trying to write a directive that checks that the argument is within a certain numerical range. I wrote a field directive that does this that's very similar to the example in the documentation but when I try to access the values from the SchemaDirectiveWiringEnvironment the value stored in element is null. Also, when I debug the program, it only hits the breakpoint I set in onArgument once, rather than every time I run a query that should be using it.
Here's the code I have in my range directive class (please ignore all the weird type casting as right now I'm just trying to get something working):
and I use the argument directive in a graphqls file here:
Can anyone advise on how to access the argument values properly or provide some resources on writing directives for arguments?
The text was updated successfully, but these errors were encountered: