You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
com.coxautodev.graphql.tools.SchemaClassScannerError: Object type 'WeirdPet' is a member of a
known union, but no class could be found for that type name.
my schema
union Pet = NormalPet | WeirdPet
type WeirdPet {
id: Int
name: String
age: Int
classification: Classification
availableDate: Date
}
type NormalPet {
id: Int
type: Animal
name: String
age: Int
owner: Owner
availableDate: Date
}
type Query {
pets: [Pet]
}
Spring boot class
@ComponentScan
@EnableAutoConfiguration
@SpringBootApplication
public class AppGraphql {
public static void main(String[] args) {
SpringApplication.run(AppGraphql.class, args);
}
@Bean
public GraphQLSchema schema() {
return SchemaParser.newParser()
.files("petshop.graphqls", "Types.graphqls", "InputTypes.graphqls")
.resolvers(new Query(), new Mutation(), new Subscription(new NewsPublisher()))
.scalars(new ScalarDate())
.dictionary("WeirdPet", WeirdPet.class)
.build().makeExecutableSchema();
}
}
I guess we should still able to create union without creating a "request" that returns the specific union type in this case "WeirdPet" type. Is this a bug?
@villerdex
Was facing similar issue and was able to solve by creating a bean of SchemaParserDictionary with union member classes.
With your schema, it would look like:
@villerdex Right now it searches for the types starting at the root resolvers. That could be improved by taking the union definition into account as well.
"Object type MyType implements a known interface, but no class could be found for that type name. Please pass a class for type MyType in the parser's dictionary."
Reacted by John-Olav Storvold, Lars Kölpin-Freese, Matt Foster, Veera, Javier A. Espeche, Gustav Karlsson, DerDawid, Chris Smith, Erich Buri, Johan Rouve and 2 more
#854 covers the sealed case. If the class behind a union or interface is a Kotlin sealed class or interface, or a Java 17 sealed type, the scanner now uses the subclass named after each member. So no dictionary entries or dummy queries needed:
sealedclassPet {
data classNormalPet(valname:String) : Pet()
data classWeirdPet(valclassification:String) : Pet()
}
For plain class hierarchies there's no way to list the subclasses of Pet without scanning the classpath, so the dictionary is still the way to go (.dictionary(NormalPet::class.java, WeirdPet::class.java) or a SchemaParserDictionary bean like @amimehra mentioned). The error message already points there.
Closing this. @briankrug's @JsonSubTypes patch is tracked in #862.
Activity
orvillelim commentedon Oct 30, 2018
Found workaround in issue by making sure there's either query request or mutation request defined in schema that returns WeirdPet type
Before :
After
orvillelim commentedon Oct 30, 2018
I guess we should still able to create union without creating a "request" that returns the specific union type in this case "WeirdPet" type. Is this a bug?
amimehra commentedon Nov 9, 2018
@villerdex
Was facing similar issue and was able to solve by creating a bean of SchemaParserDictionary with union member classes.
With your schema, it would look like:
oliemansm commentedon Nov 17, 2018
@villerdex Right now it searches for the types starting at the root resolvers. That could be improved by taking the union definition into account as well.
gustavkarlsson commentedon Jul 27, 2019
Same thing seems to happen for interfaces:
arnabkd commentedon Aug 20, 2019
Any updates on this issue?
ubarua123 commentedon Feb 25, 2020
Guess not because I got this error on 6.0.1 as well
eburi commentedon Jul 8, 2020
Same in 7.0.1. I think this is a bug. It only seems to affect you if you happen to use a type in a union for the first time.
vojtapol commentedon Jul 8, 2020
No updates yet. We welcome contributions though.
briankrug commentedon Apr 9, 2024
I have a patch that fixes this if someone wants to incorporate it:
discover-union-and-interface-members.patch
Reuse the type match for sealed subclasses and fold their tests
oryan-block commentedon Oct 17, 2026
#854 covers the sealed case. If the class behind a union or interface is a Kotlin sealed class or interface, or a Java 17 sealed type, the scanner now uses the subclass named after each member. So no dictionary entries or dummy queries needed:
For plain class hierarchies there's no way to list the subclasses of
Petwithout scanning the classpath, so the dictionary is still the way to go (.dictionary(NormalPet::class.java, WeirdPet::class.java)or aSchemaParserDictionarybean like @amimehra mentioned). The error message already points there.Closing this. @briankrug's
@JsonSubTypespatch is tracked in #862.