How to correctly enable native subscription support for Kotlin Flow in graphql-kotlin-spring-server? #970
-
| Based on the content of https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-schema-generator/src/test/kotlin/com/expediagroup/graphql/execution/FlowSubscriptionExecutionStrategyTest.kt I was expecting to enable support with the two simple steps below but an attempt to transform  @Configuration
class Configuration {
    @Bean
    fun maxQueryDepthInstrumentation(): MaxQueryDepthInstrumentation = MaxQueryDepthInstrumentation(13)
    @Bean
    fun maxQueryComplexityInstrumentation(): MaxQueryComplexityInstrumentation =
        MaxQueryComplexityInstrumentation(maxQueryComplexity) { ... }
    // 1st step
    @Bean
    fun schemaGeneratorHooks(): SchemaGeneratorHooks = object : FlowSubscriptionSchemaGeneratorHooks() {
        override fun willGenerateGraphQLType(type: KType): GraphQLType? = when (type.classifier) {
            LocalDate::class -> ExtendedScalars.Date
            YetOneCustom::class -> yetOneCustomGraphQLScalarType
            else -> null
        }
    }
    // 2nd step
    @Bean
    fun graphQL(@Autowired graphQL: GraphQL): GraphQL = graphQL.transform { it.subscriptionExecutionStrategy(FlowSubscriptionExecutionStrategy()) }
}Then I tried to change 2nd step for     // 2nd step
    @Primary
    @Bean
    fun graphQL(graphQLSchema: GraphQLSchema): GraphQL = GraphQL.newGraphQL(graphQLSchema)
            .subscriptionExecutionStrategy(FlowSubscriptionExecutionStrategy())
            .build()Also I tried to set      @Component
    class BeansInitializer : ApplicationContextInitializer<GenericApplicationContext> {
    
        override fun initialize(applicationContext: GenericApplicationContext) {
            val graphQL = (applicationContext.getBean("graphQL") as GraphQL).transform {
                it.subscriptionExecutionStrategy(FlowSubscriptionExecutionStrategy())
            }
            applicationContext.registerBean("graphQL", graphql.GraphQL::class.java, graphQL)
        }
    }
    @Component
    class Runner(private val applicationContext: GenericApplicationContext) : ApplicationRunner {
    
        override fun run(args: ApplicationArguments?) {
            val graphQL = (applicationContext.getBean("graphQL") as GraphQL).transform {
                it.subscriptionExecutionStrategy(FlowSubscriptionExecutionStrategy())
            }
            applicationContext.registerBean("graphQL", graphql.GraphQL::class.java, graphQL)
        }
    }
 | 
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
| Hello 👋 Currently you will have to recreate the  I'll open up a PR to update the default to  | 
Beta Was this translation helpful? Give feedback.
Hello 👋
Thanks for raising this. It appears that while
graphql-kotlin-schema-generatorexposesFlowSubscriptionExecutionStrategywe forgot to updategraphql-kotlin-spring-serverto easily use it.Currently you will have to recreate the
GraphQLbean (https://github.com/ExpediaGroup/graphql-kotlin/blob/master/graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/GraphQLSchemaConfiguration.kt#L61-L94) and configure it with the flow strategy.I'll open up a PR to update the default to
FlowSubscriptionExecutionStrategy.