Skip to content

Commit df0e6b3

Browse files
author
Robert Mosolgo
authored
Merge pull request rmosolgo#2142 from mengledowl/feature/resolve-subscriptions-correctly-when-passing-document-to-execute
Get query string from document if it wasn't passed in explicitly to `execute`
2 parents 7fc54bf + 58cf3b8 commit df0e6b3

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

lib/graphql/query.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def initialize(name)
4040
# @return [Boolean] if false, static validation is skipped (execution behavior for invalid queries is undefined)
4141
attr_accessor :validate
4242

43-
attr_accessor :query_string
43+
attr_writer :query_string
4444

4545
# @return [GraphQL::Language::Nodes::Document]
4646
def document
@@ -135,6 +135,11 @@ def initialize(schema, query_string = nil, query: nil, document: nil, context: n
135135
end
136136
end
137137

138+
# If a document was provided to `GraphQL::Schema#execute` instead of the raw query string, we will need to get it from the document
139+
def query_string
140+
@query_string ||= (document ? document.to_query_string : nil)
141+
end
142+
138143
def_delegators :@schema, :interpreter?
139144

140145
def subscription_update?

spec/graphql/subscriptions_spec.rb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,44 @@ def to_param
268268
end
269269
end
270270

271+
describe "passing a document into #execute" do
272+
it "sends the updated data" do
273+
query_str = <<-GRAPHQL
274+
subscription ($id: ID!){
275+
payload(id: $id) { str, int }
276+
}
277+
GRAPHQL
278+
279+
document = GraphQL.parse(query_str)
280+
281+
# Initial subscriptions
282+
response = schema.execute(nil, document: document, context: { socket: "1" }, variables: { "id" => "100" }, root_value: root_object)
283+
284+
# This difference is because of how `SKIP` is handled.
285+
# Honestly the new way is probably better, since it puts a value there.
286+
empty_response = if TESTING_INTERPRETER && schema == ClassBasedInMemoryBackend::Schema
287+
{}
288+
else
289+
nil
290+
end
291+
292+
# Initial response is nil, no broadcasts yet
293+
assert_equal(empty_response, response["data"])
294+
assert_equal [], deliveries["1"]
295+
296+
# Application stuff happens.
297+
# The application signals graphql via `subscriptions.trigger`:
298+
schema.subscriptions.trigger(:payload, {"id" => "100"}, root_object.payload)
299+
# Symobls are OK too
300+
schema.subscriptions.trigger(:payload, {:id => "100"}, root_object.payload)
301+
schema.subscriptions.trigger("payload", {"id" => "300"}, nil)
302+
303+
# Let's see what GraphQL sent over the wire:
304+
assert_equal({"str" => "Update", "int" => 1}, deliveries["1"][0]["data"]["payload"])
305+
assert_equal({"str" => "Update", "int" => 2}, deliveries["1"][1]["data"]["payload"])
306+
end
307+
end
308+
271309
describe "subscribing" do
272310
it "doesn't call the subscriptions for invalid queries" do
273311
query_str = <<-GRAPHQL

0 commit comments

Comments
 (0)