WaspW
Wasp11mo ago
cooluke.

What typescript type for queries that use "include"?

Normal behavior: When my query just returns an object from the database, Wasp automatically includes the proper Typescript type for me, like this:
export const getDecks: GetDeck<any, Deck[]> = async (args, context) => {
  return context.entities.Deck.findMany({
    where: {
      userId: context.user.id
    },
  });
}

// in frontend:
const { data: decks, isLoading, error } = useQuery(getDecks);
// decks already has type Deck[]! Amazing!


But if I want to use Prisma's
_include
to include related objects, those won't be included in the automatic typescript type.
export const getDecks: GetDeck<any, Deck[]> = async (args, context) => {
  return context.entities.Deck.findMany({
    where: {
      userId: context.user.id
    },
    include: {
      cards: true
    },
  });
}

// in frontend:
const { data: decks, isLoading, error } = useQuery(getDecks);
const firstCard = decks[0].cards[0];
// ^ typescript error
// type Deck does not have a `cards` member


What to do in this case? I can't for the life of me figure out a good solution.
Was this page helpful?