Pothos GraphQL
Searching...

Inferring TypeScript Types from Refs

In some cases you may want to use the types from your input of object refs to build helpers, or provide accurate types for other functions.

To get types from any Pothos ref object, you can use the $inferType and $inferInput properties on the ref. This pattern is inspired by drizzle ORM.

const MyInput = builder.inputType('MyInput', {
  fields: (t) => ({
    id: t.id({ required: true }),
    name: t.string({ required: true }),
  }),
});

// { id: string; name: string; }
type MyInputShape = typeof MyInput.$inferInput; /

// infer the shape of the Prisma User model
const UserRef = builder.prismaObject('User', {});
type UserType = typeof UserRef.$inferType;