class GraphQL::Streaming::StreamCollector

Send patches by calling `stream.write` Each patch is serialized as JSON and delimited with “nn” @example Streaming a response with Rails

class ChunkedGraphqlsController < ApplicationController
  include ActionController::Live

  def create
    # initialize the collector with `response.stream`
    context = {
      collector: StreamCollector.new(response.stream)
    }

    Schema.execute(query_string, variables: variables, context: context)

    # close the stream when the query is done:
    response.stream.close
  end
end

Constants

DELIMITER

Public Class Methods

new(stream) click to toggle source

@param [<#write(String)>] A stream to write patches to

# File lib/graphql/streaming/stream_collector.rb, line 26
def initialize(stream)
  @stream = stream
  @first_patch = true
end

Public Instance Methods

patch(path:, value:) click to toggle source

Implement the collector API for DeferredExecution

# File lib/graphql/streaming/stream_collector.rb, line 32
def patch(path:, value:)
  patch_string = JSON.dump({path: path, value: value})

  if @first_patch
    @first_patch = false
    @stream.write(patch_string)
  else
    @stream.write(DELIMITER + patch_string)
  end
end