-
Notifications
You must be signed in to change notification settings - Fork 167
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
Change SSE response structure so that it's compatible with GraphQL SSE standard #292
Open
perzanko
wants to merge
5
commits into
absinthe-graphql:main
Choose a base branch
from
surgeventures:make-sse-response-compatible-with-standard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2dee389
Change SSE response structure so that it's compatible with GraphQL SS…
perzanko 1f549c8
update tests
perzanko d8be368
Be less restrictive matching errors
emancu 3b7a0ff
Add `standard_sse` flag for backwards compatibility
emancu 0de0846
[BREAKING CHANGE] Make `subscribe_loop` private
emancu 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 |
---|---|---|
|
@@ -140,7 +140,8 @@ defmodule Absinthe.Plug do | |
:analyze_complexity, | ||
:max_complexity, | ||
:token_limit, | ||
:transport_batch_payload_key | ||
:transport_batch_payload_key, | ||
:standard_sse | ||
] | ||
@raw_options [ | ||
:analyze_complexity, | ||
|
@@ -167,6 +168,7 @@ defmodule Absinthe.Plug do | |
- `:max_complexity` -- (Optional) Set the maximum allowed complexity of the GraphQL query. If a document’s calculated complexity exceeds the maximum, resolution will be skipped and an error will be returned in the result detailing the calculated and maximum complexities. | ||
- `:token_limit` -- (Optional) Set a limit on the number of allowed parseable tokens in the GraphQL query. Queries with exceedingly high token counts can be expensive to parse. If a query's token count exceeds the set limit, an error will be returned during Absinthe parsing (default: `:infinity`). | ||
- `:transport_batch_payload_key` -- (Optional) Set whether or not to nest Transport Batch request results in a `payload` key. Older clients expected this key to be present, but newer clients have dropped this pattern. (default: `true`) | ||
- `:standard_sse` -- (Optional) Set whether or not to adopt SSE standard. Older clients don't support this key. (default: `false`) | ||
|
||
""" | ||
@type opts :: [ | ||
|
@@ -188,7 +190,8 @@ defmodule Absinthe.Plug do | |
before_send: {module, atom}, | ||
log_level: Logger.level(), | ||
pubsub: module | nil, | ||
transport_batch_payload_key: boolean | ||
transport_batch_payload_key: boolean, | ||
standard_sse: boolean | ||
] | ||
|
||
@doc """ | ||
|
@@ -235,6 +238,7 @@ defmodule Absinthe.Plug do | |
before_send = Keyword.get(opts, :before_send) | ||
|
||
transport_batch_payload_key = Keyword.get(opts, :transport_batch_payload_key, true) | ||
standard_sse = Keyword.get(opts, :standard_sse, false) | ||
|
||
%{ | ||
adapter: adapter, | ||
|
@@ -250,7 +254,8 @@ defmodule Absinthe.Plug do | |
log_level: log_level, | ||
pubsub: pubsub, | ||
before_send: before_send, | ||
transport_batch_payload_key: transport_batch_payload_key | ||
transport_batch_payload_key: transport_batch_payload_key, | ||
standard_sse: standard_sse | ||
} | ||
end | ||
|
||
|
@@ -369,14 +374,14 @@ defmodule Absinthe.Plug do | |
|> subscribe_loop(topic, config) | ||
end | ||
|
||
def subscribe_loop(conn, topic, config) do | ||
defp subscribe_loop(conn, topic, config) do | ||
receive do | ||
%{event: "subscription:data", payload: %{result: result}} -> | ||
case chunk(conn, "#{encode_json!(result, config)}\n\n") do | ||
case chunk(conn, encode_chunk!(result, config)) do | ||
{:ok, conn} -> | ||
subscribe_loop(conn, topic, config) | ||
|
||
{:error, :closed} -> | ||
{:error, _} -> | ||
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. ℹ️ Source code chunk/2 |
||
Absinthe.Subscription.unsubscribe(config.context.pubsub, topic) | ||
conn | ||
end | ||
|
@@ -390,7 +395,7 @@ defmodule Absinthe.Plug do | |
{:ok, conn} -> | ||
subscribe_loop(conn, topic, config) | ||
|
||
{:error, :closed} -> | ||
{:error, _} -> | ||
Absinthe.Subscription.unsubscribe(config.context.pubsub, topic) | ||
conn | ||
end | ||
|
@@ -609,4 +614,26 @@ defmodule Absinthe.Plug do | |
|
||
@doc false | ||
def error_result(message), do: %{"errors" => [%{"message" => message}]} | ||
|
||
# `encode_chunk!/2` | ||
# | ||
# When option `standard_sse` is set to TRUE, it will addopt the new standard. | ||
# Otherwise, it will use legacy behaviour. This config is for keepoing | ||
# backwards compatibility, but everyone is encouraged to adopt the standard. | ||
# | ||
# | ||
# The encoded response additionally contains an event segment `event: next` and | ||
# the data is prefixed with a `data:` field, indicating data segment. | ||
# | ||
# This structure is consistent with GraphQL over S[erver-Sent Events Protocol][1] | ||
# specification and [official SSE standard][2]. | ||
# | ||
# [1]: https://github.com/enisdenjo/graphql-sse/blob/master/PROTOCOL.md#next-event | ||
# [2]: https://html.spec.whatwg.org/multipage/server-sent-events.html | ||
|
||
defp encode_chunk!(result, config) do | ||
if config.standard_sse, | ||
do: "event: next\ndata: #{encode_json!(result, config)}\n\n", | ||
else: "#{encode_json!(result, config)}\n\n" | ||
end | ||
end |
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.