Skip to content

Commit

Permalink
Simplify rails server implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rmosolgo committed Jan 27, 2022
1 parent 071e5b6 commit 5f801bf
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions stream_server/app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
class GraphqlController < ApplicationController
# Inject a sleep to make the deferrals visible on the client:
class GraphQL::Pro::Defer::Deferral
alias :old_to_http_multipart :to_http_multipart
def to_http_multipart(*args, **kwargs)
sleep 0.1
old_to_http_multipart(*args, **kwargs)
end
end

include ActionController::Live
# If accessing from outside this domain, nullify the session
# This allows for outside API access while preventing CSRF attacks,
Expand All @@ -21,33 +30,7 @@ def execute
# like https://github.com/FormidableLabs/urql/blob/main/examples/with-defer-stream-directives/server/index.js
response.headers["Content-Type"] = "multipart/mixed; boundary=\"-\""
response.headers["Connection"] = "keep-alive"
# urql expects a leading boundary marker:
response.stream.write("---")
deferred.deferrals.each_with_index do |deferral, idx|
payload = {}
payload["data"] = deferral.data
if idx > 0
payload["path"] = deferral.path
end
payload["hasNext"] = deferral.has_next?

patch = [
"",
"Content-Type: application/json; charset=utf-8",
"",
JSON.dump(payload)
]
patch << "---" if deferral.has_next?
patch_str = patch.join("\r\n")

sleep 0.1
puts Time.now.to_f
puts patch_str.inspect

response.stream.write(patch_str)
end

response.stream.write("\r\n-----")
deferred.stream_http_multipart(response)
else
# Return a plain, non-deferred result
render json: result
Expand Down

0 comments on commit 5f801bf

Please sign in to comment.