Skip to content

benjie/postgraphile-hono

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PostGraphile hono example

A demo of PostGraphile v5 + Hono + Node 24 + TypeScript.

To start: clone, then:

yarn && yarn start

How this repo was created:

Following TypeScript project setup but adjusted for Hono.

Basic TypeScript project setup

mkdir postgraphile-hono
cd postgraphile-hono

echo 24 > .nvmrc
nvm use

echo /node_modules >> .gitignore

npm init -y
npm pkg delete author license description keywords scripts main
npm pkg set private=true --json
npm pkg set type=module scripts.start="node --env-file=./.env src/server.ts" scripts.build=tsc scripts.prod="node dist/server.js"

yarn add --dev typescript @tsconfig/node24 @types/node prettier

Create tsconfig.json

{
  "extends": "@tsconfig/node24/tsconfig.json",
  "compilerOptions": {
    "erasableSyntaxOnly": true,
    "rewriteRelativeImportExtensions": true,
    "rootDir": "./src",
    "outDir": "./dist"
  }
}

Install PostGraphile/hono

Now for PostGraphile / hono:

yarn add postgraphile@beta @graphile/simplify-inflection@beta hono @hono/node-server

Set up required files

Create .env:

DATABASE_URL=postgres:///postgraphile_example
GRAPHILE_ENV=development

Create src/graphile.config.ts:

import { makePgService } from "postgraphile/adaptors/pg";
import { PostGraphileAmberPreset } from "postgraphile/presets/amber";
import { PgSimplifyInflectionPreset } from "@graphile/simplify-inflection";

const preset: GraphileConfig.Preset = {
  extends: [PostGraphileAmberPreset, PgSimplifyInflectionPreset],
  pgServices: [
    makePgService({
      connectionString: process.env.DATABASE_URL,
      schemas: ["public"],
    }),
  ],
  grafast: {
    explain: true,
  },
};

export default preset;

Create src/pgl.ts:

import preset from "./graphile.config.ts";
import { postgraphile } from "postgraphile";

export const pgl = postgraphile(preset);

Create src/server.ts based on the Grafserv setup for Hono:

import { grafserv } from "grafserv/hono/v4";
import { pgl } from "./pgl.ts";

import { Hono } from "hono";
import { serve } from "@hono/node-server";

// Create a Node HTTP server
const app = new Hono();

// Create a Grafserv instance
// the second argument is an optional websocket upgrade handler
// see https://hono.dev/docs/helpers/websocket
const serv = pgl.createServ(grafserv);

// Mount the request handler into a new HTTP server
serv.addTo(app).catch((e) => {
  console.error(e);
  process.exit(1);
});

// Start the server with the chosen Hono adapter - here Node.js
serve(app, (info) => {
  console.log(`Listening on http://[${info.address}]:${info.port}`);
});

Run it:

yarn start

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published