Argument directive [QUESTION] #188
Description
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?