I was wondering that how can we handle Runtime polymorphism in Graphql Schema file,
@Data
class Parent {
String libraryName;
}
@Data
class Child extends Parent {
String bookName;
}
Test class=>
class Test {
String id;
Child childObj;
}
Now in normal case Runtime polymorphism will happen and Test.Child will be initialized with Parent class object i.e. Parent prnt = new Child();
e.g. Schema.graphqls
type Test{
id: String!
childObj: Child // Cant assign Parent object
}
// This doesn't work
type Child {
bookName: String
libraryName: String
}
type Parent {
bookName: String
libraryName: String
}
But in GraphQl Schema file how to mention it ?
In your example, you could define an interface that both the Child
and Parent
types implement. For example, maybe you'd name it Member
and change the type of Test.childObj
to Member
:
interface Member {
bookName: String
libraryName: String
}
type Test {
id: String!
childObj: Member
}
type Child implements Member {
bookName: String
libraryName: String
}
type Parent implements Member {
bookName: String
libraryName: String
}
Another option might be the extend
feature but that doesn't seem to be widely available yet. See this post .