Field as Interface throwing error
I have an interface "ItemInterface" with two fields "id" and "position", Cover and Source implement this interface.
In an another type I have setup as field :item, ItemInterface. Whenever I query the object, I get the following error
{"errors"=>[{"message"=>"No such type Cover, so it can't be a fragment condition", "locations"=>[{"line"=>1, "column"=>57}]}]}
I don't get the above error if I set the field to a concrete type.
here's the part for the query { items { position ... on Cover {credit} } }
Am I doing something wrong?
No, nothing wrong here. Are you using Ruby on Rails? Does this only happen in development? If so, it's an unexpected effect of Rails autoloading in development mode.
Short answer: Cover isn't loaded yet. You can force Rails to load it by manually require-ing it or by simply referencing it in your code (eg, randomly adding a line that just says Cover before defining your schema).
Long answer:
In development, Rails only loads your files when you try to access their contents. For example, it doesn't load a model into memory until you hit a controller action which fetches that model from the database. I think this makes the development server run faster.
So in this case, building your schema never accesses Cover. (I'm guessing Cover isn't a first-class field type anywhere else?) Since Cover is never directly accessed, the file is never loaded.
You could confirm this by running querying the schema:
{
__type(name: "Item") {
possibleTypes { name }
}
}
Is Cover among them? If not, then it was never loaded by Rails!
The fix for this is to force Cover to be loaded. You can do that by adding a require statement or by referencing the constant Cover, which will cause Rails to load it:
# Force these types to be loaded:
Cover
MySchema = # ...
Does any of that work for you?
@rmosolgo thanks for the details, really appreciate it. I had not mentioned, I was running those queries in the Rails' console in development.
I typed SourceType & CoverType in the console. Then I queried for types as follow:
Schema.execute("query test { __type(name: \"Item\") { possibleTypes {name} }}")
output: {"data"=>{"__type"=>{"possibleTypes"=>[{"name"=>"Cover"}, {"name"=>"Source"}]}}}
Everything seemed ok, but then after running my original query I got the same error
{"errors"=>[{"message"=>"No such type Cover, so it can't be a fragment condition", "locations"=>[{"line"=>1, "column"=>48}]}]}
:cry: seems like bug. Just curious, can you get the type by name in a direct query, for example
{ __type(name: "Cover") { name } }
Would you mind sharing a few things so i can hunt this down?
QueryTypedefinition, includingitemsfieldCoverTypedefinition- ~~original query~~ I see that's in your first message
@rmosolgo I was able to get the type after restarting the server. Something didn't seem right when I did reload!
2.1.3 :003 > result_hash = ArticleSchema.execute("query test { __type(name: \"Cover\") { name} }")
=> {"data"=>{"__type"=>{"name"=>"Cover"}}}
2.1.3 :004 > reload!
Reloading...
=> true
2.1.3 :005 > result_hash = ArticleSchema.execute("query test { __type(name: \"Cover\") { name} }")
=> {"errors"=>[{"message"=>"Something went wrong during query execution: No type found for 'Cover'"}]}
2.1.3 :006 > CoverType
=> Cover
2.1.3 :007 > result_hash = ArticleSchema.execute("query test { __type(name: \"Cover\") { name} }")
=> {"errors"=>[{"message"=>"Something went wrong during query execution: No type found for 'Cover'"}]}
TileType
TileType = GraphQL::ObjectType.define do
name "Tile"
description "Article Tile"
field :id, !types.ID
field :name, !types.String
field :tile_type, !types.Int
field :position, !types.Int, "Position"
field :items, ItemInterface do
resolve -> (object, args, ctx) {
object.articles_items.map{|obj| obj.itemable}.first # Just keeping it simple for now with only one item
}
end
end
CoverType
CoverType = GraphQL::ObjectType.define do
name("Cover")
description "Item of type image cover"
interfaces [ItemInterface]
field :id, !types.ID, "The unique ID of this item"
field :position, !types.Int, "Position of this item inside a tile"
field :credit, types.String
end
Hope this helps.
Sorry this has been open so long, hoping to nail this bug with #142
Feel free to retry on master, the new types: option to Schema.new should resolve bugs like this one!
Thanks @rmosolgo, I will try the master branch when time allows, hopefully soon.