Skip to content

Commit cad4613

Browse files
committed
Adding extra data to type resolver
Damn you IDEA - you often miss changed files Merge branch '122TypeResolver' of git://github.com/kaqqao/graphql-java into kaqqao-122TypeResolver # Conflicts: # src/main/java/graphql/execution/ExecutionStrategy.java # src/test/groovy/graphql/RelaySchema.java # src/test/groovy/graphql/validation/SpecValidationSchema.java # src/test/groovy/graphql/validation/rules/Harness.java This addresses graphql-java#122 and graphql-java#205. We had clashing parameters in place. I fix this up and removed one set of them from the original PR
1 parent 4510275 commit cad4613

11 files changed

Lines changed: 105 additions & 122 deletions

README.md

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -189,25 +189,26 @@ GraphQLInterfaceType comicCharacter = newInterface()
189189

190190
##### Creating a new Union Type
191191

192-
Example: (a snippet from [here](src/test/groovy/graphql/GarfieldSchema.java))
192+
Example:
193+
Example:
193194
```java
194195
GraphQLUnionType PetType = newUnionType()
195-
.name("Pet")
196-
.possibleType(CatType)
197-
.possibleType(DogType)
198-
.typeResolver(new TypeResolver() {
199-
@Override
200-
public GraphQLObjectType getType(Object object) {
201-
if (object instanceof Cat) {
202-
return CatType;
203-
}
204-
if (object instanceof Dog) {
205-
return DogType;
196+
.name("Pet")
197+
.possibleType(CatType)
198+
.possibleType(DogType)
199+
.typeResolver(new TypeResolver() {
200+
@Override
201+
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
202+
if (env.getObject() instanceof Cat) {
203+
return CatType;
204+
}
205+
if (env.getObject() instanceof Dog) {
206+
return DogType;
207+
}
208+
return null;
206209
}
207-
return null;
208-
}
209-
})
210-
.build();
210+
})
211+
.build();
211212

212213
```
213214

src/main/java/graphql/TypeResolutionEnvironment.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package graphql;
22

3-
import java.util.Map;
4-
53
import graphql.language.Field;
64
import graphql.schema.GraphQLSchema;
75
import graphql.schema.GraphQLType;
86

7+
import java.util.Map;
8+
9+
/**
10+
* See {@link graphql.schema.TypeResolver#getType(TypeResolutionEnvironment)} for how this is used
11+
*/
912
public class TypeResolutionEnvironment {
1013

1114
private final Object object;

src/main/java/graphql/execution/ExecutionParameters.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@
1313
public class ExecutionParameters {
1414
private final TypeInfo typeInfo;
1515
private final Object source;
16+
private final Map<String, Object> arguments;
1617
private final Map<String, List<Field>> fields;
1718

18-
private ExecutionParameters(TypeInfo typeInfo, Object source, Map<String, List<Field>> fields) {
19+
private ExecutionParameters(TypeInfo typeInfo, Object source, Map<String, List<Field>> fields, Map<String, Object> arguments) {
1920
this.typeInfo = assertNotNull(typeInfo, "");
2021
this.fields = assertNotNull(fields, "");
2122
this.source = source;
23+
this.arguments = arguments;
2224
}
2325

2426
public TypeInfo typeInfo() {
@@ -33,6 +35,10 @@ public Map<String, List<Field>> fields() {
3335
return fields;
3436
}
3537

38+
public Map<String, Object> arguments() {
39+
return arguments;
40+
}
41+
3642
public static Builder newParameters() {
3743
return new Builder();
3844
}
@@ -47,6 +53,7 @@ public static class Builder {
4753
TypeInfo typeInfo;
4854
Object source;
4955
Map<String, List<Field>> fields;
56+
Map<String, Object> arguments;
5057

5158
public Builder typeInfo(TypeInfo type) {
5259
this.typeInfo = type;
@@ -68,8 +75,13 @@ public Builder source(Object source) {
6875
return this;
6976
}
7077

78+
public Builder arguments(Map<String, Object> arguments) {
79+
this.arguments = arguments;
80+
return this;
81+
}
82+
7183
public ExecutionParameters build() {
72-
return new ExecutionParameters(typeInfo, source, fields);
84+
return new ExecutionParameters(typeInfo, source, fields, arguments);
7385
}
7486
}
7587
}

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

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ protected ExecutionResult resolveField(ExecutionContext executionContext, Execut
102102
ExecutionParameters newParameters = ExecutionParameters.newParameters()
103103
.typeInfo(fieldType)
104104
.fields(parameters.fields())
105+
.arguments(argumentValues)
105106
.source(resolvedValue).build();
106107

107108
ExecutionResult result = completeValue(executionContext, newParameters, fields);
@@ -133,31 +134,32 @@ protected ExecutionResult completeValue(ExecutionContext executionContext, Execu
133134

134135

135136
GraphQLObjectType resolvedType;
136-
if (params.getFieldType() instanceof GraphQLInterfaceType) {
137+
if (fieldType instanceof GraphQLInterfaceType) {
137138
TypeResolutionParameters resolutionParams = TypeResolutionParameters.newParameters()
138-
.graphQLInterfaceType(params.getFieldType())
139-
.field(params.getFields().get(0))
140-
.value(params.getResult())
141-
.argumentValues(params.getArgumentValues())
142-
.schema(params.getExecutionContext().getGraphQLSchema()).build();
139+
.graphQLInterfaceType((GraphQLInterfaceType) fieldType)
140+
.field(fields.get(0))
141+
.value(parameters.source())
142+
.argumentValues(parameters.arguments())
143+
.schema(executionContext.getGraphQLSchema()).build();
143144
resolvedType = resolveTypeForInterface(resolutionParams);
144-
} else if (params.getFieldType() instanceof GraphQLUnionType) {
145+
146+
} else if (fieldType instanceof GraphQLUnionType) {
145147
TypeResolutionParameters resolutionParams = TypeResolutionParameters.newParameters()
146-
.graphQLUnionType(params.getFieldType())
147-
.field(params.getFields().get(0))
148-
.value(params.getResult())
149-
.argumentValues(params.getArgumentValues())
150-
.schema(params.getExecutionContext().getGraphQLSchema()).build();
148+
.graphQLUnionType((GraphQLUnionType) fieldType)
149+
.field(fields.get(0))
150+
.value(parameters.source())
151+
.argumentValues(parameters.arguments())
152+
.schema(executionContext.getGraphQLSchema()).build();
151153
resolvedType = resolveTypeForUnion(resolutionParams);
152154
} else {
153-
resolvedType = params.getFieldType();
155+
resolvedType = (GraphQLObjectType) fieldType;
154156
}
155157

156158
Map<String, List<Field>> subFields = new LinkedHashMap<>();
157159
List<String> visitedFragments = new ArrayList<>();
158-
for (Field field : params.getFields()) {
160+
for (Field field : fields) {
159161
if (field.getSelectionSet() == null) continue;
160-
fieldCollector.collectFields(params.getExecutionContext(), resolvedType, field.getSelectionSet(), visitedFragments, subFields);
162+
fieldCollector.collectFields(executionContext, resolvedType, field.getSelectionSet(), visitedFragments, subFields);
161163
}
162164

163165
ExecutionParameters newParameters = ExecutionParameters.newParameters()
@@ -178,14 +180,6 @@ private Iterable<Object> toIterable(Object result) {
178180
return (Iterable<Object>) result;
179181
}
180182

181-
/**
182-
* @deprecated Use {@link #resolveTypeForInterface(TypeResolutionParameters)}
183-
*/
184-
@Deprecated
185-
protected GraphQLObjectType resolveType(GraphQLInterfaceType graphQLInterfaceType, Object value) {
186-
return resolveTypeForInterface(TypeResolutionParameters.newParameters().graphQLInterfaceType(graphQLInterfaceType).value(value).build());
187-
}
188-
189183
protected GraphQLObjectType resolveTypeForInterface(TypeResolutionParameters params) {
190184
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLInterfaceType(), params.getSchema());
191185
GraphQLObjectType result = params.getGraphQLInterfaceType().getTypeResolver().getType(env);
@@ -195,14 +189,6 @@ protected GraphQLObjectType resolveTypeForInterface(TypeResolutionParameters par
195189
return result;
196190
}
197191

198-
/**
199-
* @deprecated Use {@link #resolveTypeForUnion(TypeResolutionParameters)}
200-
*/
201-
@Deprecated
202-
protected GraphQLObjectType resolveType(GraphQLUnionType graphQLUnionType, Object value) {
203-
return resolveTypeForUnion(TypeResolutionParameters.newParameters().graphQLUnionType(graphQLUnionType).value(value).build());
204-
}
205-
206192
protected GraphQLObjectType resolveTypeForUnion(TypeResolutionParameters params) {
207193
TypeResolutionEnvironment env = new TypeResolutionEnvironment(params.getValue(), params.getArgumentValues(), params.getField(), params.getGraphQLUnionType(), params.getSchema());
208194
GraphQLObjectType result = params.getGraphQLUnionType().getTypeResolver().getType(env);
@@ -261,15 +247,4 @@ protected GraphQLFieldDefinition getFieldDef(GraphQLSchema schema, GraphQLObject
261247
}
262248
return fieldDefinition;
263249
}
264-
265-
private ValueCompletionParameters createCompletionParams(ExecutionContext executionContext, GraphQLType fieldType,
266-
List<Field> fields, Object result, Map<String, Object> argumentValues) {
267-
return ValueCompletionParameters.newParameters()
268-
.executionContext(executionContext)
269-
.fieldType(fieldType)
270-
.fields(fields)
271-
.result(result)
272-
.argumentValues(argumentValues)
273-
.build();
274-
}
275250
}

src/main/java/graphql/schema/TypeResolver.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33

44
import graphql.TypeResolutionEnvironment;
55

6+
/**
7+
* This is called during type resolution to work out what graphql type should be used
8+
* dynamically during runtime for {@link GraphQLInterfaceType}s and {@link GraphQLUnionType}s
9+
*/
610
public interface TypeResolver {
711

12+
/**
13+
* This call back is invoked passing in a context object to allow you to know what type to use
14+
* dynamically during runtime for {@link GraphQLInterfaceType}s and {@link GraphQLUnionType}s
15+
*
16+
* @param env the runtime environment
17+
*
18+
* @return a graphql object type to use based on examining the environment
19+
*/
820
GraphQLObjectType getType(TypeResolutionEnvironment env);
921

1022
}

src/test/groovy/graphql/GarfieldSchema.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,17 +149,14 @@ public GraphQLObjectType getType(TypeResolutionEnvironment env) {
149149
.name("Pet")
150150
.possibleType(CatType)
151151
.possibleType(DogType)
152-
.typeResolver(new TypeResolver() {
153-
@Override
154-
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
155-
if (env.getObject() instanceof Cat) {
156-
return CatType;
157-
}
158-
if (env.getObject() instanceof Dog) {
159-
return DogType;
160-
}
161-
return null;
152+
.typeResolver(env -> {
153+
if (env.getObject() instanceof Cat) {
154+
return CatType;
155+
}
156+
if (env.getObject() instanceof Dog) {
157+
return DogType;
162158
}
159+
return null;
163160
})
164161
.build();
165162

src/test/groovy/graphql/RelaySchema.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ public class RelaySchema {
2121
.fetchField())
2222
.build();
2323

24-
public static GraphQLInterfaceType NodeInterface = relay.nodeInterface(new TypeResolver() {
25-
@Override
26-
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
27-
Relay.ResolvedGlobalId resolvedGlobalId = relay.fromGlobalId((String) env.getObject());
28-
//TODO: implement
29-
return null;
30-
}
24+
public static GraphQLInterfaceType NodeInterface = relay.nodeInterface(env -> {
25+
Relay.ResolvedGlobalId resolvedGlobalId = relay.fromGlobalId((String) env.getObject());
26+
//TODO: implement
27+
return null;
3128
});
3229

3330
public static GraphQLObjectType StuffEdgeType = relay.edgeType("Stuff", StuffType, NodeInterface, new ArrayList<>());

src/test/groovy/graphql/validation/SpecValidationSchema.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -135,37 +135,31 @@ public GraphQLObjectType getType(TypeResolutionEnvironment env) {
135135
public static final GraphQLUnionType catOrDog = GraphQLUnionType.newUnionType()
136136
.name("CatOrDog")
137137
.possibleTypes(cat, dog)
138-
.typeResolver(new TypeResolver() {
139-
@Override
140-
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
141-
if (env.getObject() instanceof Cat) return cat;
142-
if (env.getObject() instanceof Dog) return dog;
143-
return null;
144-
}})
138+
.typeResolver(env -> {
139+
if (env.getObject() instanceof Cat) return cat;
140+
if (env.getObject() instanceof Dog) return dog;
141+
return null;
142+
})
145143
.build();
146144

147145
public static final GraphQLUnionType dogOrHuman = GraphQLUnionType.newUnionType()
148146
.name("DogOrHuman")
149147
.possibleTypes(dog, human)
150-
.typeResolver(new TypeResolver() {
151-
@Override
152-
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
153-
if (env.getObject() instanceof Human) return human;
154-
if (env.getObject() instanceof Dog) return dog;
155-
return null;
156-
}})
148+
.typeResolver(env -> {
149+
if (env.getObject() instanceof Human) return human;
150+
if (env.getObject() instanceof Dog) return dog;
151+
return null;
152+
})
157153
.build();
158154

159155
public static final GraphQLUnionType humanOrAlien = GraphQLUnionType.newUnionType()
160156
.name("HumanOrAlien")
161157
.possibleTypes(human, alien)
162-
.typeResolver(new TypeResolver() {
163-
@Override
164-
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
165-
if (env.getObject() instanceof Human) return human;
166-
if (env.getObject() instanceof Alien) return alien;
167-
return null;
168-
}})
158+
.typeResolver(env -> {
159+
if (env.getObject() instanceof Human) return human;
160+
if (env.getObject() instanceof Alien) return alien;
161+
return null;
162+
})
169163
.build();
170164

171165
public static final GraphQLObjectType queryRoot = GraphQLObjectType.newObject()

src/test/groovy/graphql/validation/rules/Harness.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,7 @@
1515

1616
public class Harness {
1717

18-
private static TypeResolver dummyTypeResolve = new TypeResolver() {
19-
@Override
20-
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
21-
return null;
22-
}
23-
};
18+
private static TypeResolver dummyTypeResolve = env -> null;
2419

2520

2621
public static GraphQLInterfaceType Being = newInterface()
@@ -118,12 +113,7 @@ public GraphQLObjectType getType(TypeResolutionEnvironment env) {
118113
public static GraphQLUnionType CatOrDog = newUnionType()
119114
.name("CatOrDog")
120115
.possibleTypes(Dog, Cat)
121-
.typeResolver(new TypeResolver() {
122-
@Override
123-
public GraphQLObjectType getType(TypeResolutionEnvironment env) {
124-
return null;
125-
}
126-
})
116+
.typeResolver(env -> null)
127117
.build();
128118

129119
public static GraphQLInterfaceType Intelligent = newInterface()

src/test/groovy/graphql/validation/rules/OverlappingFieldsCanBeMergedTest.groovy

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ class OverlappingFieldsCanBeMergedTest extends Specification {
3131
.name("Test")
3232
.field(newFieldDefinition().name("name").type(GraphQLString))
3333
.field(newFieldDefinition().name("nickname").type(GraphQLString))
34-
.build();
34+
.build()
3535
schema = GraphQLSchema.newSchema().query(objectType).build()
3636
}
3737

3838
Document document = new Parser().parseDocument(query)
3939
ValidationContext validationContext = new ValidationContext(schema, document)
4040
OverlappingFieldsCanBeMerged overlappingFieldsCanBeMerged = new OverlappingFieldsCanBeMerged(validationContext, errorCollector)
41-
LanguageTraversal languageTraversal = new LanguageTraversal();
41+
LanguageTraversal languageTraversal = new LanguageTraversal()
4242

43-
languageTraversal.traverse(document, new RulesVisitor(validationContext, [overlappingFieldsCanBeMerged]));
43+
languageTraversal.traverse(document, new RulesVisitor(validationContext, [overlappingFieldsCanBeMerged]))
4444
}
4545

4646
def "identical fields are ok"() {
@@ -95,11 +95,11 @@ class OverlappingFieldsCanBeMergedTest extends Specification {
9595
.name("BoxUnion")
9696
.possibleTypes(StringBox, IntBox, NonNullStringBox1, NonNullStringBox2)
9797
.typeResolver(new TypeResolver() {
98-
@Override
99-
GraphQLObjectType getType(TypeResolutionEnvironment env) {
100-
return null
101-
}
102-
})
98+
@Override
99+
GraphQLObjectType getType(TypeResolutionEnvironment env) {
100+
return null
101+
}
102+
})
103103
.build()
104104
def QueryRoot = newObject()
105105
.name("QueryRoot")

0 commit comments

Comments
 (0)