Skip to content

Commit 24edbfa

Browse files
committed
#273 - use generics where possible
This picks up from where #91 left off. The original PR seems abandoned and did tricky things with the gradle build. This is that code with more generics.
1 parent 8f1681d commit 24edbfa

File tree

7 files changed

+46
-43
lines changed

7 files changed

+46
-43
lines changed

src/main/java/graphql/ExecutionResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
public interface ExecutionResult {
77

8-
Object getData();
8+
<T> T getData();
99

1010
List<GraphQLError> getErrors();
1111
}

src/main/java/graphql/ExecutionResultImpl.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public void addErrors(List<? extends GraphQLError> errors) {
2626
}
2727

2828
@Override
29-
public Object getData() {
30-
return data;
29+
public <T> T getData() {
30+
//noinspection unchecked
31+
return (T) data;
3132
}
3233

3334
public void setData(Object result) {

src/main/java/graphql/Scalars.java

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,25 @@ public class Scalars {
2222
private static final BigInteger SHORT_MAX = BigInteger.valueOf(Short.MAX_VALUE);
2323
private static final BigInteger SHORT_MIN = BigInteger.valueOf(Short.MIN_VALUE);
2424

25-
public static GraphQLScalarType GraphQLInt = new GraphQLScalarType("Int", "Built-in Int", new Coercing() {
25+
public static GraphQLScalarType GraphQLInt = new GraphQLScalarType("Int", "Built-in Int", new Coercing<Integer>() {
2626
@Override
27-
public Object serialize(Object input) {
27+
public Integer serialize(Object input) {
2828
if (input instanceof String) {
2929
return Integer.parseInt((String) input);
3030
} else if (input instanceof Integer) {
31-
return input;
31+
return (Integer) input;
3232
} else {
3333
return null;
3434
}
3535
}
3636

3737
@Override
38-
public Object parseValue(Object input) {
38+
public Integer parseValue(Object input) {
3939
return serialize(input);
4040
}
4141

4242
@Override
43-
public Object parseLiteral(Object input) {
43+
public Integer parseLiteral(Object input) {
4444
if (!(input instanceof IntValue)) return null;
4545
BigInteger value = ((IntValue) input).getValue();
4646
if (value.compareTo(INT_MIN) == -1 || value.compareTo(INT_MAX) == 1) {
@@ -51,13 +51,13 @@ public Object parseLiteral(Object input) {
5151
});
5252

5353

54-
public static GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing() {
54+
public static GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing<Long>() {
5555
@Override
56-
public Object serialize(Object input) {
56+
public Long serialize(Object input) {
5757
if (input instanceof String) {
5858
return Long.parseLong((String) input);
5959
} else if (input instanceof Long) {
60-
return input;
60+
return (Long) input;
6161
} else if (input instanceof Integer) {
6262
return ((Integer) input).longValue();
6363
} else {
@@ -66,12 +66,12 @@ public Object serialize(Object input) {
6666
}
6767

6868
@Override
69-
public Object parseValue(Object input) {
69+
public Long parseValue(Object input) {
7070
return serialize(input);
7171
}
7272

7373
@Override
74-
public Object parseLiteral(Object input) {
74+
public Long parseLiteral(Object input) {
7575
if (input instanceof StringValue) {
7676
return Long.parseLong(((StringValue) input).getValue());
7777
} else if (input instanceof IntValue) {
@@ -86,7 +86,7 @@ public Object parseLiteral(Object input) {
8686
}
8787
});
8888

89-
public static GraphQLScalarType GraphQLFloat = new GraphQLScalarType("Float", "Built-in Float", new Coercing() {
89+
public static GraphQLScalarType GraphQLFloat = new GraphQLScalarType("Float", "Built-in Float", new Coercing<Double>() {
9090
@Override
9191
public Double serialize(Object input) {
9292
if (input instanceof String) {
@@ -103,12 +103,12 @@ public Double serialize(Object input) {
103103
}
104104

105105
@Override
106-
public Object parseValue(Object input) {
106+
public Double parseValue(Object input) {
107107
return serialize(input);
108108
}
109109

110110
@Override
111-
public Object parseLiteral(Object input) {
111+
public Double parseLiteral(Object input) {
112112
if (input instanceof IntValue) {
113113
return ((IntValue) input).getValue().doubleValue();
114114
} else if (input instanceof FloatValue) {
@@ -119,30 +119,30 @@ public Object parseLiteral(Object input) {
119119
}
120120
});
121121

122-
public static GraphQLScalarType GraphQLString = new GraphQLScalarType("String", "Built-in String", new Coercing() {
122+
public static GraphQLScalarType GraphQLString = new GraphQLScalarType("String", "Built-in String", new Coercing<String>() {
123123
@Override
124-
public Object serialize(Object input) {
124+
public String serialize(Object input) {
125125
return input == null ? null : input.toString();
126126
}
127127

128128
@Override
129-
public Object parseValue(Object input) {
129+
public String parseValue(Object input) {
130130
return serialize(input);
131131
}
132132

133133
@Override
134-
public Object parseLiteral(Object input) {
134+
public String parseLiteral(Object input) {
135135
if (!(input instanceof StringValue)) return null;
136136
return ((StringValue) input).getValue();
137137
}
138138
});
139139

140140

141-
public static GraphQLScalarType GraphQLBoolean = new GraphQLScalarType("Boolean", "Built-in Boolean", new Coercing() {
141+
public static GraphQLScalarType GraphQLBoolean = new GraphQLScalarType("Boolean", "Built-in Boolean", new Coercing<Boolean>() {
142142
@Override
143-
public Object serialize(Object input) {
143+
public Boolean serialize(Object input) {
144144
if (input instanceof Boolean) {
145-
return input;
145+
return (Boolean) input;
146146
} else if (input instanceof Integer) {
147147
return (Integer) input > 0;
148148
} else if (input instanceof String) {
@@ -153,19 +153,19 @@ public Object serialize(Object input) {
153153
}
154154

155155
@Override
156-
public Object parseValue(Object input) {
156+
public Boolean parseValue(Object input) {
157157
return serialize(input);
158158
}
159159

160160
@Override
161-
public Object parseLiteral(Object input) {
161+
public Boolean parseLiteral(Object input) {
162162
if (!(input instanceof BooleanValue)) return null;
163163
return ((BooleanValue) input).isValue();
164164
}
165165
});
166166

167167

168-
public static GraphQLScalarType GraphQLID = new GraphQLScalarType("ID", "Built-in ID", new Coercing() {
168+
public static GraphQLScalarType GraphQLID = new GraphQLScalarType("ID", "Built-in ID", new Coercing<Object>() {
169169
@Override
170170
public Object serialize(Object input) {
171171
if (input instanceof String) {
@@ -195,7 +195,7 @@ public Object parseLiteral(Object input) {
195195
}
196196
});
197197

198-
public static GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "Built-in java.math.BigInteger", new Coercing() {
198+
public static GraphQLScalarType GraphQLBigInteger = new GraphQLScalarType("BigInteger", "Built-in java.math.BigInteger", new Coercing<BigInteger>() {
199199
@Override
200200
public BigInteger serialize(Object input) {
201201
if (input instanceof BigInteger) {
@@ -227,7 +227,7 @@ public BigInteger parseLiteral(Object input) {
227227
}
228228
});
229229

230-
public static GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "Built-in java.math.BigDecimal", new Coercing() {
230+
public static GraphQLScalarType GraphQLBigDecimal = new GraphQLScalarType("BigDecimal", "Built-in java.math.BigDecimal", new Coercing<BigDecimal>() {
231231
@Override
232232
public BigDecimal serialize(Object input) {
233233
if (input instanceof BigDecimal) {
@@ -265,7 +265,7 @@ public BigDecimal parseLiteral(Object input) {
265265
}
266266
});
267267

268-
public static GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing() {
268+
public static GraphQLScalarType GraphQLByte = new GraphQLScalarType("Byte", "Built-in Byte as Int", new Coercing<Byte>() {
269269
@Override
270270
public Byte serialize(Object input) {
271271
if (input instanceof String) {
@@ -293,7 +293,7 @@ public Byte parseLiteral(Object input) {
293293
}
294294
});
295295

296-
public static GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing() {
296+
public static GraphQLScalarType GraphQLShort = new GraphQLScalarType("Short", "Built-in Short as Int", new Coercing<Short>() {
297297
@Override
298298
public Short serialize(Object input) {
299299
if (input instanceof String) {
@@ -321,7 +321,7 @@ public Short parseLiteral(Object input) {
321321
}
322322
});
323323

324-
public static GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Built-in Char as Character", new Coercing() {
324+
public static GraphQLScalarType GraphQLChar = new GraphQLScalarType("Char", "Built-in Char as Character", new Coercing<Character>() {
325325
@Override
326326
public Character serialize(Object input) {
327327
if (input instanceof String) {

src/main/java/graphql/execution/ExecutionContext.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ public Map<String, Object> getVariables() {
4747
return variables;
4848
}
4949

50-
public Object getRoot() {
51-
return root;
50+
public <T> T getRoot() {
51+
//noinspection unchecked
52+
return (T) root;
5253
}
5354

5455
public FragmentDefinition getFragment(String name) {
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package graphql.schema;
22

33

4-
public interface Coercing {
4+
public interface Coercing<T> {
55

66

77
/**
@@ -10,7 +10,7 @@ public interface Coercing {
1010
* @param input is never null
1111
* @return null if not possible/invalid
1212
*/
13-
Object serialize(Object input);
13+
T serialize(Object input);
1414

1515
/**
1616
* Called to resolve a input from a variable.
@@ -19,13 +19,13 @@ public interface Coercing {
1919
* @param input is never null
2020
* @return null if not possible/invalid
2121
*/
22-
Object parseValue(Object input);
22+
T parseValue(Object input);
2323

2424
/**
2525
* Called to convert a AST node
2626
*
2727
* @param input is never null
2828
* @return null if not possible/invalid
2929
*/
30-
Object parseLiteral(Object input);
30+
T parseLiteral(Object input);
3131
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package graphql.schema;
22

33

4-
public interface DataFetcher {
4+
public interface DataFetcher<T> {
55

6-
Object get(DataFetchingEnvironment environment);
6+
T get(DataFetchingEnvironment environment);
77
}

src/main/java/graphql/schema/DataFetchingEnvironment.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.List;
77
import java.util.Map;
88

9+
@SuppressWarnings("unchecked")
910
public class DataFetchingEnvironment {
1011
private final Object source;
1112
private final Map<String, Object> arguments;
@@ -25,8 +26,8 @@ public DataFetchingEnvironment(Object source, Map<String, Object> arguments, Obj
2526
this.graphQLSchema = graphQLSchema;
2627
}
2728

28-
public Object getSource() {
29-
return source;
29+
public <T> T getSource() {
30+
return (T) source;
3031
}
3132

3233
public Map<String, Object> getArguments() {
@@ -41,8 +42,8 @@ public <T> T getArgument(String name) {
4142
return (T) arguments.get(name);
4243
}
4344

44-
public Object getContext() {
45-
return context;
45+
public <T> T getContext() {
46+
return (T) context;
4647
}
4748

4849
public List<Field> getFields() {

0 commit comments

Comments
 (0)