-
-
Notifications
You must be signed in to change notification settings - Fork 11
Description
Hello there.
Thanks for this amazing project, which helps us delivering an easy-to-maintain GQL API at my company π€
I have a question regarding how Pylon discriminates results in Union scenarios (related to Union of multi-entities searches). We found a solution, so we are not blocked, but I would like to have your opinion on this π
Considering this sample of types:
interface Node {
id: number;
}
interface Pagination<T> {
total: number;
nodes: T[];
}
interface Toto extends Node {
name: string;
propOfToto: string;
}
interface Tata extends Node {
name: string;
propOfTata: string;
}
interface PaginatedToto extends Pagination<Toto> {
isToto: true;
}
interface PaginatedTata extends Pagination<Tata> {
isTata: true;
}
type SearchResult = PaginatedToto | PaginatedTata;and this graphql query with a set of basic data:
export const graphql = {
Query: {
searchResults: () => {
return [
{
total: 2,
isToto: true,
nodes: [
{
id: 1,
name: "Toto1",
propOfToto: "propOfToto1",
},
{
id: 2,
name: "Toto2",
propOfToto: "propOfToto2",
},
],
},
{
total: 3,
isTata: true,
nodes: [
{
id: 11,
name: "Tata1",
propOfTata: "propOfTata1",
},
{
id: 12,
name: "Tata2",
propOfTata: "propOfTata2",
},
{
id: 13,
name: "Tata3",
propOfTata: "propOfTata3",
},
],
},
] as SearchResult[];
},
},
};we did not find a proper way to discrimate results in our fragments, as it seems that pylon infers the final type according to properties of types mentionned in the Union, but what if those types shares the same properties like "total" and "nodes" in this case ?
we ended with an injection of a specific property inside each type
So I was wondering if there is a better way to hint pylon about how to resolve some types ? something like
resolvers: { SearchResult: { __resolveType: function resolveType(node) {
if (node && typeof node === "object") {
if (node.klass === "PaginatedToto" && "total" in node && "nodes" in node) {
return "PaginatedToto";
}
;
if (node.klass === "PaginatedTata" && "total" in node && "nodes" in node) {
return "PaginatedTata";
}
;
}
} }


