Skip to content

Fastify

Use MeshQL with Fastify via createMeshFastifyPlugin.

  • Node.js 22+
  • Fastify 4 or 5
Terminal window
npm install meshql-core meshql-http fastify
import Fastify from "fastify";
import { createMesh, buildSelectSql, type MeshSchema } from "@meshql/core";
import { createMeshFastifyPlugin } from "@meshql/http/fastify";
const schema: MeshSchema = {
entities: {
user: { type: {} as { id: number; name: string }, fields: ["id", "name"], table: "users" },
token: {
type: {} as { accessToken: string },
fields: ["accessToken"],
table: "tokens",
columns: { accessToken: "access_token" },
},
},
joins: {
"user.tokens": {
entity: "token",
on: "tokens.user_id = users.id",
type: "many",
},
},
};
const mesh = createMesh(schema);
mesh.resolve("user", async (plan) => {
const { sql, params } = buildSelectSql(plan, schema);
return [{ user_id: 1, user_name: "Ada", tokens_accessToken: "tok_abc" }];
});
const fastify = Fastify();
await fastify.register(createMeshFastifyPlugin(mesh, { basePath: "/mesh" }));
await fastify.listen({ port: 3001, host: "0.0.0.0" });
console.log("MeshQL on http://localhost:3001/mesh");
interface MeshFastifyOptions {
basePath?: string; // default: "/mesh"
}
import { createClient } from "@meshql/client";
const client = createClient({ url: "http://localhost:3001/mesh" });
const user = await client.query(
{ user: { id: true, name: true, tokens: { accessToken: true } } },
{ entityId: "1" },
);

HTTP adapters