Skip to content

Commit

Permalink
Changed VariableAccumulator so it can represent itself with null valu…
Browse files Browse the repository at this point in the history
…es (#2816)

* Changed VariableAccumulator so it can repreent itself with null values - Collector.toMap will not allow null values

* Better test
  • Loading branch information
bbakerman committed May 5, 2022
1 parent b424f98 commit a3a13d8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main/java/graphql/normalized/VariableAccumulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -52,10 +53,11 @@ public List<VariableDefinition> getVariableDefinitions() {
* @return the map of variable names to variable values
*/
public Map<String, Object> getVariablesMap() {
return valueWithDefinitions.stream()
.collect(Collectors.toMap(
variableWithDefinition -> variableWithDefinition.getDefinition().getName(),
VariableValueWithDefinition::getValue
));
Map<String, Object> map = new LinkedHashMap<>();
valueWithDefinitions.forEach(variableWithDefinition -> {
String name = variableWithDefinition.getDefinition().getName();
map.put(name, variableWithDefinition.getValue());
});
return map;
}
}
46 changes: 46 additions & 0 deletions src/test/groovy/graphql/normalized/VariableAccumulatorTest.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package graphql.normalized

import graphql.language.IntValue
import graphql.language.NullValue
import graphql.language.StringValue
import graphql.language.TypeName
import spock.lang.Specification

class VariableAccumulatorTest extends Specification {

def alwaysTruePredicate = new VariablePredicate() {
@Override
boolean shouldMakeVariable(ExecutableNormalizedField executableNormalizedField, String argName, NormalizedInputValue normalizedInputValue) {
return true
}
}

def "can build itself handing null values"() {
when:
VariableAccumulator accumulator = new VariableAccumulator(alwaysTruePredicate)
accumulateData(accumulator)

def variablesMap = accumulator.getVariablesMap()
then:
variablesMap == [v0: "hello", v1: 666, v2: null, v3: null]
}

def "can build variable definitions"() {
when:
VariableAccumulator accumulator = new VariableAccumulator(alwaysTruePredicate)
accumulateData(accumulator)
def names = accumulator.getVariableDefinitions().collect { vd -> vd.name }
def typeNames = accumulator.getVariableDefinitions().collect { vd -> (vd.type as TypeName).name }

then:
names == ["v0", "v1", "v2", "v3"]
typeNames == ["String", "Int", "Int", "String"]
}

private void accumulateData(VariableAccumulator accumulator) {
accumulator.accumulateVariable(new NormalizedInputValue("String", StringValue.of("hello")))
accumulator.accumulateVariable(new NormalizedInputValue("Int", IntValue.of(666)))
accumulator.accumulateVariable(new NormalizedInputValue("Int", NullValue.of()))
accumulator.accumulateVariable(new NormalizedInputValue("String", null))
}
}

0 comments on commit a3a13d8

Please sign in to comment.