@@ -6,8 +6,10 @@ import {
66 Operation ,
77} from "@apollo/client" ;
88import { 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
1214export 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 ( ) ;
0 commit comments