Skip to content

Commit 586e280

Browse files
committed
refactoring
1 parent 42029a7 commit 586e280

9 files changed

Lines changed: 229 additions & 32 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private Object resolveField(ExecutionContext executionContext, GraphQLObjectType
6565
GraphQLFieldDefinition fieldDef = getFieldDef(executionContext.getGraphQLSchema(), parentType, fields.get(0));
6666
if (fieldDef == null) return null;
6767
Object resolvedValue;
68-
resolvedValue = fieldDef.getResolveValue().resolve(source, null);
68+
resolvedValue = fieldDef.getDataFetcher().get(source, null);
6969

7070

7171
return completeValue(executionContext, fieldDef.getType(), fields, resolvedValue);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package graphql.schema;
2+
3+
4+
import java.util.List;
5+
6+
public interface DataFetcher {
7+
8+
Object get(Object source, List<Object> arguments);
9+
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package graphql.schema;
22

33

4+
import graphql.GraphQLException;
5+
import graphql.language.EnumValue;
6+
47
import java.util.ArrayList;
58
import java.util.LinkedHashMap;
69
import java.util.List;
@@ -12,7 +15,26 @@ public class GraphQLEnumType implements GraphQLType, GraphQLInputType, GraphQLOu
1215
private final String description;
1316
private final Map<String, GraphQLEnumValueDefinition> valueDefinitionMap = new LinkedHashMap<>();
1417

15-
private Coercing coercing;
18+
private final Coercing coercing = new Coercing() {
19+
@Override
20+
public Object coerce(Object input) {
21+
return getNameByValue(input);
22+
}
23+
24+
@Override
25+
public Object coerceLiteral(Object input) {
26+
EnumValue enumValue = (EnumValue) input;
27+
return valueDefinitionMap.get(enumValue.getName());
28+
}
29+
};
30+
31+
private Object getNameByValue(Object value) {
32+
for (GraphQLEnumValueDefinition valueDefinition : valueDefinitionMap.values()) {
33+
if (value.equals(valueDefinition.getValue())) return valueDefinition.getName();
34+
}
35+
throw new GraphQLException("");
36+
}
37+
1638

1739
public GraphQLEnumType(String name, String description, List<GraphQLEnumValueDefinition> values) {
1840
this.name = name;
@@ -30,6 +52,9 @@ public String getName() {
3052
return name;
3153
}
3254

55+
public String getDescription() {
56+
return description;
57+
}
3358

3459
public Coercing getCoercing() {
3560
return coercing;

src/main/java/graphql/schema/GraphQLFieldDefinition.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package graphql.schema;
22

33

4-
import graphql.language.Argument;
5-
64
import java.util.ArrayList;
75
import java.util.List;
86
import java.util.Map;
@@ -12,15 +10,15 @@ public class GraphQLFieldDefinition {
1210
private final String name;
1311
private final String description;
1412
private final GraphQLOutputType type;
15-
private final ResolveValue resolveValue;
13+
private final DataFetcher dataFetcher;
1614
private final List<GraphQLFieldArgument> arguments = new ArrayList<>();
1715

1816

19-
public GraphQLFieldDefinition(String name, String description, GraphQLOutputType type, ResolveValue resolveValue, List<GraphQLFieldArgument> arguments) {
17+
public GraphQLFieldDefinition(String name, String description, GraphQLOutputType type, DataFetcher dataFetcher, List<GraphQLFieldArgument> arguments) {
2018
this.name = name;
2119
this.description = description;
2220
this.type = type;
23-
this.resolveValue = resolveValue;
21+
this.dataFetcher = dataFetcher;
2422
if (arguments != null) {
2523
this.arguments.addAll(arguments);
2624
}
@@ -36,8 +34,8 @@ public GraphQLOutputType getType() {
3634
return type;
3735
}
3836

39-
public ResolveValue getResolveValue() {
40-
return resolveValue;
37+
public DataFetcher getDataFetcher() {
38+
return dataFetcher;
4139
}
4240

4341
public List<GraphQLFieldArgument> getArguments() {
@@ -53,17 +51,17 @@ public static Builder newFieldDefinition() {
5351
}
5452

5553
public static class Builder {
56-
private ResolveValue defaultResolver = new ResolveValue() {
54+
private DataFetcher defaultResolver = new DataFetcher() {
5755
@Override
58-
public Object resolve(Object source, List<Object> arguments) {
56+
public Object get(Object source, List<Object> arguments) {
5957
return ((Map<String, Object>) source).get(Builder.this.name);
6058
}
6159
};
6260

6361
private String name;
6462
private String description;
6563
private GraphQLOutputType type;
66-
private ResolveValue resolveValue = defaultResolver;
64+
private DataFetcher dataFetcher = defaultResolver;
6765
private List<GraphQLFieldArgument> arguments = new ArrayList<>();
6866

6967

@@ -82,15 +80,15 @@ public Builder type(GraphQLOutputType type) {
8280
return this;
8381
}
8482

85-
public Builder resolveValue(ResolveValue resolveValue) {
86-
this.resolveValue = resolveValue;
83+
public Builder dataFetcher(DataFetcher dataFetcher) {
84+
this.dataFetcher = dataFetcher;
8785
return this;
8886
}
8987

9088
public Builder staticValue(final Object value) {
91-
this.resolveValue = new ResolveValue() {
89+
this.dataFetcher = new DataFetcher() {
9290
@Override
93-
public Object resolve(Object source, List<Object> arguments) {
91+
public Object get(Object source, List<Object> arguments) {
9492
return value;
9593
}
9694
};
@@ -108,7 +106,7 @@ public Builder argument(List<GraphQLFieldArgument> arguments) {
108106
}
109107

110108
public GraphQLFieldDefinition build() {
111-
return new GraphQLFieldDefinition(name, description, type, resolveValue, arguments);
109+
return new GraphQLFieldDefinition(name, description, type, dataFetcher, arguments);
112110
}
113111

114112

src/main/java/graphql/schema/ResolveValue.java

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package graphql.schema;
2+
3+
4+
import java.util.List;
5+
6+
public class StaticDataFetcher implements DataFetcher {
7+
8+
9+
private final Object value;
10+
11+
public StaticDataFetcher(Object value) {
12+
this.value = value;
13+
}
14+
15+
@Override
16+
public Object get(Object source, List<Object> arguments) {
17+
return value;
18+
}
19+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
package graphql
2+
3+
import graphql.schema.DataFetcher
4+
5+
6+
class StarWarsData {
7+
8+
9+
static def luke = [
10+
id : '1000',
11+
name : 'Luke Skywalker',
12+
friends : ['1002', '1003', '2000', '2001'],
13+
appearsIn : [4, 5, 6],
14+
homePlanet: 'Tatooine'
15+
];
16+
17+
static def vader = [
18+
id : '1001',
19+
name : 'Darth Vader',
20+
friends : ['1004'],
21+
appearsIn : [4, 5, 6],
22+
homePlanet: 'Tatooine',
23+
];
24+
25+
static def han = [
26+
id : '1002',
27+
name : 'Han Solo',
28+
friends : ['1000', '1003', '2001'],
29+
appearsIn: [4, 5, 6],
30+
];
31+
32+
static def leia = [
33+
id : '1003',
34+
name : 'Leia Organa',
35+
friends : ['1000', '1002', '2000', '2001'],
36+
appearsIn : [4, 5, 6],
37+
homePlanet: 'Alderaan',
38+
];
39+
40+
static def tarkin = [
41+
id : '1004',
42+
name : 'Wilhuff Tarkin',
43+
friends : ['1001'],
44+
appearsIn: [4],
45+
];
46+
47+
static def humanData = [
48+
1000: luke,
49+
1001: vader,
50+
1002: han,
51+
1003: leia,
52+
1004: tarkin,
53+
];
54+
55+
static def threepio = [
56+
id : '2000',
57+
name : 'C-3PO',
58+
friends : ['1000', '1002', '1003', '2001'],
59+
appearsIn : [4, 5, 6],
60+
primaryFunction: 'Protocol',
61+
];
62+
63+
static def artoo = [
64+
id : '2001',
65+
name : 'R2-D2',
66+
friends : ['1000', '1002', '1003'],
67+
appearsIn : [4, 5, 6],
68+
primaryFunction: 'Astromech',
69+
];
70+
71+
static def droidData = [
72+
2000: threepio,
73+
2001: artoo,
74+
]
75+
76+
static def getCharacter(String id) {
77+
if (humanData[id] != null) return humanData[id]
78+
if (droidData[id] != null) return droidData[id]
79+
return null
80+
}
81+
82+
static DataFetcher humanDataFetcher = new DataFetcher() {
83+
@Override
84+
Object get(Object source, List<Object> arguments) {
85+
def id = arguments[0]
86+
humanData[id]
87+
}
88+
}
89+
90+
91+
static DataFetcher droidDataFetcher = new DataFetcher() {
92+
@Override
93+
Object get(Object source, List<Object> arguments) {
94+
def id = arguments[0]
95+
droidData[id]
96+
}
97+
}
98+
99+
static DataFetcher friendsDataFetcher = new DataFetcher() {
100+
@Override
101+
Object get(Object source, List<Object> arguments) {
102+
List<Object> result = []
103+
for (String id : source.friends) {
104+
return result.add(getCharacter(id))
105+
}
106+
result
107+
}
108+
}
109+
110+
111+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package graphql
2+
3+
import spock.lang.Ignore
4+
import spock.lang.Specification
5+
6+
7+
class StarWarsQueryTest extends Specification {
8+
9+
// not yet working
10+
@Ignore
11+
def 'Correctly identifies R2-D2 as the hero of the Star Wars Saga'() {
12+
given:
13+
def query = """
14+
query HeroNameQuery {
15+
hero {
16+
name
17+
}
18+
}
19+
"""
20+
def expectedResult = [
21+
hero: [
22+
name: 'R2-D2'
23+
]
24+
]
25+
26+
when:
27+
def result = new GraphQL(StarWarsSchema.starWarsSchema, query).execute()
28+
29+
then:
30+
result == expectedResult
31+
}
32+
33+
34+
}

0 commit comments

Comments
 (0)