Skip to content

Conversation

@smyrick
Copy link
Contributor

@smyrick smyrick commented Feb 19, 2021

📝 Description

There are two issues we are fixing here:

  • The GraphQLContextFactory should support suspend functions
  • Subscriptions need to support getting a null context from the factory

Supporting suspend functions is easy enough. We just updated the interface. However that also means in ApolloSubscriptionProtocolHandler we need to be able to run the suspend function in a class that deals with reactor Flux. Once we generate the context it could come back as null. If it does our old logic was to return a class DefaultGraphQLContext. However, this was now blowing up because if you had a custom context of type MyCustomContext and used that as an argument in your subscription functions, graphql-java was throwing an illegal argument exception because we were trying to pass DefaultGraphQLContext to the type MyCustomContext.

So what that means is that we really can't have a DefaultGraphQLContext instead, if the GraphQLContextFactory implementation could possibly return null, the schema functions need to have a parameter type defined as nullable

fun ticker(ctx: SubscriptionContext?): Flux<String> = Flux.just("${ctx?.value}:${Random.nextInt()}")

🔗 Related Issues

Default Context added here: #955
Resolves: #945
Resolves: #1052

@smyrick smyrick added changes: major Changes require a major version module: server Issue affects the server code labels Feb 19, 2021
counter = "BRANCH"
value = "COVEREDRATIO"
minimum = "0.74".toBigDecimal()
minimum = "0.70".toBigDecimal()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With removal of the default context factory we don't have code we are testing much anymore. We could separate some of the routers to internal functions but there is not much going on anymore in the spring module other than defining the beans and subscriptions which are covered.

graphQLContext: GraphQLContext
): Mono<GraphQLContext> = Mono.just(graphQLContext)
graphQLContext: GraphQLContext?
): GraphQLContext? = graphQLContext
Copy link
Contributor Author

@smyrick smyrick Feb 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need for a mono wrapper anymore. We should just cache the context to be used later


private fun saveContext(operationMessage: SubscriptionOperationMessage, session: WebSocketSession) {
val connectionParams = getConnectionParams(operationMessage.payload)
val context = runBlocking { contextFactory.generateContext(session) }
Copy link
Contributor Author

@smyrick smyrick Feb 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line here is still ambiguous to me. If we wrapping this in mono { } then the code is not run unless the client subscribes and then it might not run first before the operations are executed

val context = sessionState.getContext(session)

// If we do not have a context, that means the init message was never sent
return if (context != null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The context could be null. Since we have to handle this case this is what sparked all the other changes in subscriptions

@dariuszkuc
Copy link
Collaborator

Can we also update the docs to reflect those changes?

@smyrick smyrick force-pushed the suspend-context-factory branch from 3f6da31 to a945bdc Compare February 20, 2021 03:39
@smyrick smyrick force-pushed the suspend-context-factory branch from 6d77f77 to 86b032f Compare February 20, 2021 18:46
@smyrick smyrick merged commit b65d61a into ExpediaGroup:master Feb 20, 2021
@smyrick smyrick deleted the suspend-context-factory branch February 20, 2021 20:00
huehnerlady pushed a commit to huehnerlady/graphql-kotlin that referenced this pull request Apr 27, 2021
…roup#1053)

* Generate context with suspend and nullable in subscriptions

* Update tests

* Do not cache null context for subscriptions

* Rename internal methods

* WIP

* Update subsctiption javadocs

* Use expression body

* Update docs for context

Co-authored-by: Shane Myrick <accounts@shanemyrick.com>
dariuszkuc pushed a commit to dariuszkuc/graphql-kotlin that referenced this pull request Aug 5, 2022
…roup#1053)

* Generate context with suspend and nullable in subscriptions

* Update tests

* Do not cache null context for subscriptions

* Rename internal methods

* WIP

* Update subsctiption javadocs

* Use expression body

* Update docs for context

Co-authored-by: Shane Myrick <accounts@shanemyrick.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changes: major Changes require a major version module: server Issue affects the server code

Development

Successfully merging this pull request may close these issues.

Support suspend functions for context factory Not possible to have a custom context for subscription methods

2 participants