GraphQL Server with Fastify, Mercurius, Prisma, and Nexus
This repository is no longer maintained and some of the packages may be out of date.
Nonetheless, there are two patterns in here that I think are still really useful for anyone building a GraphQL API:
This repo shows how to build a GraphQL server with TypeScript and the following technologies:
The project is written in TypeScript and attempts to maintain a high degree of type-safety by leveraging Prisma and GraphQL.
Play with a deployed version of this API: https://fastify-prisma.up.railway.app/altair
The database schema is defined using the Prisma schema which defines 3 models:
The GraphQL schema is defined with Nexus using the code-first approach.
The relevant files are:
.env
file and define DATABASE_URL
and SENTRY_DSN
npm install
npm run migrate:dev
to run shcema migrations with Prisma Migrate
npm run dev
to start dev server and run the APIThe GraphQL server is instrumented with OpenTelemetry tracing.
Here's how it works:
@autorelic/fastify-opentelemetry
is a plugin that creates a root span for every fastify HTTP request and allows creating child spans using request.openTelemetry()
@opentelemetry/instrumentation-graphql
provides auto-instrumentation for GraphQL executioncontext.request.openTelemetry()
.You can view traces in local development using Jaeger.
docker compose up -d
.env
file set JAEGER_EXPORTER="true"
http://localhost:16686/