@@ -3,57 +3,59 @@ import type { ConstructiveOptions } from '@constructive-io/graphql-types';
33import { cors , healthz , poweredBy } from '@pgpmjs/server-utils' ;
44import { middleware as parseDomains } from '@constructive-io/url-domains' ;
55import express , { Express , NextFunction , Request , Response } from 'express' ;
6- import { GraphileCache , graphileCache } from 'graphile-cache' ;
7- import graphqlUpload from 'graphql-upload ' ;
8- // Scalar
9- import { getPgPool } from 'pg-cache' ;
6+ import { createGraphileInstance , graphileCache , GraphileCacheEntry } from 'graphile-cache' ;
7+ import { makePgService } from 'graphile-settings ' ;
8+ import type { GraphileConfig } from 'graphile-config' ;
9+ import { buildConnectionString , getPgPool } from 'pg-cache' ;
1010import { getPgEnvOptions } from 'pg-env' ;
11- import { postgraphile } from 'postgraphile' ;
1211
1312import { printDatabases , printSchemas } from './render' ;
14- import { getGraphileSettings } from './settings' ;
13+ import { getGraphilePreset } from './settings' ;
1514
1615export const GraphQLExplorer = ( rawOpts : ConstructiveOptions = { } ) : Express => {
1716 const opts = getEnvOptions ( rawOpts ) ;
1817
1918 const { pg, server } = opts ;
2019
21- const getGraphileInstanceObj = (
20+ const getGraphileInstanceObj = async (
2221 dbname : string ,
2322 schemaname : string
24- ) : GraphileCache => {
23+ ) : Promise < GraphileCacheEntry > => {
2524 const key = `${ dbname } .${ schemaname } ` ;
2625
27- if ( graphileCache . has ( key ) ) {
28- return graphileCache . get ( key ) ;
26+ const cached = graphileCache . get ( key ) ;
27+ if ( cached ) {
28+ return cached ;
2929 }
3030
31- const settings = {
32- ...getGraphileSettings ( {
33- ...opts ,
34- graphile : { schema : schemaname } ,
35- } ) ,
36- graphqlRoute : '/graphql' ,
37- graphiqlRoute : '/graphiql' ,
38- } ;
39-
40- const pgPool = getPgPool (
41- getPgEnvOptions ( {
42- ...opts . pg ,
43- database : dbname ,
44- } )
31+ const pgConfig = getPgEnvOptions ( {
32+ ...pg ,
33+ database : dbname ,
34+ } ) ;
35+ const connectionString = buildConnectionString (
36+ pgConfig . user ,
37+ pgConfig . password ,
38+ pgConfig . host ,
39+ pgConfig . port ,
40+ pgConfig . database
4541 ) ;
4642
47- const handler = postgraphile ( pgPool , schemaname , settings ) ;
48-
49- const obj = {
50- pgPool,
51- pgPoolKey : dbname ,
52- handler,
43+ const basePreset = getGraphilePreset ( opts ) ;
44+ const preset : GraphileConfig . Preset = {
45+ ...basePreset ,
46+ pgServices : [
47+ makePgService ( { connectionString, schemas : [ schemaname ] } ) ,
48+ ] ,
49+ grafserv : {
50+ graphqlPath : '/graphql' ,
51+ graphiqlPath : '/graphiql' ,
52+ graphiql : true ,
53+ } ,
5354 } ;
5455
55- graphileCache . set ( key , obj ) ;
56- return obj ;
56+ const instance = await createGraphileInstance ( { preset, cacheKey : key } ) ;
57+ graphileCache . set ( key , instance ) ;
58+ return instance ;
5759 } ;
5860
5961 const app = express ( ) ;
@@ -62,7 +64,6 @@ export const GraphQLExplorer = (rawOpts: ConstructiveOptions = {}): Express => {
6264 cors ( app , server . origin ) ;
6365 app . use ( parseDomains ( ) ) ;
6466 app . use ( poweredBy ( 'constructive' ) ) ;
65- app . use ( graphqlUpload . graphqlUploadExpress ( ) ) ;
6667
6768 app . use ( async ( req : Request , res : Response , next : NextFunction ) => {
6869 if ( req . urlDomains ?. subdomains . length === 1 ) {
@@ -132,8 +133,8 @@ export const GraphQLExplorer = (rawOpts: ConstructiveOptions = {}): Express => {
132133 if ( req . urlDomains ?. subdomains . length === 2 ) {
133134 const [ schemaName , dbName ] = req . urlDomains . subdomains ;
134135 try {
135- const { handler } = getGraphileInstanceObj ( dbName , schemaName ) ;
136- handler ( req , res , next ) ;
136+ const instance = await getGraphileInstanceObj ( dbName , schemaName ) ;
137+ instance . handler ( req , res , next ) ;
137138 return ;
138139 } catch ( e : any ) {
139140 res . status ( 500 ) . send ( e . message ) ;
0 commit comments