Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

feat(GraphQLProperty): support applying variables in the property annotation #18

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions nodes/src/main/java/io/aexp/nodes/graphql/GraphQLRequestEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,19 @@ private void setPropertiesFromClass(Class clazz) {
GraphQLProperty graphQLProperty = (GraphQLProperty) clazz.getAnnotation(GraphQLProperty.class);
if (graphQLProperty != null) {
Property resourceProperty = new Property();
List<Argument> arguments = null;
List<Argument> arguments = new ArrayList<Argument>();
String resourceName = graphQLProperty.name();
if (graphQLProperty.arguments().length > 0) {
arguments = new ArrayList<Argument>();
for (GraphQLArgument graphQLArgument : graphQLProperty.arguments()) {
arguments = setArgument(arguments, graphQLArgument);
}
}
resourceProperty.setArguments(arguments);
if (graphQLProperty.variables().length > 0) {
for (GraphQLVariable graphQLVariable : graphQLProperty.variables()) {
arguments = setVariable(arguments, graphQLVariable, propertyVariables);
}
}
if (arguments.size() > 0) resourceProperty.setArguments(arguments);
resourceProperty.setChildren(children);
Map<String, Property> resourceChild = new HashMap<String, Property>();
resourceChild.put(resourceName, resourceProperty);
Expand Down Expand Up @@ -222,6 +226,21 @@ private List<Argument> setArgument(List<Argument> arguments, GraphQLArgument gra
return arguments;
}

/**
* Adds the variables into the argument list from the GraphQLVariable annotation and sets it to the property
*
* @param arguments list of arguments to add the annotated argument to
* @param graphQLVariable annotated variable to add to the request construction
* @param propertyVariables list of variables on the property
* @return List\<Argument>
*/
private List<Argument> setVariable(List<Argument> arguments, GraphQLVariable graphQLVariable, Map<String, Object> propertyVariables) {
if (arguments == null) arguments = new ArrayList<Argument>();
arguments.add(new Argument<String>(graphQLVariable.name(), "$" + graphQLVariable.name()));
propertyVariables.put("$" + graphQLVariable.name(), graphQLVariable.scalar());
return arguments;
}

/**
* Recursively iterates over nested classes building the request tree while checking for any annotations to apply to
* the fields.
Expand Down Expand Up @@ -250,12 +269,11 @@ private Map<String, Property> getChildren(Class clazz, Map<String, Object> prope
}

String propertyKey = field.getName();
List<Argument> arguments = null;
List<Argument> arguments = new ArrayList<Argument>();
Property property = new Property();

GraphQLProperty propertyAnnotation = field.getAnnotation(GraphQLProperty.class);
if (propertyAnnotation != null) {
arguments = new ArrayList<Argument>();
String name = propertyAnnotation.name();
property.setResourceName(name);
propertyKey = field.getName();
Expand All @@ -265,34 +283,28 @@ private Map<String, Property> getChildren(Class clazz, Map<String, Object> prope
}
GraphQLArguments argumentsAnnotation = field.getAnnotation(GraphQLArguments.class);
if (argumentsAnnotation != null) {
if (arguments == null) arguments = new ArrayList<Argument>();
for (GraphQLArgument graphQLArgument : argumentsAnnotation.value()) {
arguments = setArgument(arguments, graphQLArgument);
}
}
GraphQLArgument graphQLArgument = field.getAnnotation(GraphQLArgument.class);
if (graphQLArgument != null) {
if (arguments == null) arguments = new ArrayList<Argument>();
arguments = setArgument(arguments, graphQLArgument);
}

GraphQLVariables variablesAnnotation = field.getAnnotation(GraphQLVariables.class);
if (variablesAnnotation != null) {
if (arguments == null) arguments = new ArrayList<Argument>();
for (GraphQLVariable graphQLVariable : variablesAnnotation.value()) {
arguments.add(new Argument<String>(graphQLVariable.name(), "$" + graphQLVariable.name()));
propertyVariables.put("$" + graphQLVariable.name(), graphQLVariable.scalar());
arguments = setVariable(arguments, graphQLVariable, propertyVariables);
}
}

GraphQLVariable graphQLVariable = field.getAnnotation(GraphQLVariable.class);
if (graphQLVariable != null) {
if (arguments == null) arguments = new ArrayList<Argument>();
arguments.add(new Argument<String>(graphQLVariable.name(), "$" + graphQLVariable.name()));
propertyVariables.put("$" + graphQLVariable.name(), graphQLVariable.scalar());
arguments = setVariable(arguments, graphQLVariable, propertyVariables);
}

property.setArguments(arguments);
if (arguments.size() > 0) property.setArguments(arguments);

if (isList(field)) {
Type type = field.getGenericType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@

String name();
GraphQLArgument[] arguments() default {};
GraphQLVariable[] variables() default {};

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because variables are now definable w/ @GraphQLProperty -- they are definable at class level now through GraphQLProperty only. Do you want to update @GraphQLVariable(s) annotation to class level as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep! I was going to add it here first and then another PR for both the GraphQLArgument and GraphQLVariable to be definable at the class level.
Just wanted to make this PR first for all the logic to handle a class level definition of it

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay.

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,10 @@ public void queryVarArgs() throws MalformedURLException {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Authorization", "some token");

InputObject inputObject = new InputObject.Builder<Argument>()
.put("someKey", new Argument<String>("innerKey", "someValue"))
.build();

GraphQLRequestEntity requestEntity = GraphQLRequestEntity.Builder()
.request(TestModel.class)
.url(mockServerUrl.toString())
.arguments(new Arguments("test", new Argument<InputObject>("parameters", inputObject)))
.arguments(new Arguments("test", new Argument<String>("id", "someId")))
.variables(new Variable<String>("someVariableKey", "someVariableValue"))
.scalars(BigDecimal.class, BigInteger.class)
.headers(headers)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@
package io.aexp.nodes.graphql.models;

import io.aexp.nodes.graphql.annotations.GraphQLProperty;
import io.aexp.nodes.graphql.annotations.GraphQLVariable;

import java.util.Arrays;

@GraphQLProperty(name = "test")
@GraphQLProperty(name = "test", variables = {@GraphQLVariable(name = "aVariable", scalar = "String")})
public class TestModelExtended extends TestModel {
String test;
boolean[] testPrimitiveArray;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public void extendedModel() throws GraphQLException, MalformedURLException {
.url(EXAMPLE_URL)
.request(TestModelExtended.class)
.build();
assertEquals("query ($andAnothaVariable:status,$anothaVariable:Int,$andAListVariable:[String],$variableName:String!){ test { testInteger testBoolean nestedTest { anotherTestString (variableName:$variableName) andAnothaOne (anothaVariable:$anothaVariable,andAnothaVariable:$andAnothaVariable,andAListVariable:$andAListVariable) } test testByte testFloat testLong testShort testCharacter testList { anotherTestString (variableName:$variableName) andAnothaOne (anothaVariable:$anothaVariable,andAnothaVariable:$andAnothaVariable,andAListVariable:$andAListVariable) } testPrimitiveArray testString : testString(anotherOne:null,default:\"default\",defaultList:null) testArrayList testDouble } } ", requestEntity.getRequest());
assertEquals("query ($andAnothaVariable:status,$anothaVariable:Int,$aVariable:String,$andAListVariable:[String],$variableName:String!){ test (aVariable:$aVariable) { testInteger testBoolean nestedTest { anotherTestString (variableName:$variableName) andAnothaOne (anothaVariable:$anothaVariable,andAnothaVariable:$andAnothaVariable,andAListVariable:$andAListVariable) } test testByte testFloat testLong testShort testCharacter testList { anotherTestString (variableName:$variableName) andAnothaOne (anothaVariable:$anothaVariable,andAnothaVariable:$andAnothaVariable,andAListVariable:$andAListVariable) } testPrimitiveArray testString : testString(anotherOne:null,default:\"default\",defaultList:null) testArrayList testDouble } } ", requestEntity.getRequest());
assertTrue(requestEntity.getVariables().isEmpty());
}

Expand Down