Resolvers file reference
The resolvers type definition file expresses the types of resolvers you need to implement a GraphQL server. It is generated by the nitrogql generate
command and written to the path specified by the generate.resolversOutput
option.
Exported Types
Resolvers
Resolvers<Context>
is the type of all resolvers. Usually, a GraphQL server accepts an object of this type as itsresolvers
option.
Context
is the type of the context object passed to resolvers.
Example usage:
import { Resolvers } from "./generated/resolvers";
type Context = {};
const resolvers: Resolvers<Context> = {
// resolvers for top-level queries and mutations
Query: { /* ... */ },
Mutation: { /* ... */ },
// resolvers for types
User: { /* ... */ },
};
Resolver types for individual types are not exported directly. Instead, you can use some TypeScript trick to retrieve them.
import { Resolvers } from "./generated/resolvers";
const queryResolvers: Resolvers<Context>["Query"] = { /* ... */ };
ResolverOutput
By ResolverOutput<T>
we mean the TypeScript type of the object returned by a resolver for a GraphQL type T
. T
should be a string literal type that is the name of a GraphQL type.
In other words, it is the model type of T
; see nitrogql:model
plugin references for more details about the idea of model types.
import { Resolvers, ResolverOutput } from "./generated/resolvers";
type Context = {};
const queryResolvers: Resolvers<Context>["Query"] = {
/** get the current user */
me: () => {
// the type of the returned object is ResolverOutput<"User">
return {
id: "1",
name: "John Doe",
};
}
};
const userResolvers: Resolvers<{}>["User"] = {
email: async (user) => {
// the type of `user` is ResolverOutput<"User">
const email = await getEmail(user.id);
}
}
ResolverOutput
is useful when you want to give a name to the type of an object that should be returned by a resolver.