Skip to content

Commit 0c69290

Browse files
committed
Merge pull request graphql-java#33 from zourzouvillys/additional-types-in-schema
Allow providing additional types to schema build.
2 parents d20d2fa + 9895ced commit 0c69290

3 files changed

Lines changed: 36 additions & 13 deletions

File tree

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,40 @@
11
package graphql.schema;
22

33

4-
import graphql.Directives;
4+
import static graphql.Assert.assertNotNull;
55

66
import java.util.ArrayList;
77
import java.util.Arrays;
8+
import java.util.Collections;
89
import java.util.List;
910
import java.util.Map;
11+
import java.util.Set;
1012

11-
import static graphql.Assert.assertNotNull;
13+
import graphql.Assert;
14+
import graphql.Directives;
1215

1316
public class GraphQLSchema {
1417

1518
private final GraphQLObjectType queryType;
1619
private final GraphQLObjectType mutationType;
1720
private final Map<String, GraphQLType> typeMap;
21+
private Set<GraphQLType> dictionary;
1822

1923
public GraphQLSchema(GraphQLObjectType queryType) {
20-
this(queryType, null);
24+
this(queryType, null, Collections.<GraphQLType>emptySet());
2125
}
2226

27+
public Set<GraphQLType> getDictionary() {
28+
return dictionary;
29+
}
2330

24-
public GraphQLSchema(GraphQLObjectType queryType, GraphQLObjectType mutationType) {
31+
public GraphQLSchema(GraphQLObjectType queryType, GraphQLObjectType mutationType, Set<GraphQLType> dictionary) {
32+
assertNotNull(dictionary, "dictionary can't be null");
2533
assertNotNull(queryType, "queryType can't be null");
2634
this.queryType = queryType;
2735
this.mutationType = mutationType;
28-
typeMap = new SchemaUtil().allTypes(this);
36+
this.dictionary = dictionary;
37+
typeMap = new SchemaUtil().allTypes(this, dictionary);
2938
}
3039

3140
public GraphQLType getType(String typeName) {
@@ -80,11 +89,17 @@ public Builder mutation(GraphQLObjectType mutationType) {
8089
}
8190

8291
public GraphQLSchema build() {
83-
GraphQLSchema graphQLSchema = new GraphQLSchema(queryType, mutationType);
84-
new SchemaUtil().replaceTypeReferences(graphQLSchema);
85-
return graphQLSchema;
86-
}
92+
return build(Collections.<GraphQLType>emptySet());
93+
}
94+
95+
public GraphQLSchema build(Set<GraphQLType> dictionary) {
96+
Assert.assertNotNull(dictionary, "dictionary can't be null");
97+
GraphQLSchema graphQLSchema = new GraphQLSchema(queryType, mutationType, dictionary);
98+
new SchemaUtil().replaceTypeReferences(graphQLSchema);
99+
return graphQLSchema;
100+
}
87101

88102

89103
}
104+
90105
}

src/main/java/graphql/schema/SchemaUtil.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.LinkedHashMap;
99
import java.util.List;
1010
import java.util.Map;
11+
import java.util.Set;
1112

1213
public class SchemaUtil {
1314

@@ -94,18 +95,23 @@ private void collectTypesForObjects(GraphQLObjectType objectType, Map<String, Gr
9495
}
9596
}
9697

97-
public Map<String, GraphQLType> allTypes(GraphQLSchema schema) {
98+
public Map<String, GraphQLType> allTypes(GraphQLSchema schema, Set<GraphQLType> dictionary) {
9899
Map<String, GraphQLType> typesByName = new LinkedHashMap<>();
99100
collectTypes(schema.getQueryType(), typesByName);
100101
if (schema.isSupportingMutations()) {
101102
collectTypes(schema.getMutationType(), typesByName);
102103
}
104+
if (dictionary != null) {
105+
for (GraphQLType type : dictionary) {
106+
collectTypes(type, typesByName);
107+
}
108+
}
103109
collectTypes(Introspection.__Schema, typesByName);
104110
return typesByName;
105111
}
106112

107113
public List<GraphQLObjectType> findImplementations(GraphQLSchema schema, GraphQLInterfaceType interfaceType) {
108-
Map<String, GraphQLType> allTypes = allTypes(schema);
114+
Map<String, GraphQLType> allTypes = allTypes(schema, schema.getDictionary());
109115
List<GraphQLObjectType> result = new ArrayList<>();
110116
for (GraphQLType type : allTypes.values()) {
111117
if (!(type instanceof GraphQLObjectType)) {
@@ -119,7 +125,7 @@ public List<GraphQLObjectType> findImplementations(GraphQLSchema schema, GraphQL
119125

120126

121127
void replaceTypeReferences(GraphQLSchema schema) {
122-
Map<String, GraphQLType> typeMap = allTypes(schema);
128+
Map<String, GraphQLType> typeMap = allTypes(schema, schema.getDictionary());
123129
for (GraphQLType type : typeMap.values()) {
124130
if (type instanceof GraphQLFieldsContainer) {
125131
resolveTypeReferencesForFieldsContainer((GraphQLFieldsContainer) type, typeMap);

src/test/groovy/graphql/schema/SchemaUtilTest.groovy

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package graphql.schema
33
import graphql.introspection.Introspection
44
import spock.lang.Specification
55

6+
import java.util.Collections;
7+
68
import static graphql.Scalars.GraphQLBoolean
79
import static graphql.Scalars.GraphQLString
810
import static graphql.StarWarsSchema.*
@@ -11,7 +13,7 @@ class SchemaUtilTest extends Specification {
1113

1214
def "collectAllTypes"() {
1315
when:
14-
Map<String, GraphQLType> types = new SchemaUtil().allTypes(starWarsSchema)
16+
Map<String, GraphQLType> types = new SchemaUtil().allTypes(starWarsSchema, Collections.emptySet())
1517
then:
1618
types == [(droidType.name) : droidType,
1719
(humanType.name) : humanType,

0 commit comments

Comments
 (0)