Skip to content

Commit b129244

Browse files
authored
Add additionalTypes to generateSchema (#622)
* Make additionalType public * fix linting issue * Update SchemaGenerator.kt * Switch additionalTypes back to internal * fix federated schema
1 parent dd90c71 commit b129244

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

graphql-kotlin-federation/src/main/kotlin/com/expediagroup/graphql/federation/FederatedSchemaGenerator.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import com.expediagroup.graphql.TopLevelObject
2020
import com.expediagroup.graphql.federation.directives.ExtendsDirective
2121
import com.expediagroup.graphql.generator.SchemaGenerator
2222
import graphql.schema.GraphQLSchema
23+
import kotlin.reflect.KType
2324

2425
/**
2526
* Generates federated GraphQL schemas based on the specified configuration.
@@ -30,8 +31,13 @@ open class FederatedSchemaGenerator(generatorConfig: FederatedSchemaGeneratorCon
3031
* Scans specified packages for all the federated (extended) types and adds them to the schema additional types,
3132
* then it generates the schema as usual using the [FederatedSchemaGeneratorConfig].
3233
*/
33-
override fun generateSchema(queries: List<TopLevelObject>, mutations: List<TopLevelObject>, subscriptions: List<TopLevelObject>): GraphQLSchema {
34+
override fun generateSchema(
35+
queries: List<TopLevelObject>,
36+
mutations: List<TopLevelObject>,
37+
subscriptions: List<TopLevelObject>,
38+
additionalTypes: Set<KType>
39+
): GraphQLSchema {
3440
addAdditionalTypesWithAnnotation(ExtendsDirective::class)
35-
return super.generateSchema(queries, mutations, subscriptions)
41+
return super.generateSchema(queries, mutations, subscriptions, additionalTypes)
3642
}
3743
}

graphql-kotlin-schema-generator/src/main/kotlin/com/expediagroup/graphql/generator/SchemaGenerator.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ import kotlin.reflect.full.createType
4141
*/
4242
open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
4343

44+
internal val additionalTypes = mutableSetOf<KType>()
4445
internal val classScanner = ClassScanner(config.supportedPackages)
4546
internal val cache = TypesCache(config.supportedPackages)
4647
internal val codeRegistry = GraphQLCodeRegistry.newCodeRegistry()
47-
internal val additionalTypes = mutableSetOf<KType>()
4848
internal val directives = ConcurrentHashMap<String, GraphQLDirective>()
4949

5050
/**
@@ -53,17 +53,18 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
5353
open fun generateSchema(
5454
queries: List<TopLevelObject>,
5555
mutations: List<TopLevelObject> = emptyList(),
56-
subscriptions: List<TopLevelObject> = emptyList()
56+
subscriptions: List<TopLevelObject> = emptyList(),
57+
additionalTypes: Set<KType> = emptySet()
5758
): GraphQLSchema {
5859

60+
this.additionalTypes.addAll(additionalTypes)
5961
val builder = GraphQLSchema.newSchema()
6062
builder.query(generateQueries(this, queries))
6163
builder.mutation(generateMutations(this, mutations))
6264
builder.subscription(generateSubscriptions(this, subscriptions))
6365
builder.additionalTypes(generateAdditionalTypes())
6466
builder.additionalDirectives(directives.values.toSet())
6567
builder.codeRegistry(codeRegistry.build())
66-
6768
val schema = config.hooks.willBuildSchema(builder).build()
6869

6970
classScanner.close()
@@ -85,11 +86,11 @@ open class SchemaGenerator(internal val config: SchemaGeneratorConfig) {
8586
/**
8687
* Generate the GraphQL type for all the `additionalTypes`. They are generated as non-inputs and not as IDs.
8788
* If you need to provide more custom additional types that were not picked up from reflection of the schema objects,
88-
* you can modify the set of `additionalTypes` before you call this method.
89+
* you can provide more types to be added through [generateSchema].
8990
*
90-
* This function is recursive because while generating the additionalTypes it is possible to create additional types that need to be processed.
91+
* This function loops because while generating the additionalTypes it is possible to create more additional types that need to be processed.
9192
*/
92-
protected open fun generateAdditionalTypes(): Set<GraphQLType> {
93+
protected fun generateAdditionalTypes(): Set<GraphQLType> {
9394
val graphqlTypes = mutableSetOf<GraphQLType>()
9495
while (this.additionalTypes.isNotEmpty()) {
9596
val currentlyProcessedTypes = LinkedHashSet(this.additionalTypes)

0 commit comments

Comments
 (0)