Skip to content

Commit fa0810e

Browse files
committed
Convert postgraphile library usage from V4->V5
1 parent f424e0c commit fa0810e

5 files changed

Lines changed: 297 additions & 249 deletions

File tree

@app/lib/src/GraphileApolloLink.ts

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import {
66
Operation,
77
} from "@apollo/client";
88
import { Request, Response } from "express";
9-
import { execute, getOperationAST } from "graphql";
10-
import { HttpRequestHandler } from "postgraphile";
9+
import { execute, hookArgs, isAsyncIterable } from "grafast";
10+
import type {} from "grafserv/express/v4";
11+
import { getOperationAST } from "graphql";
12+
import type { PostGraphileInstance } from "postgraphile";
1113

1214
export interface GraphileApolloLinkInterface {
1315
/** The request object. */
@@ -17,10 +19,7 @@ export interface GraphileApolloLinkInterface {
1719
res: Response;
1820

1921
/** The instance of the express middleware returned by calling `postgraphile()` */
20-
postgraphileMiddleware: HttpRequestHandler<Request, Response>;
21-
22-
/** An optional rootValue to use inside resolvers. */
23-
rootValue?: any;
22+
pgl: PostGraphileInstance;
2423
}
2524

2625
/**
@@ -36,7 +35,7 @@ export class GraphileApolloLink extends ApolloLink {
3635
operation: Operation,
3736
_forward?: NextLink
3837
): Observable<FetchResult> | null {
39-
const { postgraphileMiddleware, req, res, rootValue } = this.options;
38+
const { pgl, req, res } = this.options;
4039
return new Observable((observer) => {
4140
(async () => {
4241
try {
@@ -53,22 +52,32 @@ export class GraphileApolloLink extends ApolloLink {
5352
}
5453
return;
5554
}
56-
const schema = await postgraphileMiddleware.getGraphQLSchema();
57-
const data =
58-
await postgraphileMiddleware.withPostGraphileContextFromReqRes(
59-
req,
60-
res,
61-
{},
62-
(context) =>
63-
execute(
64-
schema,
65-
document,
66-
rootValue || {},
67-
context,
68-
variableValues,
69-
operationName
70-
)
71-
);
55+
const schema = await pgl.getSchema();
56+
const args = {
57+
schema,
58+
document,
59+
variableValues,
60+
operationName,
61+
};
62+
await hookArgs(
63+
args,
64+
{
65+
node: {
66+
req,
67+
res,
68+
},
69+
expressv4: {
70+
req,
71+
res,
72+
},
73+
},
74+
pgl.getResolvedPreset()
75+
);
76+
const data = await execute(args);
77+
if (isAsyncIterable(data)) {
78+
data.return?.();
79+
throw new Error("Iterable not supported by GraphileApolloLink");
80+
}
7281
if (!observer.closed) {
7382
observer.next(data);
7483
observer.complete();

@app/lib/src/withApollo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ function makeServerSideLink(req: any, res: any) {
8989
return new GraphileApolloLink({
9090
req,
9191
res,
92-
postgraphileMiddleware: req.app.get("postgraphileMiddleware"),
92+
pgl: req.app.get("pgl"),
9393
});
9494
}
9595

@app/server/src/app.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import express, { Express } from "express";
2-
import { IncomingMessage, Server } from "http";
3-
import { Middleware } from "postgraphile";
1+
import express, { Express, NextFunction } from "express";
2+
import { IncomingMessage, Server, ServerResponse } from "http";
43
import { Duplex } from "stream";
54

65
import { cloudflareIps } from "./cloudflare";
@@ -36,6 +35,13 @@ export function getWebsocketMiddlewares(
3635
return app.get("websocketMiddlewares");
3736
}
3837

38+
export interface Middleware<
39+
Request extends IncomingMessage,
40+
Response extends ServerResponse
41+
> {
42+
(req: Request, res: Response, next: NextFunction): void;
43+
}
44+
3945
export async function makeApp({
4046
httpServer,
4147
}: {

0 commit comments

Comments
 (0)