Use Fastify Mercurius and GraphQL contracts
Some checks are pending
Build Docker Image / build (push) Waiting to run

This commit is contained in:
Ruslan Bakiev
2026-06-01 00:25:27 +05:00
parent e5cbb8855d
commit 7eb1742d6a
6 changed files with 1252 additions and 1614 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "graphql-contracts"]
path = graphql-contracts
url = git@gitea.dsrptlab.com:optovia/orders-graphql-contracts.git

1
graphql-contracts Submodule

Submodule graphql-contracts added at 88e1aa58bf

2664
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -9,18 +9,16 @@
"start": "prisma migrate deploy && node dist/index.js"
},
"dependencies": {
"@apollo/server": "^4.11.3",
"@fastify/cors": "^11.2.0",
"@prisma/client": "^6.5.0",
"cors": "^2.8.5",
"express": "^4.21.2",
"@sentry/node": "^9.5.0",
"fastify": "^5.8.5",
"graphql": "^16.10.0",
"graphql-tag": "^2.12.6",
"jose": "^6.0.11",
"@sentry/node": "^9.5.0"
"mercurius": "^16.9.0"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^5.0.0",
"@types/node": "^22.13.0",
"prisma": "^6.5.0",
"tsx": "^4.19.0",

View File

@@ -1,6 +1,6 @@
import { createRemoteJWKSet, jwtVerify, type JWTPayload } from "jose";
import { GraphQLError } from "graphql";
import type { Request } from "express";
import type { FastifyRequest } from "fastify";
const LOGTO_JWKS_URL =
process.env.LOGTO_JWKS_URL || "https://auth.optovia.ru/oidc/jwks";
@@ -16,7 +16,7 @@ export interface AuthContext {
scopes: string[];
}
function getBearerToken(req: Request): string {
function getBearerToken(req: FastifyRequest): string {
const auth = req.headers.authorization || "";
if (!auth.startsWith("Bearer ")) {
throw new GraphQLError("Missing Bearer token", {
@@ -61,7 +61,7 @@ export async function publicContext(): Promise<AuthContext> {
return { scopes: [] };
}
export async function userContext(req: Request): Promise<AuthContext> {
export async function userContext(req: FastifyRequest): Promise<AuthContext> {
const token = getBearerToken(req);
const { payload } = await jwtVerify(token, jwks, { issuer: LOGTO_ISSUER });
return {
@@ -70,7 +70,7 @@ export async function userContext(req: Request): Promise<AuthContext> {
};
}
export async function teamContext(req: Request): Promise<AuthContext> {
export async function teamContext(req: FastifyRequest): Promise<AuthContext> {
const token = getBearerToken(req);
const { payload } = await jwtVerify(token, jwks, {
issuer: LOGTO_ISSUER,
@@ -95,7 +95,9 @@ export async function teamContext(req: Request): Promise<AuthContext> {
};
}
export async function managerContext(req: Request): Promise<AuthContext> {
export async function managerContext(
req: FastifyRequest,
): Promise<AuthContext> {
const token = getBearerToken(req);
const { payload } = await jwtVerify(token, jwks, {
issuer: LOGTO_ISSUER,

View File

@@ -1,100 +1,104 @@
import express from 'express'
import cors from 'cors'
import { ApolloServer } from '@apollo/server'
import { expressMiddleware } from '@apollo/server/express4'
import * as Sentry from '@sentry/node'
import { publicTypeDefs, publicResolvers } from './schemas/public.js'
import { userTypeDefs, userResolvers } from './schemas/user.js'
import { teamTypeDefs, teamResolvers } from './schemas/team.js'
import { publicContext, userContext, teamContext, managerContext, type AuthContext } from './auth.js'
import Fastify from "fastify";
import type { FastifyInstance, FastifyRequest } from "fastify";
import cors from "@fastify/cors";
import mercurius from "mercurius";
import * as Sentry from "@sentry/node";
import { publicTypeDefs, publicResolvers } from "./schemas/public.js";
import { userTypeDefs, userResolvers } from "./schemas/user.js";
import { teamTypeDefs, teamResolvers } from "./schemas/team.js";
import {
publicContext,
userContext,
teamContext,
managerContext,
} from "./auth.js";
const PORT = parseInt(process.env.PORT || '8000', 10)
const SENTRY_DSN = process.env.SENTRY_DSN || ''
const PORT = Number.parseInt(process.env.PORT || "8000", 10);
const SENTRY_DSN = process.env.SENTRY_DSN || "";
if (SENTRY_DSN) {
Sentry.init({
dsn: SENTRY_DSN,
tracesSampleRate: 0.01,
release: process.env.RELEASE_VERSION || '1.0.0',
environment: process.env.ENVIRONMENT || 'production',
})
release: process.env.RELEASE_VERSION || "1.0.0",
environment: process.env.ENVIRONMENT || "production",
});
}
const app = express()
const app = Fastify();
await app.register(cors, { origin: ["https://optovia.ru"], credentials: true });
app.use(cors({ origin: ['https://optovia.ru'], credentials: true }))
type GraphqlBody = {
query?: string;
variables?: Record<string, unknown>;
operationName?: string;
};
const publicServer = new ApolloServer<AuthContext>({
typeDefs: publicTypeDefs,
resolvers: publicResolvers,
introspection: true,
})
const userServer = new ApolloServer<AuthContext>({
typeDefs: userTypeDefs,
resolvers: userResolvers,
introspection: true,
})
const teamServer = new ApolloServer<AuthContext>({
typeDefs: teamTypeDefs,
resolvers: teamResolvers,
introspection: true,
})
const managerServer = new ApolloServer<AuthContext>({
typeDefs: teamTypeDefs,
resolvers: teamResolvers,
introspection: true,
})
await Promise.all([publicServer.start(), userServer.start(), teamServer.start(), managerServer.start()])
app.use(
'/graphql/public',
express.json(),
expressMiddleware(publicServer, {
context: async () => publicContext(),
}) as unknown as express.RequestHandler,
)
app.use(
'/graphql/user',
express.json(),
expressMiddleware(userServer, {
context: async ({ req }) => {
try {
return await userContext(req as unknown as import('express').Request)
} catch {
return { scopes: [] }
}
async function registerGraphqlEndpoint(
server: FastifyInstance,
path: string,
schema: string,
resolvers: unknown,
context: (request: FastifyRequest) => Promise<unknown> | unknown,
) {
await server.register(
async (route) => {
await route.register(mercurius, {
schema,
resolvers: resolvers as never,
routes: false,
});
route.post("/", async (request, reply) => {
const body = request.body as GraphqlBody;
if (typeof body.query !== "string") {
throw new Error("GraphQL query is required");
}
return reply.graphql(
body.query,
(await context(request)) as Record<string, unknown>,
body.variables,
body.operationName,
);
});
},
}) as unknown as express.RequestHandler,
)
{ prefix: path },
);
}
app.use(
'/graphql/team',
express.json(),
expressMiddleware(teamServer, {
context: async ({ req }) => teamContext(req as unknown as import('express').Request),
}) as unknown as express.RequestHandler,
)
await registerGraphqlEndpoint(
app,
"/graphql/public",
publicTypeDefs,
publicResolvers,
async () => publicContext(),
);
await registerGraphqlEndpoint(
app,
"/graphql/user",
userTypeDefs,
userResolvers,
userContext,
);
await registerGraphqlEndpoint(
app,
"/graphql/team",
teamTypeDefs,
teamResolvers,
teamContext,
);
await registerGraphqlEndpoint(
app,
"/graphql/manager",
teamTypeDefs,
teamResolvers,
managerContext,
);
app.use(
'/graphql/manager',
express.json(),
expressMiddleware(managerServer, {
context: async ({ req }) => managerContext(req as unknown as import('express').Request),
}) as unknown as express.RequestHandler,
)
app.get("/health", async () => ({ status: "ok" }));
app.get('/health', (_, res) => {
res.json({ status: 'ok' })
})
app.listen(PORT, '0.0.0.0', () => {
console.log(`Orders server ready on port ${PORT}`)
console.log(` /graphql/public - public`)
console.log(` /graphql/user - id token auth`)
console.log(` /graphql/team - team access token auth`)
console.log(` /graphql/manager - manager JWT auth`)
})
await app.listen({ port: PORT, host: "0.0.0.0" });
console.log(`Orders server ready on port ${PORT}`);
console.log(` /graphql/public - public`);
console.log(` /graphql/user - id token auth`);
console.log(` /graphql/team - team access token auth`);
console.log(` /graphql/manager - manager JWT auth`);