Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscription trigger not working #2195

Closed
Loschcode opened this issue Mar 18, 2019 · 1 comment
Closed

Subscription trigger not working #2195

Loschcode opened this issue Mar 18, 2019 · 1 comment

Comments

@Loschcode
Copy link

Loschcode commented Mar 18, 2019

I've got a subscription such as

module Subscriptions
  class SubscribeToBullshit < Mutations::BaseMutation
    description 'yes'

    field :id, String, null: true
    field :first_name, String, null: true
    field :last_name, String, null: true

    def resolve
      return unless current_identity

      {
        id: current_identity.id,
        first_name: current_identity.first_name,
        last_name: current_identity.last_name
      }
    end
  end
end

Which's plugged on my schema this way

module Types
  class SubscriptionType < GraphQL::Schema::Object
    field :subscribeToBullshit, resolver: Subscriptions::SubscribeToBullshit
  end
end

And finally the schema declaration as so

class MySchema < GraphQL::Schema
  mutation Types::MutationType
  query Types::QueryType
  subscription Types::SubscriptionType
end

On my front-end I subscribe to subscribeToBullshit successfully through a command which is like that

subscription SubscribeToBullshit {
  subscribeToBullshit {
    id
    firstName
    lastName
  }
}

The subscription from my front-end is successful. I now want to trigger the update from my server, inside a mutation, to be sent to the different clients listening to subscribeToBullshit

module Mutations
  class StoreIdentityName < Mutations::BaseMutation
    def resolve
     # some logic around
     MySchema.subscriptions.trigger('subscribeToBullshit', {}, {
        id: current_identity.id,
        first_name: current_identity.first_name,
        last_name: current_identity.last_name
      })
    end
  end
end

I don't think the detail of the mutation matters. when I check MySchema.subscriptions it returns nil and will systematically cause an error. Is the documentation up-to-date on this matter?

undefined method `trigger' for nil:NilClass

What am I doing wrong here?

@Loschcode
Copy link
Author

I figured you have to add use GraphQL::Subscriptions::ActionCableSubscriptions at the beginning of your schema, #2091 gave the way.

You should add this detail in https://graphql-ruby.org/subscriptions/triggers.html as it's quite important to know, I could never guess ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant