Skip to content

How to discriminate results in Unions with same properties shape ?Β #95

@Alahel

Description

@Alahel

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 ?

Image

Image

we ended with an injection of a specific property inside each type

Image

Image

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";
      }
      ;
    }
  } }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions