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

GraphQL input type fails to cast #120

Open
OlegSavinov opened this issue Jun 23, 2022 · 4 comments
Open

GraphQL input type fails to cast #120

OlegSavinov opened this issue Jun 23, 2022 · 4 comments
Labels
enhancement New feature or request

Comments

@OlegSavinov
Copy link

Hi! Thank you for a really cool gem first of all.
The issue raises when the model get Input type from GraphQL mutation and Rails defines it as an instance of a different from Model or JSON type (in my case Types::Inputs::MeetingSettingsInputType)

The set up is as follows

create_table "meetings", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t|
      t.json "settings", default: {}
      ...other fields

Child class

class MeetingSettings
  include StoreModel::Model

  attribute :visitor_access, :boolean
  attribute :host_launch, :boolean
  attribute :users_access, :boolean
  attribute :join_request, :boolean
end

Parent class

class Meeting < ApplicationRecord
  attribute :settings, MeetingSettings.to_type
end

The error, when model creates caused by:
# StoreModel::Types::CastError:
# failed casting #<Types::Inputs::MeetingSettingsInputType: .... > only String, Hash or MeetingSettings instances are allowed

By the source code I defined that lib/store_model/types/one.rb:35:in `cast_value' won't try to cast it and raises error.
I guess the resolving will be useful and will make the StoreModel a perfect solution for GraphQL purposes also!

@DmitryTsepelev
Copy link
Owner

Hi! Can you call .to_h on the Types::Inputs::MeetingSettingsInputType instance? I guess it will help

@OlegSavinov
Copy link
Author

Thats what I did, and it helped. Though I could not catch this param in any callbacks, so it needs to be tracked throughout the project and it not very good

@DmitryTsepelev
Copy link
Owner

We cannot add any possible class to the case statement inside one.rb 🤔 Looks like we need some way to configure these casts on the app level

@DmitryTsepelev DmitryTsepelev added the enhancement New feature or request label Jun 23, 2022
@DmitryTsepelev
Copy link
Owner

...or maybe we could check if object has .to_h and just assume that it's something that could become a Hash

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

No branches or pull requests

2 participants