A Pothos integration for unthrown's
Result: GraphQL fields resolved by aResult, each failure answered as a typed member of the field's result union — without a throw.
📖 Documentation · API Reference
pnpm add @unthrown/pothos unthrown @pothos/core @pothos/plugin-errors graphql@pothos/plugin-errors turns a field's error classes into a result union, but
it learns of an error by catching it, and nothing ties the classes a field lists
to the failures its resolver can raise. @unthrown/pothos closes that gap: the
resolver answers a Result, and refusals names the GraphQL error class each
case of its failure becomes — checked like a matcher, so a case left out does not
compile.
| unthrown | GraphQL |
|---|---|
Ok(value) |
the field's value |
Err(error) |
the refusal its case maps to, a member of the result union |
Defect |
a thrown error, which the server must mask |
A Defect is never answered as a typed refusal, but its message is only hidden
if the server masks errors: GraphQL Yoga does by default; executing the schema
with graphql-js alone returns the message as it is.
import SchemaBuilder from "@pothos/core";
import ErrorsPlugin from "@pothos/plugin-errors";
import UnthrownPlugin from "@unthrown/pothos";
class BookNotFound extends Error {
constructor(failure: NotFound) {
super(`No book titled ${failure.title}`);
}
}
const builder = new SchemaBuilder({
plugins: [ErrorsPlugin, UnthrownPlugin],
errors: { directResult: true },
});
builder.objectType(BookNotFound, {
name: "BookNotFound",
fields: (t) => ({ message: t.exposeString("message") }),
});
builder.queryField("book", (t) =>
t.resultField({
type: Book,
args: { title: t.arg.string({ required: true }) },
refusals: { NotFound: BookNotFound }, // one class per case of the failure
resolve: (_root, { title }) => library.find(title), // AsyncResult<Book, NotFound>
}),
);A failure names its case by its _tag (a TaggedError) or its code (an
ORPCError from @unthrown/orpc's client, or any code-discriminated value).
resultField—t.fieldwith aResult-answeringresolveand itsrefusals.resultConnection(@unthrown/pothos/relay) — the same for@pothos/plugin-relay'st.connection.outcomeOf— aResultas a DataLoader answers one key: the value or the refusal, aDefectas anErrorrather than a rejected batch. Pass it the builder'sdefaultTypes.
A Defect is thrown as an error of the operation, never claimed as a refusal;
hiding its message is the server's job (GraphQL Yoga masks by default,
graphql-js alone does not). A GraphQLError defect is wrapped with its original
cause so its message and extensions cannot bypass default masking; answer
client-visible refusals through a mapped Err. outcomeOf wraps it too.
Peers: @pothos/core ^4.15.0, @pothos/plugin-errors ^4.9.0, graphql
^16.10.0 || ^17.0.0, and @pothos/plugin-relay ^4.8.0 (optional, for
/relay).
MIT