
createResolver()
import { createResolver } from '@green-stack/schemas/createResolver'
- createResolver.ts
The createResolver()
function is a utility that simplifies the creation of data resolvers from Data Bridges:
/* --- someResolver() --- */
/* -i- ... */
export const someResolver = createResolver(async ({
args, // <- Auto inferred types (from bridge 'inputSchema')
context, // <- Request context (from next.js middleware)
parseArgs, // <- Input validator (from bridge 'inputSchema')
withDefaults, // <- Response helper (from bridge 'outputSchema')
}) => {
// Validate input and apply defaults
const validatedArgs = parseArgs(args)
// -- Business Logic --
// ... Your back-end business logic goes here ...
// -- Respond --
// Typecheck response and apply defaults from bridge's outputSchema
return withDefaults({
...,
})
}, someDataBridge)
Pass the bridge (☝️) as the second argument to
createResolver()
to:
- 1️⃣ infer the input / arg types from the bridge’s
inputSchema
- 2️⃣ enable
parseArgs()
andwithDefaults()
helpers for validation, hints + defaults
The resulting function can be used as just another async function anywhere in your back-end.
The difference with a regular function, since the logic is bundled together with its data-bridge / input + output metadata, is that we can easily transform it into APIs
Why Resolvers?
Our definition of a resolver is a function that:
- Takes arguments (input data)
- Returns a value (output data)
- Can be used as an API (e.g. in a Next.js route handler or GraphQL API)
- Has its metadata like the input and output schema bundled wiht it (Data Bridges)
That last part is important, because when you bundle the business logic with it’s input and output metadata, you can easilyt transform it into APIs, while still keeping it as reusable function returning a promise.
More about this in the Data Bridges docs.
Related Automations
add-resolver
- generator
You don’t need to manually create a new resolver with createResolver()
. You can use the add-resolver
generator to create a new resolver and all related files (like the DataBridge and API route) in one go:
npx turbo gen resolver