ponder-sh / ponder

A backend framework for crypto apps
https://ponder.sh
MIT License
516 stars 70 forks source link

Custom Server Alternative to GraphQL #514

Open kyscott18 opened 6 months ago

kyscott18 commented 6 months ago

With the completion of #370, it becomes possible for Ponder to support custom load-time logic. This would work in parallel or completely replace GraphQL to serve incoming requests. Ponder could completely internalize a web server, and define its own api for serving requests:

import { app } from "@/generated";

app.get("/players", async ({request: Request, context}): Response => {
  const players = await context.db.Player.findMany({
    where: {
      id: {
        startsWith: "J",
      },
    },
  });

  // Do an rpc request, perform computation on players, etc

  return new Response(players);
});

or support any arbitrary framework, which Ponder would be in charge of running. For example, server/index.ts could be the default entrypoint, which would contain a file like:

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

const app = new Hono();

app.get("/players", async (c) => {
  const players = await context.db.Player.findMany({
    where: {
      id: {
        startsWith: "J",
      },
    },
  });

  // Do an rpc request, perform computation on players, etc

  return c.json(players);
});

serve(app);

The benefits to this solution is supporting maximum flexibility. The environment would not be as sandboxed which would potentially created harder to recover from errors.

Both of these solutions would also allow for queries directly against the db. We need to determine a methodology for supporting a strongly typed ORM.

faces-of-eth commented 6 months ago

I proposed a feature request to disable the GraphQL server entirely, but I also this could work for my use case, which would help merge the indexed Ponder data with some other things I'm keeping track of in the database.