Skip to content

Commit e255b62

Browse files
committed
Merge branch 'master' into remove-undeclared-directives
2 parents 973205f + 44d9c56 commit e255b62

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

src/main/java/graphql/schema/idl/SchemaGenerator.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,9 @@ public SchemaGenerator() {
213213
*
214214
* @param typeRegistry this can be obtained via {@link SchemaParser#parse(String)}
215215
* @param wiring this can be built using {@link RuntimeWiring#newRuntimeWiring()}
216+
*
216217
* @return an executable schema
218+
*
217219
* @throws SchemaProblem if there are problems in assembling a schema such as missing type resolvers or no operations defined
218220
*/
219221
public GraphQLSchema makeExecutableSchema(TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring) throws SchemaProblem {
@@ -227,7 +229,9 @@ public GraphQLSchema makeExecutableSchema(TypeDefinitionRegistry typeRegistry, R
227229
* @param options the controlling options
228230
* @param typeRegistry this can be obtained via {@link SchemaParser#parse(String)}
229231
* @param wiring this can be built using {@link RuntimeWiring#newRuntimeWiring()}
232+
*
230233
* @return an executable schema
234+
*
231235
* @throws SchemaProblem if there are problems in assembling a schema such as missing type resolvers or no operations defined
232236
*/
233237
public GraphQLSchema makeExecutableSchema(Options options, TypeDefinitionRegistry typeRegistry, RuntimeWiring wiring) throws SchemaProblem {
@@ -351,6 +355,7 @@ private GraphQLObjectType buildOperation(BuildContext buildCtx, OperationTypeDef
351355
* but then we build the rest of the types specified and put them in as additional types
352356
*
353357
* @param buildCtx the context we need to work out what we are doing
358+
*
354359
* @return the additional types not referenced from the top level operations
355360
*/
356361
private Set<GraphQLType> buildAdditionalTypes(BuildContext buildCtx) {
@@ -368,6 +373,14 @@ private Set<GraphQLType> buildAdditionalTypes(BuildContext buildCtx) {
368373
}
369374
}
370375
});
376+
typeRegistry.scalars().values().forEach(scalarTypeDefinition -> {
377+
if (ScalarInfo.isStandardScalar(scalarTypeDefinition.getName())) {
378+
return;
379+
}
380+
if (buildCtx.hasInputType(scalarTypeDefinition) == null && buildCtx.hasOutputType(scalarTypeDefinition) == null) {
381+
additionalTypes.add(buildScalar(buildCtx, scalarTypeDefinition));
382+
}
383+
});
371384
return additionalTypes;
372385
}
373386

@@ -387,6 +400,7 @@ private Set<GraphQLDirective> buildAdditionalDirectives(BuildContext buildCtx) {
387400
*
388401
* @param buildCtx the context we need to work out what we are doing
389402
* @param rawType the type to be built
403+
*
390404
* @return an output type
391405
*/
392406
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"})

src/test/groovy/graphql/schema/idl/MockedWiringFactory.groovy

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

33
import graphql.TypeResolutionEnvironment
4+
import graphql.schema.Coercing
5+
import graphql.schema.CoercingParseLiteralException
6+
import graphql.schema.CoercingParseValueException
7+
import graphql.schema.CoercingSerializeException
48
import graphql.schema.DataFetcher
59
import graphql.schema.GraphQLObjectType
10+
import graphql.schema.GraphQLScalarType
611
import graphql.schema.PropertyDataFetcher
712
import graphql.schema.TypeResolver
813

@@ -47,4 +52,31 @@ class MockedWiringFactory implements WiringFactory {
4752
DataFetcher getDataFetcher(FieldWiringEnvironment environment) {
4853
return new PropertyDataFetcher(environment.getFieldDefinition().getName())
4954
}
55+
56+
@Override
57+
boolean providesScalar(ScalarWiringEnvironment environment) {
58+
if (ScalarInfo.isStandardScalar(environment.getScalarTypeDefinition().getName())) {
59+
return false
60+
}
61+
return true
62+
}
63+
64+
GraphQLScalarType getScalar(ScalarWiringEnvironment environment) {
65+
return GraphQLScalarType.newScalar().name(environment.getScalarTypeDefinition().getName()).coercing(new Coercing() {
66+
@Override
67+
Object serialize(Object dataFetcherResult) throws CoercingSerializeException {
68+
throw new UnsupportedOperationException("Not implemented");
69+
}
70+
71+
@Override
72+
Object parseValue(Object input) throws CoercingParseValueException {
73+
throw new UnsupportedOperationException("Not implemented");
74+
}
75+
76+
@Override
77+
Object parseLiteral(Object input) throws CoercingParseLiteralException {
78+
throw new UnsupportedOperationException("Not implemented");
79+
}
80+
}).build()
81+
}
5082
}

src/test/groovy/graphql/schema/idl/SchemaGeneratorTest.groovy

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1998,4 +1998,49 @@ class SchemaGeneratorTest extends Specification {
19981998
GraphQLTypeUtil.simplePrint(directive.getArgument("enumArguments").getType()) == "[SomeEnum!]"
19991999
directive.getArgument("enumArguments").getDefaultValue() == []
20002000
}
2001+
2002+
def "scalar used as output is not in additional types"() {
2003+
given:
2004+
2005+
def spec = """
2006+
scalar UsedScalar
2007+
type Query{ foo: UsedScalar }
2008+
"""
2009+
when:
2010+
def schema = schema(spec)
2011+
then:
2012+
schema.getType("UsedScalar") != null
2013+
schema.getAdditionalTypes().find { it.name == "UsedScalar" } == null
2014+
}
2015+
2016+
def "scalar used as input is not in additional types"() {
2017+
given:
2018+
2019+
def spec = """
2020+
scalar UsedScalar
2021+
input Input {
2022+
foo: UsedScalar
2023+
}
2024+
type Query{ foo(arg: Input): String }
2025+
"""
2026+
when:
2027+
def schema = schema(spec)
2028+
then:
2029+
schema.getType("UsedScalar") != null
2030+
schema.getAdditionalTypes().find { it.name == "UsedScalar" } == null
2031+
}
2032+
2033+
def "unused scalar is not ignored and provided as additional type"() {
2034+
given:
2035+
2036+
def spec = """
2037+
scalar UnusedScalar
2038+
type Query{ foo: String }
2039+
"""
2040+
when:
2041+
def schema = schema(spec)
2042+
then:
2043+
schema.getType("UnusedScalar") != null
2044+
schema.getAdditionalTypes().find { it.name == "UnusedScalar" } != null
2045+
}
20012046
}

0 commit comments

Comments
 (0)