Skip to content

Commit 3dad4df

Browse files
committed
more field arguments
1 parent 3fa91cd commit 3dad4df

5 files changed

Lines changed: 150 additions & 8 deletions

File tree

‎src/main/java/graphql/language/BooleanValue.java‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,41 @@
22

33

44
public class BooleanValue implements Value{
5+
6+
private boolean value;
7+
8+
public BooleanValue(boolean value) {
9+
this.value = value;
10+
}
11+
12+
public boolean isValue() {
13+
return value;
14+
}
15+
16+
public void setValue(boolean value) {
17+
this.value = value;
18+
}
19+
20+
@Override
21+
public boolean equals(Object o) {
22+
if (this == o) return true;
23+
if (o == null || getClass() != o.getClass()) return false;
24+
25+
BooleanValue that = (BooleanValue) o;
26+
27+
return value == that.value;
28+
29+
}
30+
31+
@Override
32+
public int hashCode() {
33+
return (value ? 1 : 0);
34+
}
35+
36+
@Override
37+
public String toString() {
38+
return "BooleanValue{" +
39+
"value=" + value +
40+
'}';
41+
}
542
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package graphql.language;
2+
3+
4+
import java.math.BigDecimal;
5+
6+
public class FloatValue implements Value{
7+
8+
private BigDecimal value;
9+
10+
public FloatValue(BigDecimal value) {
11+
this.value = value;
12+
}
13+
14+
public BigDecimal getValue() {
15+
return value;
16+
}
17+
18+
public void setValue(BigDecimal value) {
19+
this.value = value;
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "FloatValue{" +
25+
"value=" + value +
26+
'}';
27+
}
28+
29+
@Override
30+
public boolean equals(Object o) {
31+
if (this == o) return true;
32+
if (o == null || getClass() != o.getClass()) return false;
33+
34+
FloatValue that = (FloatValue) o;
35+
36+
return !(value != null ? !value.equals(that.value) : that.value != null);
37+
38+
}
39+
40+
@Override
41+
public int hashCode() {
42+
return value != null ? value.hashCode() : 0;
43+
}
44+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package graphql.language;
2+
3+
4+
public class StringValue implements Value{
5+
6+
private String value;
7+
8+
public StringValue(String value) {
9+
this.value = value;
10+
}
11+
12+
public String getValue() {
13+
return value;
14+
}
15+
16+
public void setValue(String value) {
17+
this.value = value;
18+
}
19+
20+
@Override
21+
public String toString() {
22+
return "StringValue{" +
23+
"value='" + value + '\'' +
24+
'}';
25+
}
26+
27+
@Override
28+
public boolean equals(Object o) {
29+
if (this == o) return true;
30+
if (o == null || getClass() != o.getClass()) return false;
31+
32+
StringValue that = (StringValue) o;
33+
34+
return !(value != null ? !value.equals(that.value) : that.value != null);
35+
36+
}
37+
38+
@Override
39+
public int hashCode() {
40+
return value != null ? value.hashCode() : 0;
41+
}
42+
}

‎src/main/java/graphql/parser/GraphqlAntlrToLanguage.java‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import graphql.parser.antlr.GraphqlParser;
88
import org.antlr.v4.runtime.misc.NotNull;
99

10+
import java.math.BigDecimal;
1011
import java.util.LinkedHashMap;
1112
import java.util.Map;
1213

@@ -152,18 +153,31 @@ public Void visitTypeName(@NotNull GraphqlParser.TypeNameContext ctx) {
152153

153154
@Override
154155
public Void visitArgument(@NotNull GraphqlParser.ArgumentContext ctx) {
155-
Argument argument = new Argument(ctx.NAME().getText(),getValue(ctx.value()));
156+
Argument argument = new Argument(ctx.NAME().getText(), getValue(ctx.value()));
156157
Field field = (Field) context.get(ContextProperty.Field);
157158
field.getArguments().add(argument);
158159
return super.visitArgument(ctx);
159160
}
160161

161-
private Value getValue(GraphqlParser.ValueContext ctx){
162+
private Value getValue(GraphqlParser.ValueContext ctx) {
162163
if (ctx.IntValue() != null) {
163164
IntValue intValue = new IntValue(Integer.parseInt(ctx.IntValue().getText()));
164165
return intValue;
166+
} else if (ctx.FloatValue() != null) {
167+
FloatValue floatValue = new FloatValue(new BigDecimal(ctx.FloatValue().getText()));
168+
return floatValue;
169+
} else if (ctx.BooleanValue() != null) {
170+
BooleanValue booleanValue = new BooleanValue(Boolean.parseBoolean(ctx.BooleanValue().getText()));
171+
return booleanValue;
172+
} else if (ctx.StringValue() != null) {
173+
StringValue booleanValue = new StringValue(trimQuotes(ctx.StringValue().getText()));
174+
return booleanValue;
165175
}
166176
return null;
167177
}
168178

179+
private String trimQuotes(String string) {
180+
return string.substring(1, string.length() - 1);
181+
}
182+
169183
}

‎src/test/groovy/graphql/parser/ParserTest.groovy‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package graphql.parser
22

33
import graphql.language.Argument
4+
import graphql.language.BooleanValue
45
import graphql.language.Document
56
import graphql.language.Field
7+
import graphql.language.FloatValue
68
import graphql.language.GraphQLType
79
import graphql.language.IntValue
810
import graphql.language.NamedType
911
import graphql.language.OperationDefinition
1012
import graphql.language.Selection
1113
import graphql.language.SelectionSet
14+
import graphql.language.StringValue
1215
import graphql.language.VariableDefinition
1316
import spock.lang.Specification
1417

@@ -78,7 +81,7 @@ class ParserTest extends Specification {
7881

7982
}
8083

81-
def "parse mutation"(){
84+
def "parse mutation"() {
8285
given:
8386
def input = 'mutation setName { setName(name: "Homer") { newName } }'
8487

@@ -89,12 +92,15 @@ class ParserTest extends Specification {
8992
getOperationDefinition(document).operation == OperationDefinition.Operation.MUTATION
9093
}
9194

92-
def "parse field argument"(){
95+
def "parse field arguments"() {
9396
given:
94-
def input = '{ user(id: 10) }'
97+
def input = '{ user(id: 10 name: "homer" admin:true floatValue: 3.04) }'
9598

96-
def argument = new Argument("id",new IntValue(10))
97-
def field = new Field("user",[argument])
99+
def argument = new Argument("id", new IntValue(10))
100+
def argument2 = new Argument("name", new StringValue("homer"))
101+
def argument3 = new Argument("admin", new BooleanValue(true))
102+
def argument4 = new Argument("floatValue", new FloatValue(3.04))
103+
def field = new Field("user", [argument,argument2,argument3,argument4])
98104
def selectionSet = new SelectionSet([field])
99105
def operationDefinition = new OperationDefinition()
100106
operationDefinition.operation = OperationDefinition.Operation.QUERY
@@ -109,7 +115,6 @@ class ParserTest extends Specification {
109115
}
110116

111117

112-
113118
Field getInnerField(SelectionSet selectionSet) {
114119
def field = (Field) selectionSet.selections[0]
115120
(Field) field.selectionSet.selections[0]

0 commit comments

Comments
 (0)