Skip to content

Commit 22cf400

Browse files
authored
Made TypeResolution return <T> for easier consumption (graphql-java#774)
1 parent 7e9fcf8 commit 22cf400

3 files changed

Lines changed: 98 additions & 3 deletions

File tree

‎src/main/java/graphql/TypeResolutionEnvironment.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ public TypeResolutionEnvironment(Object object, Map<String, Object> arguments, F
2828
}
2929

3030
/**
31-
* @return the object that needs to be resolved into a specific graphql type
31+
* You will be passed the specific source object that needs to be resolve into a concrete graphql object type
32+
*
33+
* @param <T> you decide what type it is
34+
*
35+
* @return the object that needs to be resolved into a specific graphql object type
3236
*/
33-
public Object getObject() {
34-
return object;
37+
@SuppressWarnings("unchecked")
38+
public <T> T getObject() {
39+
return (T) object;
3540
}
3641

3742
/**

‎src/main/java/graphql/schema/GraphQLSchema.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.util.function.Consumer;
2020

2121
import static graphql.Assert.assertNotNull;
22+
import static graphql.Assert.assertTrue;
2223
import static graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY;
24+
import static java.lang.String.format;
2325

2426
/**
2527
* The schema represents the combined type system of the graphql engine. This is how the engine knows
@@ -75,6 +77,24 @@ public GraphQLType getType(String typeName) {
7577
return typeMap.get(typeName);
7678
}
7779

80+
/**
81+
* Called to return a named {@link graphql.schema.GraphQLObjectType} from the schema
82+
*
83+
* @param typeName the name of the type
84+
*
85+
* @return a graphql object type or null if there is one
86+
*
87+
* @throws graphql.GraphQLException if the type is NOT a object type
88+
*/
89+
public GraphQLObjectType getObjectType(String typeName) {
90+
GraphQLType graphQLType = typeMap.get(typeName);
91+
if (graphQLType != null) {
92+
assertTrue(graphQLType instanceof GraphQLObjectType,
93+
format("You have asked for named object type '%s' but its not an object type but rather a '%s'", typeName, graphQLType.getClass().getName()));
94+
}
95+
return (GraphQLObjectType) graphQLType;
96+
}
97+
7898
public List<GraphQLType> getAllTypesAsList() {
7999
return new ArrayList<>(typeMap.values());
80100
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package graphql
2+
3+
import graphql.language.Field
4+
import graphql.schema.GraphQLObjectType
5+
import graphql.schema.TypeResolver
6+
import spock.lang.Specification
7+
8+
class TypeResolutionEnvironmentTest extends Specification {
9+
10+
def idl = """
11+
type Query {
12+
foo : Foo
13+
bar : Bar
14+
fooBar : FooBar
15+
}
16+
17+
interface Foo {
18+
foo : String
19+
}
20+
21+
interface Bar {
22+
bar : String
23+
}
24+
25+
type FooBar implements Foo, Bar {
26+
foo : String
27+
bar : String
28+
}
29+
"""
30+
31+
def schema = TestUtil.schema(idl)
32+
33+
def "basic operations"() {
34+
given:
35+
36+
def environment = new TypeResolutionEnvironment("source", [:], new Field("field"), Scalars.GraphQLString, schema)
37+
38+
when:
39+
40+
TypeResolver resolverOfFooBar = new TypeResolver() {
41+
@Override
42+
GraphQLObjectType getType(TypeResolutionEnvironment env) {
43+
String source = env.getObject()
44+
assert source == "source"
45+
return schema.getObjectType("FooBar")
46+
}
47+
}
48+
49+
def objType = resolverOfFooBar.getType(environment)
50+
51+
then:
52+
objType.name == "FooBar"
53+
54+
55+
when:
56+
57+
TypeResolver resolverOfBadness = new TypeResolver() {
58+
@Override
59+
GraphQLObjectType getType(TypeResolutionEnvironment env) {
60+
return schema.getObjectType("Foo")
61+
}
62+
}
63+
64+
resolverOfBadness.getType(environment)
65+
66+
then:
67+
thrown(GraphQLException)
68+
69+
}
70+
}

0 commit comments

Comments
 (0)