graphql-ruby: Trying to use a union, getting "Unable to autoload constant Types::MutationType"
I’m getting a confusing error while trying to use a UnionType for a polymorphic model association:
LoadError - Unable to autoload constant Types::MutationType, expected .../app/graphql/types/mutation_type.rb to define it:
I have just run the rails graphql:install and have made a small number of changes to try to test out our use case. (Group has a polymorphic column that can be a number of different things) .
# app/graphql/types/query_type.rb
Types::QueryType = GraphQL::ObjectType.define do
name "Query"
# Add root-level fields here.
# They will be entry points for queries on your schema.
field :groups, types[Types::GroupType] do
resolve ->(_obj, _args, _ctx) { Group.all }
end
end
# app/graphql/types/group_type.rb
Types::GroupType = GraphQL::ObjectType.define do
name 'Group'
field :polymorphic_object_reference, ProductUnion
end
# app/graphql/product_union.rb
ProductUnion = GraphQL::UnionType.define do
name 'ProductUnion'
description 'Does something'
possible_types [Types::ThingType]
end
# app/graphql/types/thing_type.rb
Types::ThingType = GraphQL::ObjectType.define do
name 'Thing'
field :name, types.String
end
I can reproduce the above described issue with this code. However, if I change Types::GroupType to directly reference the other type, it works fine:
# app/graphql/types/group_type.rb
Types::GroupType = GraphQL::ObjectType.define do
name 'Group'
field :polymorphic_object_reference, Types::ThingType
end
No errors for that.
(Some names were sanitized for NDA)
My guess is that I’m doing something wrong and the DSL loader is choking on it, but without a useful error message I don’t have any idea what to do next. Maybe I’m putting the union in the wrong spot?No idea, and I can’t find a working example in either the docs or a web search.
I dug around in the Rails autoloading code to try to emit a more valuable error message, but I couldn’t get any useful results.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 30 (12 by maintainers)
I’m also getting this error whenever something is wrong in any of the schema files. Very hard to trace indeed.
@rmosolgo this needs to be reopened
Yeah, this error still happens all the time even in 1.8. 😐 Most of the time the issue claims to be in
MutationType, but sometimes it’s ourBaseObject. 100% of the time, the problem is elsewhere (like an incorrect field parameter), and the parser is just reporting it incorrectly. Occasionally there will be an earlier error where the parser actually failed, try checking the logs.I’m curious why you decided not to autoload, as it works fine in development. Additionally, there’s an issue https://github.com/rmosolgo/graphql-ruby/issues/1505 with not preloading all of the structure ahead of time in production, so I’d recommend not doing it how you’re doing it and looking at that thread instead.
I faced the same problem and it is kind of related to auto loading. Temporary fix is to do
require 'types/mutation_type'in the schema file.Sorry about the bad error messages. If one of the above solutions works for you, please share it. My hope is to fix the “hidden” exception by simplifying the way that schemas “boot”, #2363
I’ve been facing this issue as well - Looking at my autoload paths, here is what I found (in a vanilla graphql installation)
We are not autoloading
app/graphql/typesdirectory in here.My fix was to autoload
typesdirectory explicitly by adding the following linePlease note that any additional directories inside
app/graphql/types/will need to be called out in the autoload path.In non-dev (where we might need to eager load), I will tend to do something on these lines
That said, I’m not 100% sure if this is the right way to do it - maybe @rmosolgo can advice more?
No, I don’t see anything wrong with the original code