Graphql-ruby: Using an interface from another file and making changes causes NameError (uninitialized constant...):

Created on 30 Aug 2017  路  6Comments  路  Source: rmosolgo/graphql-ruby

I am trying to use an interface that is in a different file from the type using it.

Queries work when the server is started as long as I make no changes to any of the GraphQL-related files. As soon as I do, the next query in GraphiQL will spin for a while and then the server logs will show a NameError (uninitialized constant Types::MyInterface). Even if the change is just a comment, I'll see this error. It doesn't matter if the change is in the schema file, the type files, or the interface file.

Once the error shows up, the server must be restarted before it will work again. If I put the interface in the same file as the type, I don't have this problem. When there's no interface at all, I can make changes till the cows come home without an error.

I think the following code samples are a minimal set that shows the problem without introducing extraneous complexity.

# my_type.rb
require './app/graphql/types/myinterface_type'
# i've also tried require_relative 'myinterface_type'

Types::MyType = GraphQL::ObjectType.define do
  name 'MyType'
  interfaces [Types::MyInterface]
  field :tf, types.String do
    resolve -> (_,_,_) { 'foo' }
  end
end
# myinterface_type.rb
Types::MyInterface = GraphQL::InterfaceType.define do
  name 'MyInterface'
  field :if, types.String do
    resolve ->(_,_,_) { 'bar' }
  end
end
# query_type.rb
Types::QueryType = GraphQL::ObjectType.define do
  name "Query"

  field :mine do
    type Types::MyType
    resolve ->(_,_,_) { Hash.new }
  end
end
# my_schema.rb
MySchema = GraphQL::Schema.define do
  query(Types::QueryType)

  # because we are using an interface to represent metadata, a resolve_type method is required by graphql-ruby
  resolve_type ->(_, _, _) do
    # however, it never seems to be invoked since we're not using interfaces to distinguish implementing types
  end
end

Most helpful comment

Sorry to hear it 馃槚

there are some other known issues with Rails autoloading (https://github.com/rmosolgo/graphql-ruby/issues/651), the only solution I can think of is to redo the schema definition from scratch to use classes, as Rails expects.

So, I'm starting some experiments here and in GitHub to find another way!

All 6 comments

Sorry to hear it 馃槚

there are some other known issues with Rails autoloading (https://github.com/rmosolgo/graphql-ruby/issues/651), the only solution I can think of is to redo the schema definition from scratch to use classes, as Rails expects.

So, I'm starting some experiments here and in GitHub to find another way!

I'm only at the start of this schema so getting it setup correctly now is easy. Is there something in the docs that shows what you mean by "redo the schema definition from scratch to use classes"? Or is this what is meant by the comment by qrush in the autoloading ticket?

Yes, that's what I mean. The API doesn't exist yet 馃槚

I want to add that as soon as I define an interface (in the same file), I'd get a LoadError.

LoadError (Unable to autoload constant Types::MutationType, expected root_dir/app/graphql/types/mutation_type.rb to define it):
AddressInterface = GraphQL::InterfaceType.define do
  name "AddressInterface"
  field :address1, types.String
  field :address2, types.String
  field :city, types.String
  field :province, types.String
  field :postalCode, types.String, property: :postal_code
end

Types::ContactType = GraphQL::ObjectType.define do
  name "Contact"

  interfaces [AddressInterface]

  field :id, !types.ID
  field :name, !types.String
  field :phone, types.String
  field :fax, types.String
  field :email, types.String
end

redo the schema definition from scratch to use classes, as Rails expects

This is "done", please reopen this if someone else finds this issue in a class-based schema!

I am following the tutorial from https://www.howtographql.com/graphql-ruby/4-authentication/
I get the following error while trying to create a mutation to create new Users;

NameError (uninitialized constant Types::AuthProviderEmailInput):
app/graphql/resolvers/create_user.rb:5:in `block in <class:CreateUser>'
app/graphql/graph_ql_ruby_schema.rb:1:in `<top (required)>'app/controllers/graphql_controller.rb:10:in `execute'
Was this page helpful?
0 / 5 - 0 ratings

Related issues

gastonmorixe picture gastonmorixe  路  3Comments

skanev picture skanev  路  3Comments

KevinColemanInc picture KevinColemanInc  路  3Comments

ecomuere picture ecomuere  路  3Comments

pareeohnos picture pareeohnos  路  3Comments