Skip to content

Commit

Permalink
Create Next.js Grafserv package and create an example PostGraphile se…
Browse files Browse the repository at this point in the history
…rver (#17)
  • Loading branch information
psirenny authored May 23, 2024
1 parent 302b724 commit 721eaba
Show file tree
Hide file tree
Showing 43 changed files with 3,252 additions and 371 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,18 @@ jobs:
uses: actions/[email protected]
with:
fetch-depth: 2
- name: Install Poetry
run: pipx install poetry
- name: Setup Node.js
uses: actions/[email protected]
with:
cache: yarn
node-version-file: .node-version
- name: Setup Python
uses: actions/[email protected]
with:
cache: poetry
python-version-file: .python-version
- name: Install Node.js dependencies
run: yarn install --immutable
- name: Build
Expand Down
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
*.log
.DS_Store
.turbo
node_modules
.pnp.*
.turbo
.venv
.yarn/*
node_modules
!.yarn/patches
!.yarn/plugins
!.yarn/releases
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.11
37 changes: 37 additions & 0 deletions .sqlfluff
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[sqlfluff]
dialect = postgres
max_line_length = 120
templater = placeholder

[sqlfluff:templater:placeholder]
DATABASE_OWNER=owner_role
param_style = colon

[sqlfluff:rules:ambiguous.column_references]
group_by_and_order_by_style = explicit

[sqlfluff:rules:ambiguous.join]
fully_qualify_join_types = both

[sqlfluff:rules:capitalisation.functions]
capitalisation_policy = lower
extended_capitalisation_policy = lower

[sqlfluff:rules:capitalisation.identifiers]
capitalisation_policy = lower
extended_capitalisation_policy = lower

[sqlfluff:rules:capitalisation.literals]
capitalisation_policy = upper

[sqlfluff:rules:capitalisation.keywords]
capitalisation_policy = upper

[sqlfluff:rules:capitalisation.types]
extended_capitalisation_policy = lower

[sqlfluff:rules:convention.casting_style]
preferred_type_casting_style = shorthand

[sqlfluff:rules:references.consistent]
single_table_references = unqualified
2 changes: 2 additions & 0 deletions .sqlfluffignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/committed/*.sql
schema.sql
71 changes: 71 additions & 0 deletions .yarn/patches/ruru-npm-2.0.0-beta.12-299bc077af.patch

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions .yarn/plugins/@yarnpkg/plugin-after-install.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable */
//prettier-ignore
module.exports = {
name: "@yarnpkg/plugin-after-install",
factory: function (require) {
"use strict";var plugin=(()=>{var s=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames;var C=Object.prototype.hasOwnProperty;var r=(t=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(t,{get:(o,e)=>(typeof require<"u"?require:o)[e]}):t)(function(t){if(typeof require<"u")return require.apply(this,arguments);throw new Error('Dynamic require of "'+t+'" is not supported')});var I=(t,o)=>{for(var e in o)s(t,e,{get:o[e],enumerable:!0})},h=(t,o,e,a)=>{if(o&&typeof o=="object"||typeof o=="function")for(let n of x(o))!C.call(t,n)&&n!==e&&s(t,n,{get:()=>o[n],enumerable:!(a=g(o,n))||a.enumerable});return t};var k=t=>h(s({},"__esModule",{value:!0}),t);var P={};I(P,{default:()=>y});var d=r("@yarnpkg/core");var f=r("@yarnpkg/core"),c={afterInstall:{description:"Hook that will always run after install",type:f.SettingsType.STRING,default:""}};var p=r("clipanion"),u=r("@yarnpkg/core");var m=r("@yarnpkg/shell"),l=async(t,o)=>{let e=t.get("afterInstall"),a=!!t.projectCwd?.endsWith(`dlx-${process.pid}`);return e&&!a?(o&&console.log("Running `afterInstall` hook..."),(0,m.execute)(e,[],{cwd:t.projectCwd||void 0})):0};var i=class extends p.Command{async execute(){let o=await u.Configuration.find(this.context.cwd,this.context.plugins);return l(o,!1)}};i.paths=[["after-install"]];var w={configuration:c,commands:[i],hooks:{afterAllInstalled:async(t,o)=>{if(o?.mode===d.InstallMode.UpdateLockfile)return;if(await l(t.configuration,!0))throw new Error("The `afterInstall` hook failed, see output above.")}}},y=w;return k(P);})();
return plugin;
}
};
9 changes: 5 additions & 4 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
afterInstall: yarn workspaces foreach -A run postinstall
compressionLevel: mixed

enableGlobalCache: false

nodeLinker: node-modules

npmPublishAccess: public

plugins:
- checksum: 0a2a35fbed2f33f0df1ceb1db51bf72554201f994eaecb86cbc62a295c3d05f7cc44fa8be8e64fc5e1c0bee4f529a17a0cc429ea9e3486ad467443291d5a8e3b
path: .yarn/plugins/@yarnpkg/plugin-after-install.cjs
spec: "https://raw.githubusercontent.com/mhassan1/yarn-plugin-after-install/v0.6.0/bundles/@yarnpkg/plugin-after-install.js"
yarnPath: .yarn/releases/yarn-4.2.2.cjs
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@spear-ai/api-monorepo",
"name": "@spear-ai/monorepo",
"version": "1.0.0",
"description": "Spear API",
"author": {
Expand All @@ -10,11 +10,12 @@
"type": "module",
"dependencies": {
"@changesets/changelog-github": "0.5.0",
"@changesets/cli": "2.27.2",
"@changesets/cli": "2.27.3",
"@commitlint/cli": "19.3.0",
"@commitlint/types": "19.0.3",
"@spear-ai/commitlint-config": "3.1.1",
"@spear-ai/eslint-config": "19.0.0",
"@delucis/if-env": "1.1.2",
"@spear-ai/commitlint-config": "3.2.0",
"@spear-ai/eslint-config": "20.0.0",
"@spear-ai/npm-package-json-lint-config": "3.1.1",
"@spear-ai/prettier-config": "2.2.0",
"@spear-ai/tsconfig": "4.0.0",
Expand All @@ -36,20 +37,24 @@
"type": "git",
"url": "https://github.com/spear-ai/api.git"
},
"resolutions": {
"ruru@npm:^2.0.0-beta.12": "patch:ruru@npm%3A2.0.0-beta.12#~/.yarn/patches/ruru-npm-2.0.0-beta.12-299bc077af.patch"
},
"scripts": {
"version": "changeset version && yarn workspaces foreach -A run sync-version",
"commitlint:check": "commitlint --from=HEAD^1",
"eslint:check": "eslint --max-warnings 0 .",
"eslint:fix": "yarn eslint:check --fix",
"github-actions:push": "act push -s GH_PAT=$GH_PAT -s GITHUB_TOKEN=$GH_PAT -s NPM_TOKEN=$NPM_TOKEN",
"github-actions:push:release-tag": "act push -e .github/test-events/push-release-tag.json -s GH_PAT=$GH_PAT -s GITHUB_TOKEN=$GH_PAT",
"npmpkgjsonlint:check": "npmPkgJsonLint .",
"postinstall": "if-env VERCEL='1' || (husky install && poetry install --no-root && cp .venv/lib/python3.11/site-packages/spear_ai_sqlfluff_config/.sqlfluff .sqlfluff)",
"prettier:check": "prettier --check .",
"prettier:fix": "prettier --write .",
"publish-packages": "yarn run publish-packages:npm && yarn run publish-packages:github && yarn run publish-packages:custom && yarn changeset tag",
"publish-packages:custom": "yarn workspaces foreach -A run publish-package",
"publish-packages:github": "npm_config_registry=https://npm.pkg.github.com changeset publish --no-git-tag",
"publish-packages:npm": "changeset publish --no-git-tag",
"sqlfluff:check": "poetry run sqlfluff lint packages/example-postgraphile-server/src/**/*.sql",
"sqlfluff:fix": "poetry run sqlfluff fix packages/example-postgraphile-server/src/**/*.sql --force",
"typescript:check": "tsc --noEmit"
},
"workspaces": [
Expand Down
1 change: 1 addition & 0 deletions packages/example-postgraphile-server/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
POSTGRES_URL=postgresql://postgres:postgres@localhost:5432/postgres
1 change: 1 addition & 0 deletions packages/example-postgraphile-server/.env.development
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
GRAPHILE_ENV=development
3 changes: 3 additions & 0 deletions packages/example-postgraphile-server/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.tsbuildinfo
.next
node_modules
6 changes: 6 additions & 0 deletions packages/example-postgraphile-server/.gmrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const graphileMigrateConfig = {
connectionString: "postgresql://postgres:postgres@localhost:5432/postgres",
migrationsFolder: "src/migrations",
};

module.exports = graphileMigrateConfig;
2 changes: 2 additions & 0 deletions packages/example-postgraphile-server/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
schema.graphql
9 changes: 9 additions & 0 deletions packages/example-postgraphile-server/.squawk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
assume_in_transaction = true
excluded_rules = [
"ban-drop-table",
"prefer-big-int",
"prefer-bigint-over-smallint",
"require-concurrent-index-creation",
"require-concurrent-index-deletion",
]
pg_version = "16.0"
17 changes: 17 additions & 0 deletions packages/example-postgraphile-server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# @spear-ai/example-postgraphile-server

An example [PostGraphile](https://postgraphile.org) server to test against custom plugins.

## Installation

```shell
yarn install
```

## Usage

Run the PostGraphile server in dev mode:

```shell
yarn turbo run dev
```
18 changes: 18 additions & 0 deletions packages/example-postgraphile-server/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: forerunner-database
services:
postgresql:
container_name: postgraphile-postgresql
environment:
PGHOST: /var/run/postgresql
POSTGRES_DB: postgres
POSTGRES_HOST: /var/run/postgresql
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
healthcheck:
interval: 3s
retries: 10
test: pg_isready -h postgresql -U postgres -d postgres
timeout: 3s
image: supabase/postgres:15.1.1.52
ports:
- 5432:5432
12 changes: 12 additions & 0 deletions packages/example-postgraphile-server/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { baseEslintConfig, prettierConfig } from "@spear-ai/eslint-config";

/** @type {import("eslint").Linter.FlatConfig} */
const eslintConfig = [
{
ignores: ["**/.next", "dist", "node_modules"],
},
...baseEslintConfig,
prettierConfig,
];

export default eslintConfig;
5 changes: 5 additions & 0 deletions packages/example-postgraphile-server/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
23 changes: 23 additions & 0 deletions packages/example-postgraphile-server/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
eslint: {
ignoreDuringBuilds: true,
},
experimental: {
esmExternals: true,
serverComponentsExternalPackages: [
"grafast",
"grafserv",
"graphile-export",
"graphql",
"postgraphile",
"ruru",
],
},
poweredByHeader: false,
typescript: {
ignoreBuildErrors: true,
},
};

export default nextConfig;
11 changes: 11 additions & 0 deletions packages/example-postgraphile-server/npmpackagejsonlint.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
extends: ["@spear-ai/npm-package-json-lint-config/spear-application"],
rules: {
"prefer-absolute-version-dependencies": [
"error",
{
exceptions: ["@spear-ai/grafserv-next-server", "@spear-ai/graphile-sort-schema-plugin"],
},
],
},
};
70 changes: 70 additions & 0 deletions packages/example-postgraphile-server/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"name": "@spear-ai/example-postgraphile-server",
"version": "1.0.0",
"description": "Example PostGraphile server",
"author": {
"name": "Spear AI",
"email": "[email protected]",
"url": "https://spear.ai"
},
"type": "module",
"dependencies": {
"@dataplan/pg": "0.0.1-beta.22",
"@graphile/simplify-inflection": "8.0.0-beta.5",
"@spear-ai/grafserv-next-server": "*",
"@spear-ai/graphile-sort-schema-plugin": "*",
"dirfilename": "1.1.1",
"next": "14.2.3",
"postgraphile": "5.0.0-beta.25",
"react": "18.3.1",
"react-dom": "18.3.1",
"ruru": "patch:ruru@npm%3A2.0.0-beta.12#~/.yarn/patches/ruru-npm-2.0.0-beta.12-299bc077af.patch"
},
"devDependencies": {
"@dotenv-run/cli": "1.3.5",
"@spear-ai/eslint-config": "20.0.0",
"@spear-ai/npm-package-json-lint-config": "3.1.1",
"@spear-ai/prettier-config": "2.2.0",
"@spear-ai/tsconfig": "4.0.0",
"autoprefixer": "10.4.19",
"concurrently": "8.2.2",
"eslint": "8.57.0",
"graphile-migrate": "2.0.0-rc.2",
"graphql": "16.8.1",
"npm-package-json-lint": "7.1.0",
"postcss": "8.4.38",
"prettier": "3.2.5",
"squawk-cli": "0.28.0",
"tailwindcss": "3.4.3",
"turbo": "1.13.3",
"typescript": "5.4.5"
},
"license": "MIT",
"packageManager": "[email protected]",
"private": true,
"repository": {
"type": "git",
"directory": "packages/example-postgraphile-server",
"url": "https://github.com/spear-ai/api.git"
},
"scripts": {
"build": "yarn run next:build",
"database:pre-dev": "docker compose up --build --wait",
"dev": "concurrently yarn:migrate:dev yarn:next:dev",
"dotenv-linter:check": "docker run --rm -v \"$(pwd)\":/app -w /app dotenvlinter/dotenv-linter:3.3.0 .env*",
"dotenv-linter:fix": "docker run --rm -v \"$(pwd)\":/app -w /app dotenvlinter/dotenv-linter:3.3.0 fix .env*",
"eslint:check": "eslint --max-warnings 0 .",
"eslint:fix": "yarn eslint:check --fix",
"migrate:dev": "dotenv-run -e development -- graphile-migrate watch",
"migrate:up": "dotenv-run -e production -- graphile-migrate watch --once",
"next:build": "next build",
"next:dev": "next dev",
"next:start": "next start",
"npmpkgjsonlint:check": "npmPkgJsonLint .",
"pre-dev": "yarn database:pre-dev",
"prettier:check": "prettier --check .",
"prettier:fix": "prettier --write .",
"squawk:check": "squawk --assume-in-transaction src/migrations/current/*.sql",
"typescript:check": "tsc --noEmit"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { NextResponse } from "next/server";
import { ruruHTML } from "ruru/server";

export const GET = () =>
new NextResponse(ruruHTML({ endpoint: `/api/graphql` }), {
headers: { "Content-Type": "text/html" },
status: 200,
});
38 changes: 38 additions & 0 deletions packages/example-postgraphile-server/src/app/api/graphql/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import path from "node:path";
import { PgSimplifyInflectionPreset } from "@graphile/simplify-inflection";
import { grafserv } from "@spear-ai/grafserv-next-server";
import { sortSchemaPlugin } from "@spear-ai/graphile-sort-schema-plugin";
import { dirname } from "dirfilename";
import { NextRequest } from "next/server";
import { makeSchema } from "postgraphile";
import { makePgService } from "postgraphile/adaptors/pg";
import { PostGraphileAmberPreset } from "postgraphile/presets/amber";
import { PostGraphileRelayPreset } from "postgraphile/presets/relay";

const preset: GraphileConfig.Preset = {
extends: [PostGraphileAmberPreset, PostGraphileRelayPreset, PgSimplifyInflectionPreset],
gather: {
pgStrictFunctions: true,
},
pgServices: [
makePgService({
connectionString: process.env.POSTGRES_URL ?? "",
schemas: ["public"],
}),
],
plugins: [sortSchemaPlugin],
schema: {
dontSwallowErrors: process.env.NODE_ENV === "production",
exportSchemaSDLPath: path.join(dirname(import.meta.url), "./schema.graphql"),
pgSimplifyPatch: false,
retryOnInitFail: process.env.NODE_ENV === "development",
sortExport: true,
},
};

export const POST = async (request: NextRequest) => {
const { schema } = await makeSchema(preset);
const server = grafserv({ preset, schema });
const handle = server.createGraphQLHandler();
return handle(request);
};
Loading

0 comments on commit 721eaba

Please sign in to comment.