Skip to content

Commit e9067f7

Browse files
Adding tests to proove scalars are ommited if not used
1 parent 8a5fe91 commit e9067f7

File tree

2 files changed

+154
-2
lines changed

2 files changed

+154
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public boolean isUseAstDefinitions() {
146146
}
147147

148148
public static Options defaultOptions() {
149-
return new Options(false, false, false,
149+
return new Options(false, true, true,
150150
false, false, false,
151151
directive -> true, DefaultGraphqlTypeComparatorRegistry.defaultComparators());
152152
}

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

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import spock.lang.Specification
2424

2525
import java.util.function.UnaryOperator
2626

27+
import static graphql.Scalars.GraphQLInt
2728
import static graphql.Scalars.GraphQLString
2829
import static graphql.TestUtil.mockScalar
2930
import static graphql.TestUtil.mockTypeRuntimeWiring
@@ -165,7 +166,6 @@ class SchemaPrinterTest extends Specification {
165166

166167
expect:
167168
result != null
168-
!result.contains("scalar")
169169
!result.contains("__TypeKind")
170170
}
171171

@@ -766,6 +766,9 @@ enum Episode {
766766
JEDI
767767
NEWHOPE
768768
}
769+
770+
"Asteroid"
771+
scalar Asteroid
769772
"""
770773
}
771774

@@ -1390,5 +1393,154 @@ type Query {
13901393
'''
13911394
}
13921395

1396+
def "omit unused built-in scalars by default - created by sdl string"(){
1397+
given:
1398+
def sdl = '''type Query {scalarcustom : RandomScalar} scalar RandomScalar'''
1399+
1400+
def registry = new SchemaParser().parse(sdl)
1401+
def runtimeWiring = newRuntimeWiring().scalar(mockScalar("RandomScalar")).build()
1402+
def options = SchemaGenerator.Options.defaultOptions().enforceSchemaDirectives(false)
1403+
1404+
def schema = new SchemaGenerator().makeExecutableSchema(options, registry, runtimeWiring)
1405+
1406+
def result = new SchemaPrinter(noDirectivesOption).print(schema)
1407+
1408+
expect:
1409+
1410+
ScalarInfo.STANDARD_SCALARS.forEach({
1411+
scalarType -> assert !result.contains(scalarType.name)
1412+
})
1413+
1414+
result ==
1415+
'''type Query {
1416+
scalarcustom: RandomScalar
1417+
}
1418+
1419+
"RandomScalar"
1420+
scalar RandomScalar
1421+
'''
1422+
}
1423+
1424+
def "omit unused custom scalars when unused - created by sdl string"(){
1425+
given:
1426+
def sdl = '''type Query {astring : String aInt : Int} "Some Scalar" scalar CustomScalar'''
1427+
1428+
def registry = new SchemaParser().parse(sdl)
1429+
def runtimeWiring = newRuntimeWiring().scalar(mockScalar("CustomScalar")).build()
1430+
def options = SchemaGenerator.Options.defaultOptions().enforceSchemaDirectives(false)
1431+
1432+
def schema = new SchemaGenerator().makeExecutableSchema(options, registry, runtimeWiring)
13931433

1434+
def result = new SchemaPrinter(noDirectivesOption).print(schema)
1435+
1436+
expect:
1437+
assert !result.contains("ID") && !result.contains("Float") && !result.contains("Boolean")
1438+
result ==
1439+
'''type Query {
1440+
aInt: Int
1441+
astring: String
13941442
}
1443+
'''
1444+
}
1445+
1446+
def "show graphql-java extended scalar types by default when used - created by sdl string"(){
1447+
given:
1448+
def sdl = '''type Query {aInt : BigInteger}'''
1449+
1450+
def registry = new SchemaParser().parse(sdl)
1451+
def runtimeWiring = newRuntimeWiring().build()
1452+
def options = SchemaGenerator.Options.defaultOptions().enforceSchemaDirectives(false)
1453+
1454+
def schema = new SchemaGenerator().makeExecutableSchema(options, registry, runtimeWiring)
1455+
1456+
def result = new SchemaPrinter(defaultOptions().includeDirectives(false)).print(schema)
1457+
1458+
expect:
1459+
result ==
1460+
'''type Query {
1461+
aInt: BigInteger
1462+
}
1463+
1464+
"Built-in java.math.BigInteger"
1465+
scalar BigInteger
1466+
'''
1467+
}
1468+
1469+
def "omit unused built-in by default - created programmatically"(){
1470+
given:
1471+
GraphQLScalarType myScalar = new GraphQLScalarType("RandomScalar", "about scalar", new Coercing() {
1472+
@Override
1473+
Object serialize(Object input) {
1474+
return null
1475+
}
1476+
1477+
@Override
1478+
Object parseValue(Object input) {
1479+
return null
1480+
}
1481+
1482+
@Override
1483+
Object parseLiteral(Object input) {
1484+
return null
1485+
}
1486+
})
1487+
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
1488+
.name("scalarType").type(myScalar).build()
1489+
def queryType = GraphQLObjectType.newObject().name("Query").field(fieldDefinition).build()
1490+
1491+
def schema = GraphQLSchema.newSchema().query(queryType).additionalType(myScalar).build()
1492+
1493+
def result = new SchemaPrinter(defaultOptions().includeDirectives(false)).print(schema)
1494+
1495+
expect:
1496+
ScalarInfo.STANDARD_SCALARS.forEach({
1497+
scalarType -> assert !result.contains(scalarType.name)
1498+
})
1499+
result ==
1500+
'''type Query {
1501+
scalarType: RandomScalar
1502+
}
1503+
1504+
"about scalar"
1505+
scalar RandomScalar
1506+
'''
1507+
}
1508+
1509+
def "omit unused custom scalars when unused - created programmatically"(){
1510+
given:
1511+
GraphQLScalarType myScalar = new GraphQLScalarType("Scalar", "about scalar", new Coercing() {
1512+
@Override
1513+
Object serialize(Object input) {
1514+
return null
1515+
}
1516+
1517+
@Override
1518+
Object parseValue(Object input) {
1519+
return null
1520+
}
1521+
1522+
@Override
1523+
Object parseLiteral(Object input) {
1524+
return null
1525+
}
1526+
})
1527+
GraphQLFieldDefinition fieldDefinition = newFieldDefinition()
1528+
.name("someType").type(GraphQLInt).build()
1529+
def queryType = GraphQLObjectType.newObject().name("Query").field(fieldDefinition).build()
1530+
1531+
def schema = GraphQLSchema.newSchema().query(queryType).additionalType(myScalar).build()
1532+
1533+
def result = new SchemaPrinter(defaultOptions().includeScalarTypes(true).includeDirectives(false)).print(schema)
1534+
1535+
expect:
1536+
result ==
1537+
'''type Query {
1538+
someType: Int
1539+
}
1540+
1541+
"about scalar"
1542+
scalar Scalar
1543+
'''
1544+
}
1545+
1546+
}

0 commit comments

Comments
 (0)