
createGraphResolver()
import { createGraphResolver } from '@green-stack/schemas/createGraphResolver'
- createGraphResolver.ts
We made it quite easy to enable GraphQL for your resolvers. The flow is quite similar to adding API routes.
From a related route file, add the following:
import { updatePost } from '@app/resolvers/updatePost.resolver'
import { createNextRouteHandler } from '@green-stack/schemas/createNextRouteHandler'
import { createGraphResolver } from '@green-stack/schemas/createGraphResolver'
/* --- Routes ------------ */
// exports of `GET` / `POST` / `PUT` / ...
/* --- GraphQL ----------- */
export const graphResolver = createGraphResolver(updatePost)
// Automatically extracts input (☝️) from graphql request context
After exporting graphResolver
here, restart the dev server or run npm run build:schema
manually.
This will:
-
- pick up the
graphResolver
export
- pick up the
-
- put it in our list of graphql compatible resolvers at
resolvers.generated.ts
in@app/registries
- put it in our list of graphql compatible resolvers at
-
- recreate
schema.graphql
from input & output schemas from registered resolvers
- recreate
You can now check out your GraphQL API playground at /api/graphql
Related Automations
collect-resolvers
- script
npm run collect:resolvers
This script will:
-
- Scan the
/routes/
folder in any of your non-app workspaces
- Scan the
-
- Find all
route.ts
files that export acreateGraphResolver()
function
- Find all
-
- Re-export them in the
@app/registries/resolvers.generated.ts
file
- Re-export them in the
The goal of this script is so you can define which resolvers should be registered for GraphQL on a feature by feature basis, instead of manually linking them somewhere centrally.
Overall, this will keep your features more modular and portable between GREEN stack / FullProduct.dev projects.
The collect-routes
script runs automatically on npm run dev
and any production build commands.
build-schema
- script
npm run build:schema
This script will:
-
- Import all the resolvers from your
@app/registries/resolvers.generated.ts
file
- Import all the resolvers from your
-
- Read the input and output schemas from each resolver (the Data Bridge for the resolver logic)
-
- Generate a new
schema.graphql
file in@app/core/graphql/
folder
- Generate a new
-
- Bind the schema to the resolvers to create an executable schema and effortless GraphQL API
Meaning you won’t need to manually write your own GraphQL schema, it will be generated automatically based on your resolvers.
The build-schema
script runs automatically on npm run dev
and any production build commands.
add-resolver
- generator
You don’t need to manually create a new API route with createGraphResolver()
every time you create a new resolver. You can use the add-resolver
generator to create a new resolver and its API route at once:
npx turbo gen resolver