MichalLytek / MichalLytek/typegraphql-prisma

Union Resolvers feature idea

Open
#12 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement priority:low
Dominant language
TypeScript
Stars
918
Forks
130
PR merge metrics
No merged PRs in 30d

Description

I had an idea for another possible feature.
Union Resolvers. Adding /// @TypeGraphql.Union = result to two models will generate an additional set of findOne/findMany revolvers of a union type of each model that is in that union.

An example may look like this.

/// @TypeGraphql.Union = product
model Movie {
  id        Int     @id @default(autoincrement())
  title     String
  length    Int
  actors    Actor[]
}

/// @TypeGraphql.Union = product
model Book {
  id        Int     @id @default(autoincrement())
  title     String
  pages     Int   
}

model Actor {
  id        Int     @id @default(autoincrement())
  name      String
}

Which would generate this type and the resolvers for it.

export const Product = createUnionType({
  name: 'Product',
  types: () => [Movie, Book],
});

The Prisma code for this may look something like this.

const movies = await prisma.movies.findMany(args).then((movies: Movie[]) => plainToClass(Movie, movies));
const books = await prisma.books.findMany(args).then((books: Book[]) => plainToClass(Book, books));
return [...movies, ...books].sort(byArgs(args));

The difficult part would be defining the orderBy and where arguments as you would need to determine what fields were shared between the two (or more) models and use those to generate the arguments.
Additionally sorting will likely have some additional overhead but it would be cool to be able to do something like this.

{
  products {
    ... on Book {
      title
      pages
    }
    ... on Movie {
      title
      length
    }
  }
}

Originally posted by @wSedlacek in https://github.com/MichalLytek/typegraphql-prisma/issues/1#issuecomment-674492557

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No files, tests, or implementation entry points are named. Start by locating the generator paths that emit GraphQL types and CRUD resolvers, then determine how union members, shared fields, filtering, ordering, and resolver output would be represented. Done would require an agreed design and working generated union types and resolvers.

Written by the indexing model from the issue text.

Assessment

Tech stack
graphql, typescript
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.