@@ -22,7 +22,7 @@ import type { GraphileConfig } from 'graphile-config';
2222import { extendSchema , gql } from 'graphile-utils' ;
2323import { Logger } from '@pgpmjs/logger' ;
2424
25- import type { PresignedUrlPluginOptions } from './types' ;
25+ import type { PresignedUrlPluginOptions , S3Config } from './types' ;
2626import { getStorageModuleConfig , getBucketConfig } from './storage-module-cache' ;
2727import { generatePresignedPutUrl , headObject } from './s3-signer' ;
2828
@@ -67,10 +67,24 @@ async function resolveDatabaseId(pgClient: any): Promise<string | null> {
6767
6868// --- Plugin factory ---
6969
70+ /**
71+ * Resolve the S3 config from the options. If the option is a lazy getter
72+ * function, call it (and cache the result). This avoids reading env vars
73+ * or constructing an S3Client at module-import time.
74+ */
75+ function resolveS3 ( options : PresignedUrlPluginOptions ) : S3Config {
76+ if ( typeof options . s3 === 'function' ) {
77+ const resolved = options . s3 ( ) ;
78+ // Cache so subsequent calls don't re-evaluate
79+ options . s3 = resolved ;
80+ return resolved ;
81+ }
82+ return options . s3 ;
83+ }
84+
7085export function createPresignedUrlPlugin (
7186 options : PresignedUrlPluginOptions ,
7287) : GraphileConfig . Plugin {
73- const { s3 } = options ;
7488
7589 return extendSchema ( ( ) => ( {
7690 typeDefs : gql `
@@ -272,7 +286,7 @@ export function createPresignedUrlPlugin(
272286
273287 // --- Generate presigned PUT URL ---
274288 const uploadUrl = await generatePresignedPutUrl (
275- s3 ,
289+ resolveS3 ( options ) ,
276290 s3Key ,
277291 contentType ,
278292 size ,
@@ -362,7 +376,7 @@ export function createPresignedUrlPlugin(
362376 }
363377
364378 // --- Verify file exists in S3 ---
365- const s3Head = await headObject ( s3 , file . key , file . content_type ) ;
379+ const s3Head = await headObject ( resolveS3 ( options ) , file . key , file . content_type ) ;
366380
367381 if ( ! s3Head ) {
368382 throw new Error ( 'FILE_NOT_IN_S3: the file has not been uploaded yet' ) ;
0 commit comments