Graphql-ruby: Unable to autoload constant Types::BaseObject

Created on 21 Sep 2018  路  8Comments  路  Source: rmosolgo/graphql-ruby

First of all

I'm complety new on rails, I'm trying to follow the docs, but I got this error, in my graphql/types/query_type.rb file I got to run if I set all queries inside of this file, but I'm trying to split better the code, so I created separated file, I tried this to mutations to...
At this issue lets talk about mutation

Enviroment info

Well, I got this problem running on

|product|version|
|--|--|
|rails|5.2.1|
|graphql|1.8.9|
|graphiql-rails|1.4.11|
|ruby|ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]|

Lets see the code

First let me show, how I got to do, and later how I got the problem

The way that works

Well after execute the generator files I got this structure in folder app/graphql

|file|
|--|
|base_enum.rb|
|base_input_object.rb|
|base_interface.rb|
|base_object.rb|
|base_union.rb|
|mutation_type.rb|
|query_type.rb|

Building mutation

Inside of mutation type I got to reproduce this

# graphql/types/mutation_type.rb
module Types
  class MutationType < Types::BaseObject
    field :test, String, description: "Done this", null: false

    def test
      "Hello"
    end
  end
end

This above works well, but I want to create all of mutations and queries by each file to keep better structure

The problem

So well, I have created my Type of return that its called SurvivorType

# graphql/types/survivor_type
module Types
  class SurvivorType < GraphQL::Schema::Object

    description "A survivor object"

    field :id, ID, null: false
    field :gender, String, null: false
    field :age, Int, null: false
    field :last_latitude, Float, null: false
    field :last_longitude, Float, null: false
  end
end

And after this I created my Mutation in another folder

# graphql/mutations/create_survivor.rb
class Mutations::CreateSurvivor < GraphQL::Schema::Mutation
  name 'create_survivor'
  null true
  description "Create new survivor"

  argument :gender, types.String
  argument :age, types.Int
  argument :last_latitude, types.Float
  argument :last_longitude, types.Float

  field :survivor, Types::SurvivorType, null: true
  field :errors, [String], null: false

  def resolve(gender:, age:, last_latitude:, last_longitude:)
    survivor = Survivor.new(gender: gender, age: age, last_latitude: last_latitude, last_longitude: last_longitude)
    if survivor.save
      { survivor: survivor, errors: [] }
    else
      { survivor: survivor, errors: survivor.errors.full_messages }
    end
  end
end

Well when I define on graphql/types/query_type.rb I got the error

# graphql/types/mutation_type.rb
module Types
  class MutationType < Types::BaseObject
    field :create_survivor, mutation: Mutations::CreateSurvivor
    field :test, String, description: "Done this", null: false

    def test
      "Hello"
    end
  end
end

The error

  "status": 500,
  "error": "Internal Server Error",
  "exception": "#<LoadError: Unable to autoload constant Types::BaseObject, expected /home/helio/IdeaProjects/codeminer-test-in-rails/app/graphql/types/base_object.rb to define it>",
"traces": {
    "Application Trace": [
      {
        "id": 5,
        "trace": "app/graphql/types/mutation_type.rb:2:in `<module:Types>'"
      },
      {
        "id": 6,
        "trace": "app/graphql/types/mutation_type.rb:1:in `<main>'"
      },
      {
        "id": 27,
        "trace": "app/graphql/codeminer_test_in_rails_schema.rb:2:in `<class:CodeminerTestInRailsSchema>'"
      },
      {
        "id": 28,
        "trace": "app/graphql/codeminer_test_in_rails_schema.rb:1:in `<main>'"
      },
      {
        "id": 54,
        "trace": "app/controllers/graphql_controller.rb:10:in `execute'"
      }
    ]
  }

You can find the complety traces of application when happens the error here
traces.txt

Most helpful comment

How I can reproduce this, but instead of Mutation, create a single file for Query and define it, can u show me example? I'm with difficult

All 8 comments

Did you see this part of the error?

expected /home/helio/IdeaProjects/codeminer-test-in-rails/app/graphql/types/base_object.rb to define it

It's looking in that file for Types::BaseObject.

Do you have that file? What's in that file?

Exist a file called by base_object.rb

# graphql/types/base_object.rb
module Types
  class BaseObject < GraphQL::Schema::Object
  end
end

I even tried to change MutationType to extend from GraphQL::Schema::Object directly

Hmm, I'm not sure! Try looking in your Rails log. Sometimes, one error will happen, and then cause a second, different error. So maybe there's a previous error in your log.

I've found some mistakes, and fixed some of thems, so now the log error is saying:

missing keyword: required
/home/helio/.rvm/gems/ruby-2.5.1/gems/graphql-1.8.9/lib/graphql/schema/argument.rb:33:in `initialize'
/home/helio/.rvm/gems/ruby-2.5.1/gems/graphql-1.8.9/lib/graphql/schema/member/accepts_definition.rb:142:in `initialize'
/home/helio/.rvm/gems/ruby-2.5.1/gems/graphql-1.8.9/lib/graphql/schema/member/has_arguments.rb:18:in `new'
/home/helio/.rvm/gems/ruby-2.5.1/gems/graphql-1.8.9/lib/graphql/schema/member/has_arguments.rb:18:in `argument'
/home/helio/.rvm/gems/ruby-2.5.1/gems/graphql-1.8.9/lib/graphql/schema/resolver.rb:344:in `argument'
/home/helio/IdeaProjects/codeminer-test-in-rails/app/graphql/mutations/create_survivor.rb:7:in `<class:CreateSurvivor>'
/home/helio/IdeaProjects/codeminer-test-in-rails/app/graphql/mutations/create_survivor.rb:2:in `<module:Mutations>'
/home/helio/IdeaProjects/codeminer-test-in-rails/app/graphql/mutations/create_survivor.rb:1:in `<main>'

and my file now looks

module Mutations
  class CreateSurvivor < GraphQL::Schema::Mutation
    graphql_name 'create_survivor'
    null true
    description "Create new survivor"

    argument :gender, String
    argument :age, Int
    argument :last_latitude, Float
    argument :last_longitude, Float

    field :survivor, Types::SurvivorType, null: true
    field :errors, [String], null: false

    def resolve(gender:, age:, last_latitude:, last_longitude:)
      survivor = Survivor.new(gender: gender, age: age, last_latitude: last_latitude, last_longitude: last_longitude)
      if survivor.save
        { survivor: survivor, errors: [] }
      else
        { survivor: survivor, errors: survivor.errors.full_messages }
      end
    end
  end
end
    argument :gender, String, required: true
    argument :age, Int, required: true
    argument :last_latitude, Float, required: true
    argument :last_longitude, Float, required: true

I think that now its solved

馃帀 Glad you got to the bottom of it, thanks for sharing what you found!

Thx man, I glad for ur time

How I can reproduce this, but instead of Mutation, create a single file for Query and define it, can u show me example? I'm with difficult

Was this page helpful?
0 / 5 - 0 ratings