Skip to content

Commit e68571a

Browse files
committed
change coerce to serialize (inspired by the js impl)
1 parent 60d48ac commit e68571a

7 files changed

Lines changed: 21 additions & 31 deletions

File tree

src/main/java/graphql/Scalars.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class Scalars {
1313

1414
public static GraphQLScalarType GraphQLInt = new GraphQLScalarType("Int", "Built-in Int", new Coercing() {
1515
@Override
16-
public Object coerce(Object input) {
16+
public Object serialize(Object input) {
1717
if (input instanceof String) {
1818
return Integer.parseInt((String) input);
1919
} else if (input instanceof Integer) {
@@ -38,7 +38,7 @@ public Object parseLiteral(Object input) {
3838

3939
public static GraphQLScalarType GraphQLLong = new GraphQLScalarType("Long", "Long type", new Coercing() {
4040
@Override
41-
public Object coerce(Object input) {
41+
public Object serialize(Object input) {
4242
if (input instanceof String) {
4343
return Long.parseLong((String) input);
4444
} else if (input instanceof Long) {
@@ -64,7 +64,7 @@ public Object parseLiteral(Object input) {
6464

6565
public static GraphQLScalarType GraphQLFloat = new GraphQLScalarType("Float", "Built-in Float", new Coercing() {
6666
@Override
67-
public Object coerce(Object input) {
67+
public Object serialize(Object input) {
6868
if (input instanceof String) {
6969
return Float.parseFloat((String) input);
7070
} else if (input instanceof Float) {
@@ -87,7 +87,7 @@ public Object parseLiteral(Object input) {
8787

8888
public static GraphQLScalarType GraphQLString = new GraphQLScalarType("String", "Built-in String", new Coercing() {
8989
@Override
90-
public Object coerce(Object input) {
90+
public Object serialize(Object input) {
9191
return input == null ? null : input.toString();
9292
}
9393

@@ -106,7 +106,7 @@ public Object parseLiteral(Object input) {
106106

107107
public static GraphQLScalarType GraphQLBoolean = new GraphQLScalarType("Boolean", "Built-in Boolean", new Coercing() {
108108
@Override
109-
public Object coerce(Object input) {
109+
public Object serialize(Object input) {
110110
if (input instanceof Boolean) {
111111
return input;
112112
} else if (input instanceof Integer) {
@@ -133,7 +133,7 @@ public Object parseLiteral(Object input) {
133133

134134
public static GraphQLScalarType GraphQLID = new GraphQLScalarType("ID", "Built-in ID", new Coercing() {
135135
@Override
136-
public Object coerce(Object input) {
136+
public Object serialize(Object input) {
137137
if (input instanceof String) {
138138
return input;
139139
}

src/main/java/graphql/execution/Execution.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
package graphql.execution;
22

33

4-
import graphql.ExceptionWhileDataFetching;
54
import graphql.ExecutionResult;
6-
import graphql.ExecutionResultImpl;
75
import graphql.GraphQLException;
86
import graphql.language.Document;
97
import graphql.language.Field;
108
import graphql.language.OperationDefinition;
11-
import graphql.schema.*;
12-
import org.slf4j.Logger;
13-
import org.slf4j.LoggerFactory;
9+
import graphql.schema.GraphQLObjectType;
10+
import graphql.schema.GraphQLSchema;
1411

1512
import java.util.ArrayList;
1613
import java.util.LinkedHashMap;
1714
import java.util.List;
1815
import java.util.Map;
19-
import java.util.concurrent.Callable;
20-
import java.util.concurrent.ExecutionException;
21-
import java.util.concurrent.ExecutorService;
22-
import java.util.concurrent.Future;
23-
24-
import static graphql.introspection.Introspection.*;
2516

2617
public class Execution {
2718

src/main/java/graphql/execution/ExecutionStrategy.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@
1414
import java.util.List;
1515
import java.util.Map;
1616

17-
import static graphql.introspection.Introspection.SchemaMetaFieldDef;
18-
import static graphql.introspection.Introspection.TypeMetaFieldDef;
19-
import static graphql.introspection.Introspection.TypeNameMetaFieldDef;
17+
import static graphql.introspection.Introspection.*;
2018

2119
public abstract class ExecutionStrategy {
2220
private static final Logger log = LoggerFactory.getLogger(ExecutionStrategy.class);
@@ -112,11 +110,11 @@ protected GraphQLObjectType resolveType(GraphQLUnionType graphQLUnionType, Objec
112110

113111

114112
protected ExecutionResult completeValueForEnum(GraphQLEnumType enumType, Object result) {
115-
return new ExecutionResultImpl(enumType.getCoercing().coerce(result), null);
113+
return new ExecutionResultImpl(enumType.getCoercing().serialize(result), null);
116114
}
117115

118116
protected ExecutionResult completeValueForScalar(GraphQLScalarType scalarType, Object result) {
119-
return new ExecutionResultImpl(scalarType.getCoercing().coerce(result), null);
117+
return new ExecutionResultImpl(scalarType.getCoercing().serialize(result), null);
120118
}
121119

122120
protected ExecutionResult completeValueForList(ExecutionContext executionContext, GraphQLList fieldType, List<Field> fields, List<Object> result) {

src/main/java/graphql/execution/ValuesResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private Object coerceValueForInputObjectField(GraphQLInputObjectType inputObject
8989
}
9090

9191
private Object coerceValueForScalar(GraphQLScalarType graphQLScalarType, Object value) {
92-
return graphQLScalarType.getCoercing().coerce(value);
92+
return graphQLScalarType.getCoercing().serialize(value);
9393
}
9494

9595
private Object coerceValueForEnum(GraphQLEnumType graphQLEnumType, Object value) {

src/main/java/graphql/schema/Coercing.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ public interface Coercing {
55

66

77
/**
8+
* Called to convert a result of a DataFetcher to a valid runtime value.
89
* @param input
910
* @return
1011
*/
11-
Object coerce(Object input);
12+
Object serialize(Object input);
1213

1314
/**
1415
* Called to resolve a input from a variable.

src/main/java/graphql/schema/GraphQLEnumType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class GraphQLEnumType implements GraphQLType, GraphQLInputType, GraphQLOu
1919

2020
private final Coercing coercing = new Coercing() {
2121
@Override
22-
public Object coerce(Object input) {
22+
public Object serialize(Object input) {
2323
return getNameByValue(input);
2424
}
2525

src/test/groovy/graphql/ScalarsTest.groovy

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ScalarsTest extends Specification {
2020

2121
def "String coerce object"() {
2222
expect:
23-
Scalars.GraphQLString.getCoercing().coerce(value) == result
23+
Scalars.GraphQLString.getCoercing().serialize(value) == result
2424

2525
where:
2626
value | result
@@ -39,7 +39,7 @@ class ScalarsTest extends Specification {
3939

4040
def "ID coerce object"() {
4141
expect:
42-
Scalars.GraphQLID.getCoercing().coerce(value) == result
42+
Scalars.GraphQLID.getCoercing().serialize(value) == result
4343

4444
where:
4545
value | result
@@ -57,7 +57,7 @@ class ScalarsTest extends Specification {
5757

5858
def "Long coerce object"() {
5959
expect:
60-
Scalars.GraphQLLong.getCoercing().coerce(value) == result
60+
Scalars.GraphQLLong.getCoercing().serialize(value) == result
6161

6262
where:
6363
value | result
@@ -77,7 +77,7 @@ class ScalarsTest extends Specification {
7777

7878
def "Int coerce object"() {
7979
expect:
80-
Scalars.GraphQLInt.getCoercing().coerce(value) == result
80+
Scalars.GraphQLInt.getCoercing().serialize(value) == result
8181

8282
where:
8383
value | result
@@ -96,7 +96,7 @@ class ScalarsTest extends Specification {
9696

9797
def "Float coerce object"() {
9898
expect:
99-
Scalars.GraphQLFloat.getCoercing().coerce(value) == result
99+
Scalars.GraphQLFloat.getCoercing().serialize(value) == result
100100

101101
where:
102102
value | result
@@ -116,7 +116,7 @@ class ScalarsTest extends Specification {
116116

117117
def "Boolean coerce object"() {
118118
expect:
119-
Scalars.GraphQLBoolean.getCoercing().coerce(value) == result
119+
Scalars.GraphQLBoolean.getCoercing().serialize(value) == result
120120

121121
where:
122122
value | result

0 commit comments

Comments
 (0)