diff --git a/lib/graphql/subscriptions/serialize.rb b/lib/graphql/subscriptions/serialize.rb index e9cd802b76..f58f282b72 100644 --- a/lib/graphql/subscriptions/serialize.rb +++ b/lib/graphql/subscriptions/serialize.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true require "set" +require "ostruct" + module GraphQL class Subscriptions # Serialization helpers for passing subscription data around. diff --git a/spec/dummy/app/assets/javascripts/application.js b/spec/dummy/app/assets/javascripts/application.js index fa3cb17b7c..470abfb253 100644 --- a/spec/dummy/app/assets/javascripts/application.js +++ b/spec/dummy/app/assets/javascripts/application.js @@ -28,7 +28,8 @@ var receivedCallback = options.received // Unique-ish var uuid = Math.round(Date.now() + Math.random() * 100000).toString(16) - return { + var subscription = { + _subscribed: false, subscription: App.cable.subscriptions.create({ channel: "GraphqlChannel", id: uuid, @@ -41,7 +42,8 @@ console.log("Connected", query, variables) }, received: function(payload) { - console.log("received", query, variables, payload) + subscription._subscribed = true + App.logToBody("ActionCable received: " + JSON.stringify(payload)) if (payload.result) { receivedCallback(payload) } @@ -53,12 +55,27 @@ } ), trigger: function(options) { - this.subscription.perform("make_trigger", options) + if (!subscription._subscribed) { + options.retries ||= 0 + options.retries++ + if (options.retries > 5) { + throw new Error("Retried 5 times, failed to trigger: " + JSON.stringify(options)) + } else { + App.logToBody("Retrying trigger " + options.retries + " : " + JSON.stringify(options)) + setTimeout(function() { + subscription.trigger(options) + }, 500) + } + } else { + App.logToBody("Triggering " + JSON.stringify(options)) + this.subscription.perform("make_trigger", options) + } }, unsubscribe: function() { this.subscription.unsubscribe() }, } + return subscription } // Add `text` to the HTML body, for debugging @@ -67,5 +84,6 @@ var logEntry = document.createElement("p") logEntry.innerText = text bodyLog.appendChild(logEntry) + bodyLog.append("\n") } }).call(this); diff --git a/spec/dummy/app/views/pages/show.html b/spec/dummy/app/views/pages/show.html index b1357b8576..67c80fb909 100644 --- a/spec/dummy/app/views/pages/show.html +++ b/spec/dummy/app/views/pages/show.html @@ -75,28 +75,36 @@

ActionCable Test Page

fingerprintSubscriptions[key] = [] } var subs = fingerprintSubscriptions[key] + var number = subs.length + 1 var newSub = App.subscribe({ + number: number, query: "subscription fingerprint" + key + " { counterIncremented { newValue } }", variables: {}, received: function(data) { App.logToBody("received from " + key + " " + JSON.stringify(data)) if (data.result.data.counterIncremented) { - appendItem("fingerprint-updates-" + key, "update-" + newSub.number + "-value-" + data.result.data.counterIncremented.newValue) + appendItem("fingerprint-updates-" + key, "update-" + number + "-value-" + data.result.data.counterIncremented.newValue) } else { - appendItem("fingerprint-updates-" + key, "connected-" + newSub.number) + appendItem("fingerprint-updates-" + key, "connected-" + number) } } }) - newSub.number = subs.length + 1 + App.logToBody("Appending Subscription, key: " + key + ", number:" + number ) subs.push(newSub) } function triggerWithFingerprint(key) { - App.logToBody("triggering " + key) var subs = fingerprintSubscriptions[key] - var sub = subs && subs[0] - sub && sub.trigger({field: "counterIncremented", arguments: {}, value: null}) + if (!subs) { + App.logToBody("No Subscriptions found for Key: " + key + ", in: " + Object.keys(fingerprintSubscriptions)) + } + var sub = subs[0] + if (!sub) { + App.logToBody("Empty subscriptions array for key: " + key + ", in: " + Object.keys(fingerprintSubscriptions)) + } + App.logToBody("triggering " + key) + sub.trigger({field: "counterIncremented", arguments: {}, value: null}) } function unsubscribeWithFingerprint(key) { diff --git a/spec/dummy/test/system/action_cable_subscription_test.rb b/spec/dummy/test/system/action_cable_subscription_test.rb index da1117fb2e..a7ce09ba3c 100644 --- a/spec/dummy/test/system/action_cable_subscription_test.rb +++ b/spec/dummy/test/system/action_cable_subscription_test.rb @@ -2,6 +2,9 @@ require "application_system_test_case" class ActionCableSubscriptionsTest < ApplicationSystemTestCase + setup do + ActionCable.server.config.logger = Logger.new(STDOUT) + end # This test covers a lot of ground! test "it handles subscriptions" do # Load the page and let the subscriptions happen