@green-stack/coreschemascreateGraphResolver
Alt description missing in image

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:

features / @app-core / routes / api / health / route.ts
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:

    1. pick up the graphResolver export
    1. put it in our list of graphql compatible resolvers at resolvers.generated.ts in @app/registries
    1. recreate schema.graphql from input & output schemas from registered resolvers

You can now check out your GraphQL API playground at /api/graphql

Apollo Server GraphQL Playground Preview

collect-resolvers - script

npm run collect:resolvers

This script will:

    1. Scan the /routes/ folder in any of your non-app workspaces
    1. Find all route.ts files that export a createGraphResolver() function
    1. Re-export them in the @app/registries/resolvers.generated.ts file

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:

    1. Import all the resolvers from your @app/registries/resolvers.generated.ts file
    1. Read the input and output schemas from each resolver (the Data Bridge for the resolver logic)
    1. Generate a new schema.graphql file in @app/core/graphql/ folder
    1. 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