Skip to content

Commit af9f0c4

Browse files
Adding tests to proove scalars are ommited if not used
1 parent 0a40ca4 commit af9f0c4

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed

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

Lines changed: 150 additions & 0 deletions
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
@@ -1411,5 +1412,154 @@ type Query {
14111412
'''
14121413
}
14131414

1415+
def "omit unused built-in scalars by default - created by sdl string"(){
1416+
given:
1417+
def sdl = '''type Query {scalarcustom : RandomScalar} scalar RandomScalar'''
1418+
1419+
def registry = new SchemaParser().parse(sdl)
1420+
def runtimeWiring = newRuntimeWiring().scalar(mockScalar("RandomScalar")).build()
1421+
def options = SchemaGenerator.Options.defaultOptions().enforceSchemaDirectives(false)
1422+
1423+
def schema = new SchemaGenerator().makeExecutableSchema(options, registry, runtimeWiring)
1424+
1425+
def result = new SchemaPrinter(noDirectivesOption).print(schema)
1426+
1427+
expect:
1428+
1429+
ScalarInfo.STANDARD_SCALARS.forEach({
1430+
scalarType -> assert !result.contains(scalarType.name)
1431+
})
14141432

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

0 commit comments

Comments
 (0)