Code Monkey home page Code Monkey logo

Comments (5)

sobrinho avatar sobrinho commented on June 7, 2024

Here's a reproducible spec:

# frozen_string_literal: true
require "spec_helper"

module SubscriptionEnum
  class InMemoryBackend < GraphQL::Subscriptions
    attr_reader :write_subscription_events, :execute_all_events

    def initialize(...)
      super
      @write_subscription_events = []
      @execute_all_events = []
    end

    def write_subscription(_query, events)
      @write_subscription_events.concat(events)
    end

    def execute_all(event, _object)
      @execute_all_events.push(event)
    end
  end

  class MyEnumType < GraphQL::Schema::Enum
    value "ONE", value: "one"
    value "TWO", value: "two"
  end

  class MySubscription < GraphQL::Schema::Subscription
    argument :my_enum, MyEnumType, required: true
    field :my_enum, MyEnumType
  end

  class SubscriptionType < GraphQL::Schema::Object
    field :my_subscription, resolver: MySubscription
  end

  class Schema < GraphQL::Schema
    subscription SubscriptionType
    use InMemoryBackend
  end
end

describe GraphQL::Subscriptions do
  describe "enums" do
    let(:schema) { SubscriptionEnum::Schema }
    let(:implementation) { schema.subscriptions }
    let(:write_subscription_events) { implementation.write_subscription_events }
    let(:execute_all_events) { implementation.execute_all_events }

    describe "pushing updates" do
      focus
      it "sends updated data" do
        query_str = <<-GRAPHQL
          subscription ($myEnum: MyEnum!) {
            mySubscription (myEnum: $myEnum) {
              myEnum
            }
          }
        GRAPHQL

        schema.execute(query_str, variables: { "myEnum" => "ONE" })

        schema.subscriptions.trigger(:mySubscription, { "myEnum" => "ONE" }, nil)

        assert_equal(":mySubscription:myEnum:one", write_subscription_events[0].topic)
        assert_equal(":mySubscription:myEnum:one", execute_all_events[0].topic)
      end
    end
  end
end

from graphql-ruby.

sobrinho avatar sobrinho commented on June 7, 2024

It fails with:

$ bundle exec rake test

# Running tests with run options --seed 49142:

F

Finished tests in 0.062282s, 16.0560 tests/s, 32.1120 assertions/s.


Failure:
GraphQL::Subscriptions::enums::pushing updates#test_0001_sends updated data [/Users/sobrinho/Developer/opensource/graphql-ruby/spec/graphql/subscriptions_enum_spec.rb:66]
Minitest::Assertion: --- expected
+++ actual
@@ -1 +1 @@
-":mySubscription:myEnum:one"
+":mySubscription:myEnum:ONE"


1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
rake aborted!
Command failed with status (1)
/Users/sobrinho/.gem/ruby/2.7.6/gems/rake-13.1.0/exe/rake:27:in `<top (required)>'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/cli/exec.rb:58:in `load'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/cli/exec.rb:58:in `kernel_load'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/cli/exec.rb:23:in `run'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/cli.rb:492:in `exec'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/vendor/thor/lib/thor/invocation.rb:127:in `invoke_command'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/vendor/thor/lib/thor.rb:392:in `dispatch'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/cli.rb:34:in `dispatch'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/vendor/thor/lib/thor/base.rb:485:in `start'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/cli.rb:28:in `start'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/exe/bundle:45:in `block in <top (required)>'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/lib/bundler/friendly_errors.rb:117:in `with_friendly_errors'
/Users/sobrinho/.gem/ruby/2.7.6/gems/bundler-2.4.13/exe/bundle:33:in `<top (required)>'
/Users/sobrinho/.gem/ruby/2.7.6/bin/bundle:25:in `load'
/Users/sobrinho/.gem/ruby/2.7.6/bin/bundle:25:in `<main>'
Tasks: TOP => test
(See full trace by running task with --trace)

from graphql-ruby.

sobrinho avatar sobrinho commented on June 7, 2024

That's how we fixed it for our current version:

 # Normalize enum values to its custom value when triggering subscriptions.
 #
 # In a nutshell, Event#topic normalizes "ENUM_VALUE" to "enum_value" when
 # writing a subscription while when triggering it doesn't.
 #
 # This monkey-patch will allow us to call either way:
 #
 #   RootSchema.subscriptions.trigger(:my_subscription, my_enum: "enum_value")
 #   RootSchema.subscriptions.trigger(:my_subscription, my_enum: "ENUM_VALUE")
 #
 # See https://github.com/rmosolgo/graphql-ruby/issues/4713
 module NormalizeTriggerSubscriptionEnumValues
   def stringify_args(arg_owner, args, context)
     result = super

     if arg_owner.is_a?(Class) && arg_owner < GraphQL::Schema::Enum
       arg_owner.coerce_isolated_input(result) || result
     else
       result
     end
   end
 end

 GraphQL::Subscriptions::Event.singleton_class.prepend(NormalizeTriggerSubscriptionEnumValues)

from graphql-ruby.

rmosolgo avatar rmosolgo commented on June 7, 2024

Hey, sorry for the trouble and thanks for the great report and work-around! I bet a patch very similar to what you shared will fix this in the gem. I'll take a look soon, or feel free to submit a PR if you're interested 👍

from graphql-ruby.

sobrinho avatar sobrinho commented on June 7, 2024

Thanks @rmosolgo ! ❤️

from graphql-ruby.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.