fs / rails-base-graphql-api

Base Rails application for GraphQL API
27 stars 7 forks source link

Add ability to trigger event in resolvers #197

Closed sergeyantonov1 closed 2 years ago

sergeyantonov1 commented 2 years ago

Summary

Added ability to trigger events in GraphQL resolvers.

Problem:

Case when you can use it:

Example of usage:

module Resolvers
  class PollItem < Resolvers::Base
    argument :id, Integer, required: true

    type Types::PollType, null: false

    private

    def fetch_data
      context[:poll] = current_user.received_polls.find(options[:id])
    end

    def trigger_event
      TriggerEvent.new(event: "poll_opened", options: { poll_id: options[:id] })
    end
  end
end
# config/initializers/events.rb
ActiveSupport::Notifications.subscribe "poll_opened" do |_name, _start, _finish, _id, payload|
  # do something
end

How it works

Test plan

None

Review notes

While reviewing pull-request (especially when it's your pull-request), please make sure that:

Deploy notes

None

References

None

sergeyantonov1 commented 2 years ago

@DmitryBarskov by GraphQL rules you can use query for reading data, mutation for modify data. You can read it here: