Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(grafast/grafserv/src/servers/deno.ts): add deno server #500

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions grafast/grafserv/examples/example-deno.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { grafserv } from "npm:grafserv/deno/v1";
import preset from "./graphile.config.mjs";
import schema from "./schema.mjs";

// Create a Grafserv instance
const serv = grafserv({ schema, preset });

// Create a Deno server with our handler.
Deno.serve({ port: preset.grafserv.port ?? 5678 }, serv.handler);
4 changes: 4 additions & 0 deletions grafast/grafserv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
"types": "./dist/servers/h3/v1/index.d.ts",
"default": "./dist/servers/h3/v1/index.js"
},
"./deno/v1": {
"types": "./dist/servers/deno/v1/index.d.ts",
"default": "./dist/servers/deno/v1/index.js"
},
"./ruru": {
"types": "./fwd/ruru/index.d.ts",
"node": "./fwd/ruru/index.js",
Expand Down
125 changes: 125 additions & 0 deletions grafast/grafserv/src/servers/deno/v1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import {
convertHandlerResultToResult,
GrafservBase,
normalizeRequest,
} from "grafserv";

import type {
GrafservBodyJSON,
GrafservConfig,
RequestDigest,
Result,
} from "grafserv";

declare global {
namespace Grafast {
interface RequestContext {
denov1: {
ctx: Deno.ServeHandlerInfo;
};
}
}
}

/* This block is to trick TypeScript into compiling; really we need the Deno types in Node.js so we can build the package. */
declare global {
const Response: any;
namespace Deno {
export type ServeHandlerInfo = any;
}
type Request = any;
}

function getDigest(req: Request, ctx: Deno.ServeHandlerInfo): RequestDigest {
const url = new URL(req.url);
return {
// TODO: figure out the actual HTTP version
httpVersionMajor: 2,
httpVersionMinor: 0,
isSecure: url.protocol === "https",
method: req.method,
path: url.pathname,
headers: Object.fromEntries(req.headers.entries()),
getQueryParams() {
// TODO: on duplicate keys, this should convert to an array instead
return Object.fromEntries(url.searchParams) as Record<
string,
string | string[]
>;
},
async getBody() {
return {
type: "json",
json: await req.json(),
} as GrafservBodyJSON;
},
requestContext: {
denov1: {
ctx,
},
},
};
}

export class DenoGrafserv extends GrafservBase {
handler = async (req: Request, ctx: Deno.ServeHandlerInfo) => {
const digest = getDigest(req, ctx);

const normalizedRequest = normalizeRequest(digest);

const handlerResult = await this.graphqlHandler(
normalizedRequest,
this.graphiqlHandler,
);
const result = await convertHandlerResultToResult(handlerResult);
return this.send(req, result);
};

public send(_req: Request, result: Result | null) {
if (result === null) return new Response(null, { status: 404 });

switch (result.type) {
case "error": {
console.error(result.error);
return new Response(
JSON.stringify(
Object.assign(result.error, { status: result.statusCode }),
),
{ status: result.statusCode, headers: result.headers },
);
}
case "buffer": {
console.log("ok");
return new Response(result.buffer, {
status: result.statusCode,
headers: result.headers,
});
}
case "json": {
return new Response(JSON.stringify(result.json), {
status: result.statusCode,
headers: result.headers,
});
}
case "noContent": {
return new Response(null, {
status: result.statusCode,
headers: result.headers,
});
}
default: {
const never = result as never;
console.log("Unhandled:");
console.dir(never);
return new Response("Server hasn't implemented this yet", {
status: 503,
headers: { "Content-Type": "text/plain" },
});
}
}
}
}

export function grafserv(config: GrafservConfig) {
return new DenoGrafserv(config);
}
13 changes: 13 additions & 0 deletions grafast/website/grafserv/servers/deno.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Deno

```ts
import { grafserv } from "npm:grafserv/deno/v1";
import preset from "./graphile.config.mjs";
import schema from "./schema.mjs";

// Create a Grafserv instance
const serv = grafserv({ schema, preset });

// Create a Deno server with our handler.
Deno.serve({ port: preset.grafserv.port ?? 5678 }, serv.handler);
```