americanexpress / nodes

A GraphQL JVM Client - Java, Kotlin, Scala, etc.
Apache License 2.0
307 stars 70 forks source link

Variables not taken into account - Need help (or bug) #60

Closed hhocine closed 5 years ago

hhocine commented 5 years ago

Hi, My variables don't seem to be taken into account when executing the query

List<Variable> myVars = new ArrayList<Variable>();
myVars.add(new Variable<String>("name", "text"));
myVars.add(new Variable<String>("language", "en"));

GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
            .url(TestGlobalConfiguration.getGraphQlUrl())
            .variables(myVars).request(Jcr.class)
            .build();

GraphQLTemplate graphQLTemplate = new GraphQLTemplate();
GraphQLResponseEntity<Jcr> responseEntity = graphQLTemplate.query(requestEntity, Jcr.class);

In the Model I have defined, one of the property has the variables definition:

@GraphQLVariables({@GraphQLVariable(name = "name", scalar = "String"),
@GraphQLVariable(name = "language", scalar = "String")})
private Property property;

However when executing the request I can see the query is:

query ($name:String,$language:String){ jcr { nodeByPath { marketingFactory { optimizationTestVariant { path name property (name:$name,language:$language) { value } uuid } personalizedVariant { path name property (name:$name,language:$language) { value } uuid } } name uuid } } }

Which obviously returns: Validation error of type VariableTypeMismatch: Variable type doesn't match since $name is not the expected "text"

I used to use GraphQLArguments, which worked fine but when you have the same arguments at two different places it is recommended to use Variables. What am I doing wrong?

hhocine commented 5 years ago

Apparently I needed to write scalar = "String!" to require the parsing of my variable. Not sure why you would not want to parse the variable then....