-
-
Notifications
You must be signed in to change notification settings - Fork 249
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
Add support for SSE-based subscriptions for caliban-quick
#2141
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c15748a
Add support for SSE-based subscriptions in the quick adapter
kyri-petrou 7848c5f
Cleanup `AcceptsGqlEncodings`
kyri-petrou 1cf019d
One more cleanup
kyri-petrou b30ccba
Always send a 200 code for SSE (Tapir)
kyri-petrou 6b0ed9d
Unify SSE response handling
kyri-petrou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ package caliban | |
|
||
import caliban.Configurator.ExecutionConfiguration | ||
import caliban.HttpUtils.DeferMultipart | ||
import caliban.ResponseValue.StreamValue | ||
import caliban.ResponseValue.{ ObjectValue, StreamValue } | ||
import caliban.Value.NullValue | ||
import caliban.interop.jsoniter.ValueJsoniter | ||
import caliban.uploads.{ FileMeta, GraphQLUploadRequest, Uploads } | ||
import caliban.wrappers.Caching | ||
|
@@ -148,13 +149,7 @@ final private class QuickRequestHandler[-R](interpreter: GraphQLInterpreter[R, A | |
cacheDirective.fold(headers)(headers.addHeader(Header.CacheControl.name, _)) | ||
|
||
private def transformResponse(httpReq: Request, resp: GraphQLResponse[Any])(implicit trace: Trace): Response = { | ||
|
||
val acceptsGqlJson: Boolean = | ||
httpReq.headers.get(Header.Accept.name).exists { h => | ||
// Better performance than having to parse the Accept header | ||
h.length >= 33 && h.toLowerCase.contains("application/graphql-response+json") | ||
} | ||
|
||
val accepts = new HttpUtils.AcceptsGqlEncodings(httpReq.headers.get(Header.Accept.name)) | ||
val cacheDirective = HttpUtils.computeCacheDirective(resp.extensions) | ||
|
||
resp match { | ||
|
@@ -164,7 +159,9 @@ final private class QuickRequestHandler[-R](interpreter: GraphQLInterpreter[R, A | |
headers = responseHeaders(ContentTypeMultipart, None), | ||
body = Body.fromStream(encodeMultipartMixedResponse(resp, stream)) | ||
) | ||
case resp if acceptsGqlJson => | ||
case resp if accepts.serverSentEvents => | ||
Response.fromServerSentEvents(encodeTextEventStream(resp)) | ||
case resp if accepts.graphQLJson => | ||
Response( | ||
status = resp.errors.collectFirst { case _: CalibanError.ParsingError | _: CalibanError.ValidationError => | ||
Status.BadRequest | ||
|
@@ -205,11 +202,24 @@ final private class QuickRequestHandler[-R](interpreter: GraphQLInterpreter[R, A | |
.mapConcatChunk(Chunk.fromArray) | ||
} | ||
|
||
private def encodeTextEventStream(resp: GraphQLResponse[Any])(implicit trace: Trace) = { | ||
val stream = (resp.data match { | ||
case ObjectValue((fieldName, StreamValue(stream)) :: Nil) => | ||
stream.either.map { | ||
case Right(r) => GraphQLResponse(ObjectValue(List(fieldName -> r)), resp.errors) | ||
case Left(err) => GraphQLResponse(ObjectValue(List(fieldName -> NullValue)), List(err)) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @paulpdaniels Is this error handling sound? Also, is there a better way to merge the error and success channels? I couldn't for the life of me figure it out 😕 |
||
case _ => | ||
ZStream.succeed(resp) | ||
}).map(resp => ServerSentEvent(writeToString(resp.toResponseValue), eventType = Some("next"))) | ||
|
||
stream ++ ZStream.succeed(CompleteSse) | ||
} | ||
|
||
private def isFtv1Request(req: Request) = | ||
req.headers | ||
.get(GraphQLRequest.`apollo-federation-include-trace`) | ||
.exists(_.equalsIgnoreCase(GraphQLRequest.ftv1)) | ||
|
||
} | ||
|
||
object QuickRequestHandler { | ||
|
@@ -225,6 +235,8 @@ object QuickRequestHandler { | |
private val ContentTypeMultipart = | ||
Headers(Header.ContentType(MediaType.multipart.mixed.copy(parameters = DeferMultipart.DeferHeaderParams)).untyped) | ||
|
||
private val CompleteSse = ServerSentEvent("", Some("complete")) | ||
|
||
private val BodyDecodeErrorResponse = | ||
badRequest("Failed to decode json body") | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this saying: for all response bodies return the original errors? Would we want errors to be null here perhaps?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm I think you're right. It's probably better to send an initial one with all the errors (if they exist) and then the subsequent ones not containing errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I made the the GraphQLResponse stream handling generic and placed it in HttpUtils in the core module so that both the caliban-quick and tapir adapters use the same one. Also the errors now are reported in the first event only