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

standardize coroutine context propagation in the execution #1349

Merged
merged 1 commit into from Feb 2, 2022

Conversation

dariuszkuc
Copy link
Collaborator

@dariuszkuc dariuszkuc commented Jan 25, 2022

📝 Description

Attempts to standardize how we can propagate the coroutine/reactor context created by the server when processing incoming requests to the data fetchers that would resolve the underlying fields.

GraphQLContext map was introduced in graphql-java 17 as means to standardize usage of GraphQL context (previously it could be any object). We can store the original GraphQL request context in the map and then use it from within the data fetchers.

This is a breaking change as we are changing signatures of GraphQLContextFactory and FunctionDataFetcher.

🔗 Related Issues

@dariuszkuc dariuszkuc added changes: major Changes require a major version status: do not merge Do not merge until this is removed module: generator Issue affects the schema generator and federation code module: server Issue affects the server code labels Jan 25, 2022
@@ -16,23 +16,16 @@

package com.expediagroup.graphql.server.execution

import com.expediagroup.graphql.generator.execution.GraphQLContext
const val GRAPHQL_COROUTINE_CONTEXT_KEY = "GRAPHQL_COROUTINE_CONTEXT"
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this supposed to be GRAPHQL_COROUTINE_SCOPE? Maybe we should just use the value defined in the generator package so we don't have it defined in two locations

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually I'm thinking of dropping the String key in favor of just using classes as keys.

@dariuszkuc dariuszkuc marked this pull request as draft January 25, 2022 23:23
val contextMap = contextFactory.generateContextMap(request) ?: emptyMap<Any, Any?>()

val customContext: CoroutineContext = contextMap[GRAPHQL_COROUTINE_CONTEXT_KEY] as? CoroutineContext ?: EmptyCoroutineContext
val graphQLExecutionScope = CoroutineScope(coroutineContext + customContext + SupervisorJob())
Copy link
Contributor

Choose a reason for hiding this comment

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

Not an expert on coroutines, but does this need to be cancelled at the end of request processing? Was just reading through this: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When we return the value from this method then all children coroutines will already complete so there is nothing to cancel.

@dariuszkuc dariuszkuc force-pushed the coroutine_context branch 2 times, most recently from 7515e76 to f99d268 Compare January 27, 2022 04:18
@dariuszkuc dariuszkuc marked this pull request as ready for review January 27, 2022 04:26
@dariuszkuc dariuszkuc changed the title [DRAFT] standardize coroutine context propagation in the execution standardize coroutine context propagation in the execution Jan 27, 2022
@dariuszkuc dariuszkuc removed the status: do not merge Do not merge until this is removed label Jan 27, 2022
@TiKevin83
Copy link

Ran into #1300 testing this library in a Kotlin project and was able to confirm that this PR resolves the issues we were seeing with the spring security context being empty when coming from the GraphQL endpoints!

@wallind
Copy link

wallind commented Jan 31, 2022

Ran into #1300 testing this library in a Kotlin project and was able to confirm that this PR resolves the issues we were seeing with the spring security context being empty when coming from the GraphQL endpoints!

to be even more specific we were able to get things working with manually built jars specifically from this commit 87fc89c

📝 Description

Attempts to standardize how we can propagate the coroutine/reactor context created by the server when processing incoming requests to the data fetchers that would resolve the underlying fields.

`GraphQLContext` map was introduced in `graphql-java` 17 as means to standardize usage of GraphQL context (previously it could be any object). We can store the original GraphQL request context in the map and then use it from within the data fetchers.

This is a breaking change as we are changing signatures of `GraphQLContextFactory` and `FunctionDataFetcher`.

🔗 Related Issues

* ExpediaGroup#1336
* ExpediaGroup#1318
* ExpediaGroup#1300
* ExpediaGroup#1257
@dariuszkuc
Copy link
Collaborator Author

Updated to keep the deprecated custom GraphQL context around (will drop it in v7) to provide easier migration path.

@samuelAndalon samuelAndalon self-requested a review February 2, 2022 17:03
@dariuszkuc dariuszkuc merged commit eb3a870 into ExpediaGroup:master Feb 2, 2022
@dariuszkuc dariuszkuc deleted the coroutine_context branch February 2, 2022 18:16
dariuszkuc added a commit to dariuszkuc/graphql-kotlin that referenced this pull request Feb 8, 2022
…oup#1349)

📝 Description

Attempts to standardize how we can propagate the coroutine/reactor context created by the server when processing incoming requests to the data fetchers that would resolve the underlying fields.

`GraphQLContext` map was introduced in `graphql-java` 17 as means to standardize usage of GraphQL context (previously it could be any object). We can store the original GraphQL request context in the map and then use it from within the data fetchers.

This is a breaking change as we are changing signatures of `GraphQLContextFactory` and `FunctionDataFetcher`.

🔗 Related Issues

* ExpediaGroup#1336
* ExpediaGroup#1318
* ExpediaGroup#1300
* ExpediaGroup#1257
This was referenced Feb 13, 2022
@frozenice
Copy link

I'm interested in trying this out. I don't want to build and publish it somewhere for myself and our CI.

@dariuszkuc Do you publish snapshots of the graphql-kotlin packages somewhere? Or even better: any idea when this will be released?

@dariuszkuc
Copy link
Collaborator Author

@frozenice we just released 6.0.0-alpha.0

dariuszkuc added a commit to dariuszkuc/graphql-kotlin that referenced this pull request Aug 5, 2022
…oup#1349)

📝 Description

Attempts to standardize how we can propagate the coroutine/reactor context created by the server when processing incoming requests to the data fetchers that would resolve the underlying fields.

`GraphQLContext` map was introduced in `graphql-java` 17 as means to standardize usage of GraphQL context (previously it could be any object). We can store the original GraphQL request context in the map and then use it from within the data fetchers.

This is a breaking change as we are changing signatures of `GraphQLContextFactory` and `FunctionDataFetcher`.

🔗 Related Issues

* ExpediaGroup#1336
* ExpediaGroup#1318
* ExpediaGroup#1300
* ExpediaGroup#1257
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: generator Issue affects the schema generator and federation code module: server Issue affects the server code
Development

Successfully merging this pull request may close these issues.

None yet

8 participants