Inferring Types
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.
When building helpers, most Pothos types have a generic called Types
that extends SchemaTypes
.
This combines all the defaults and settings passed in when creating the SchemaBuilder. To make you
own type helpers and utility functions, you often need access the the Types
used by your builder.
This can be inferred from the builder using typeof builder.$inferSchemaTypes
.
The following is a simple helper that for creating objects that have an id
field. The helper
itself isn't that useful, but shows how inferring SchemaTypes from a builder can work.
Rather than explicitly using the inferred type, you can also infer SchemaTypes from the builder in
an argument. In the following example, we pass in the builder to the createPaginationArgs, and infer
the Types
from the provided builder. This is useful when building helpers that might be used with
multiple builder instances.