Skip to content

Commit 39e1489

Browse files
authored
Merge branch 'master' into renovate/spring-core
2 parents 0f0bf40 + e8f3708 commit 39e1489

File tree

4 files changed

+62
-4
lines changed

4 files changed

+62
-4
lines changed

src/main/kotlin/graphql/kickstart/tools/RootTypeInfo.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.kickstart.tools
22

3+
import graphql.language.Description
34
import graphql.language.SchemaDefinition
45
import graphql.language.TypeName
56

@@ -9,7 +10,8 @@ import graphql.language.TypeName
910
internal class RootTypeInfo private constructor(
1011
private val queryType: TypeName?,
1112
private val mutationType: TypeName?,
12-
private val subscriptionType: TypeName?
13+
private val subscriptionType: TypeName?,
14+
private val description: Description?
1315
) {
1416
companion object {
1517
const val DEFAULT_QUERY_NAME = "Query"
@@ -20,14 +22,16 @@ internal class RootTypeInfo private constructor(
2022
val queryType = definitions.lastOrNull()?.operationTypeDefinitions?.find { it.name == "query" }?.typeName
2123
val mutationType = definitions.lastOrNull()?.operationTypeDefinitions?.find { it.name == "mutation" }?.typeName
2224
val subscriptionType = definitions.lastOrNull()?.operationTypeDefinitions?.find { it.name == "subscription" }?.typeName
25+
val description = definitions.lastOrNull()?.description
2326

24-
return RootTypeInfo(queryType, mutationType, subscriptionType)
27+
return RootTypeInfo(queryType, mutationType, subscriptionType, description)
2528
}
2629
}
2730

2831
fun getQueryName() = queryType?.name ?: DEFAULT_QUERY_NAME
2932
fun getMutationName() = mutationType?.name ?: DEFAULT_MUTATION_NAME
3033
fun getSubscriptionName() = subscriptionType?.name ?: DEFAULT_SUBSCRIPTION_NAME
34+
fun getDescription() = description?.content
3135

3236
fun isMutationRequired() = mutationType != null
3337
fun isSubscriptionRequired() = subscriptionType != null

src/main/kotlin/graphql/kickstart/tools/SchemaObjects.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ data class SchemaObjects(
1313
val mutation: GraphQLObjectType?,
1414
val subscription: GraphQLObjectType?,
1515
val dictionary: Set<GraphQLType>,
16-
val codeRegistryBuilder: GraphQLCodeRegistry.Builder
16+
val codeRegistryBuilder: GraphQLCodeRegistry.Builder,
17+
val description: String?
1718
) {
1819
/**
1920
* Makes a GraphQLSchema with query, mutation and subscription.
2021
*/
2122
fun toSchema(): GraphQLSchema {
2223
return GraphQLSchema.newSchema()
24+
.description(description)
2325
.query(query)
2426
.mutation(mutation)
2527
.subscription(subscription)
@@ -32,6 +34,7 @@ data class SchemaObjects(
3234
* Makes a GraphQLSchema with query but without mutation and subscription.
3335
*/
3436
fun toReadOnlySchema(): GraphQLSchema = GraphQLSchema.newSchema()
37+
.description(description)
3538
.query(query)
3639
.additionalTypes(dictionary)
3740
.build()

src/main/kotlin/graphql/kickstart/tools/SchemaParser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class SchemaParser internal constructor(
103103
val additionalObjects = objects.filter { o -> o != query && o != subscription && o != mutation }
104104

105105
val types = (additionalObjects.toSet() as Set<GraphQLType>) + inputObjects + enums + interfaces + unions
106-
return SchemaObjects(query, mutation, subscription, types, codeRegistryBuilder)
106+
return SchemaObjects(query, mutation, subscription, types, codeRegistryBuilder, rootInfo.getDescription())
107107
}
108108

109109
/**

src/test/kotlin/graphql/kickstart/tools/SchemaParserTest.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,57 @@ class SchemaParserTest {
574574
assertEquals(schema.queryType.getFieldDefinition("empty").description, "")
575575
}
576576

577+
@Test
578+
fun `parser should include schema descriptions when declared`() {
579+
val schema = SchemaParser.newParser()
580+
.schemaString(
581+
"""
582+
"This is a schema level description"
583+
schema {
584+
query: SubstituteQuery
585+
}
586+
587+
type SubstituteQuery {
588+
description: String
589+
comment: String
590+
omitted: String
591+
both: String
592+
empty: String
593+
}
594+
""")
595+
.resolvers(object : GraphQLQueryResolver {})
596+
.options(SchemaParserOptions.newOptions().allowUnimplementedResolvers(true).build())
597+
.build()
598+
.makeExecutableSchema()
599+
600+
assertEquals(schema.description, "This is a schema level description")
601+
}
602+
603+
@Test
604+
fun `parser should return null schema description when not declared`() {
605+
val schema = SchemaParser.newParser()
606+
.schemaString(
607+
"""
608+
schema {
609+
query: SubstituteQuery
610+
}
611+
612+
type SubstituteQuery {
613+
description: String
614+
comment: String
615+
omitted: String
616+
both: String
617+
empty: String
618+
}
619+
""")
620+
.resolvers(object : GraphQLQueryResolver {})
621+
.options(SchemaParserOptions.newOptions().allowUnimplementedResolvers(true).build())
622+
.build()
623+
.makeExecutableSchema()
624+
625+
assertNull(schema.description)
626+
}
627+
577628
enum class EnumType {
578629
TEST
579630
}

0 commit comments

Comments
 (0)