Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject schema resources from spring-graphql into DgsSchemaProvider. #1779

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,33 +21,23 @@ import com.netflix.graphql.dgs.internal.DgsSchemaProvider
import graphql.GraphQL
import graphql.execution.instrumentation.ChainedInstrumentation
import graphql.execution.instrumentation.Instrumentation
import graphql.schema.GraphQLCodeRegistry
import graphql.schema.GraphQLSchema
import graphql.schema.*
import graphql.schema.GraphQLSchema.BuilderWithoutTypes
import graphql.schema.GraphQLTypeVisitor
import graphql.schema.SchemaTransformer
import graphql.schema.SchemaTraverser
import graphql.schema.TypeResolver
import graphql.schema.idl.RuntimeWiring
import graphql.schema.idl.TypeDefinitionRegistry
import org.springframework.core.io.Resource
import org.springframework.graphql.execution.ContextDataFetcherDecorator
import org.springframework.graphql.execution.DataFetcherExceptionResolver
import org.springframework.graphql.execution.GraphQlSource
import org.springframework.graphql.execution.*
import org.springframework.graphql.execution.GraphQlSource.SchemaResourceBuilder
import org.springframework.graphql.execution.RuntimeWiringConfigurer
import org.springframework.graphql.execution.SchemaReport
import org.springframework.graphql.execution.SubscriptionExceptionResolver
import org.springframework.graphql.execution.TypeDefinitionConfigurer
import org.springframework.graphql.execution.TypeVisitorHelper
import org.springframework.lang.Nullable
import java.util.*
import java.util.function.BiFunction
import java.util.function.Consumer

class DgsGraphQLSourceBuilder(private val dgsSchemaProvider: DgsSchemaProvider, private val reloadSchemaIndicator: DefaultDgsQueryExecutor.ReloadSchemaIndicator) : SchemaResourceBuilder {
private val typeDefinitionConfigurers = mutableListOf<TypeDefinitionConfigurer>()
private val runtimeWiringConfigurers = mutableListOf<RuntimeWiringConfigurer>()

private val schemaResources: Set<Resource> = LinkedHashSet()
private val exceptionResolvers = mutableListOf<DataFetcherExceptionResolver>()
private val subscriptionExceptionResolvers = mutableListOf<SubscriptionExceptionResolver>()
private val typeVisitors = mutableListOf<GraphQLTypeVisitor>()
Expand Down Expand Up @@ -96,7 +86,7 @@ class DgsGraphQLSourceBuilder(private val dgsSchemaProvider: DgsSchemaProvider,
}

fun reload(): GraphQlSource {
var schema: GraphQLSchema = dgsSchemaProvider.schema()
var schema: GraphQLSchema = dgsSchemaProvider.schema(schemaResources = schemaResources)
val schemaTransformer = SchemaTransformer()
typeVisitorsToTransformSchema.forEach {
schemaTransformer.transform(schema, it)
Expand Down Expand Up @@ -144,7 +134,8 @@ class DgsGraphQLSourceBuilder(private val dgsSchemaProvider: DgsSchemaProvider,
}

override fun schemaResources(vararg resources: Resource?): SchemaResourceBuilder {
throw IllegalStateException("Setting schema resources in this builder is not supported - DGS SchemaProvider is already handling schema loading")
schemaResources.plus(listOf(*resources))
return this
}

override fun configureTypeDefinitions(configurer: TypeDefinitionConfigurer): SchemaResourceBuilder {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,18 @@ class DgsSchemaProvider(

fun schema(
@Language("GraphQL") schema: String? = null,
fieldVisibility: GraphqlFieldVisibility = DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY
fieldVisibility: GraphqlFieldVisibility = DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY,
schemaResources: Set<Resource> = emptySet()
): GraphQLSchema {
schemaReadWriteLock.write {
dataFetchers.clear()
dataFetcherTracingInstrumentationEnabled.clear()
dataFetcherMetricsInstrumentationEnabled.clear()
return computeSchema(schema, fieldVisibility)
return computeSchema(schema, fieldVisibility, schemaResources)
}
}

private fun computeSchema(schema: String? = null, fieldVisibility: GraphqlFieldVisibility): GraphQLSchema {
private fun computeSchema(schema: String? = null, fieldVisibility: GraphqlFieldVisibility, schemaResources: Set<Resource> = emptySet()): GraphQLSchema {
val startTime = System.currentTimeMillis()
val dgsComponents = applicationContext.getBeansWithAnnotation<DgsComponent>().values.let { beans ->
if (componentFilter != null) beans.filter(componentFilter) else beans
Expand All @@ -171,6 +172,10 @@ class DgsSchemaProvider(
// the source files aren't newline-terminated.
readerBuilder.reader("\n".reader(), "newline")
}

for (resource in schemaResources) {
readerBuilder.reader(resource.inputStream.reader(), resource.filename)
}
SchemaParser().parse(readerBuilder.build())
} else {
SchemaParser().parse(schema)
Expand Down
Loading