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

AASM and graphql #818

Open
revh opened this issue Mar 4, 2023 · 0 comments
Open

AASM and graphql #818

revh opened this issue Mar 4, 2023 · 0 comments

Comments

@revh
Copy link

revh commented Mar 4, 2023

I added the gem and successfully created the state machine for my model, I tested it in rails console and it works well, but my graphql existing code is now crashing with this error:

Rails couldn't find a valid model for Match association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass.

if I comment out include AASM and the related aasm configuration everything works fine again

match.rb

class Match < ApplicationRecord
  include AASM

  enum status: [ :pending, :queue, :ready, :active ]

  belongs_to :user
  belongs_to :matched_user, :class_name => "User"

  aasm column: :status do
    state :pending, initial: true
    state :queue, :ready, :active
    
    event :next do
      transitions from: :pending, to: :queue
      transitions from: :queue, to: :ready
      transitions from: :ready, to: :active
    end

    event :back do
      transitions from: :ready, to: :queue
    end
  end
end

user.rb

class User < ApplicationRecord
  has_many :matches
  has_many :matched_users, through: :matches, dependent: :destroy
end

user_type.rb (graphql)

module Types
  class UserType < Types::BaseObject
    field :matches, [Types::MatchType]
    field :matched_users, [Types::UserType]
  end
end

match_type.rb (graphql)

module Types
  class Types::MatchStatus < Types::BaseEnum
    value :pending
    value :queue
    value :ready
    value :active
  end

  class MatchType < Types::BaseObject
    field :id, ID, null: false
    field :user_id, Integer, null: false
    field :matched_user_id, Integer, null: false
    field :status, Types::MatchStatus, null: false
    field :created_at, GraphQL::Types::ISO8601DateTime, null: false
    field :updated_at, GraphQL::Types::ISO8601DateTime, null: false
  end
end

Query

{
  user(id: 1) {
    id
    matches {
      id
    }
  }
}
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