Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Argument directive [QUESTION] #188

Closed
carolinebholmes opened this issue Jan 17, 2019 · 3 comments
Closed

Argument directive [QUESTION] #188

carolinebholmes opened this issue Jan 17, 2019 · 3 comments
Labels
Milestone

Comments

@carolinebholmes
Copy link

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?

@greenled
Copy link

greenled commented Feb 3, 2019

The javadoc says this:

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 oliemansm added this to the 5.9.1 milestone Jun 20, 2019
@oliemansm
Copy link
Member

The documentation is outdated. I've created a sample here for the latest version that works.

@fediazgon
Copy link

@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 free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants