0

I am completely new to using Rails, I'm trying to create a basic crud application using ruby-graphql to do a simple find query with active record to a sqlite3 database. I set up a user type

  class UserType < Types::BaseObject
    description "A user type "
    field :id, ID, null: false
    field :username, String, null: false
    field :email, String, null: false
    field :email_confirmed, Boolean, null:false
    field :first_name, String , null:false
    field :last_name, String, null:false
    field :biography, String, null:true
    field :avatar, String, null:true
    field :created_at, GraphQL::Types::ISO8601DateTime, null:false
    field :updated_at, GraphQL::Types::ISO8601DateTime, null:false
  end

Then I setup the query :

field :user, UserType, null:false,
      description: "Get a single user" do
      argument :username, String, required: true
      end
    def user(username:)
      User.find(username)
    end

My query is :

{
  user(username:"test"){
    username
  }
}

I get the following error :

{
  "error": {
    "message": "no implicit conversion of #<Class:0x000000000b975440> into Hash",
    "backtrace": [
      "C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/graphql-1.9.13/lib/graphql/schema/field.rb:519:in `block in resolve'",

If anyone could help me I would really appreciate it. How do I do this conversion in Ruby / Rails?

  • You have another problem here in that `.find` is used to find records by id. If you want to find records by another unique column use `.find_by(username: username)`. Thats also a somewhat strange use of keyword arguments. – max Oct 14 '19 at 09:53
  • Thank you max, I don't actually know proper usage I'm just trying to hack away and learn while doing what would be a better way? – Julian Mourell Oct 14 '19 at 10:01
  • Well if you only have one argument then it makes more sense that its a positonal argument. And I would name the method something that tells you about what it actually does like `find_user_by_username`. – max Oct 14 '19 at 10:03
  • Is this the same when you use graphql-ruby https://graphql-ruby.org/fields/arguments , I'm just trying to follow the docs here. It still displays the same error even with find_by – Julian Mourell Oct 14 '19 at 10:10
  • I've tried to use find and the id as well and I still get this same problem. If anyone has any clues or they think they know whats going on I would really appreciate any advice or suggestions. – Julian Mourell Oct 14 '19 at 13:13
  • ```field :users, [UserType], null:false, description: "Get all the users" def users User.all end``` This code when I query for all users raises no issues. – Julian Mourell Oct 14 '19 at 13:14
  • In schema.rb it tries to call ``` if extended_args.any? field_receiver.public_send(@resolver_method, **extended_args) else field_receiver.public_send(@resolver_method) end ``` It tries to call the public send passing in the extended_args of query:1 and throws an error. If any ruby / graphql-ruby experienced developers could help, I'd be very grateful – Julian Mourell Oct 14 '19 at 15:08

1 Answers1

1

Turns out there was an issue with Ruby gem I was using upgraded to the new gem and it solved the problem. See - https://github.com/rmosolgo/graphql-ruby/issues/2537