From 582759e65cea45eeb94ecebf1c0f3c11225edabb Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Wed, 27 Mar 2024 08:04:05 +0000 Subject: [PATCH] Remove invalid default for PgServiceConfiguration generic and address some cascading issues --- grafast/dataplan-pg/src/adaptors/pg.ts | 15 +++++++++------ grafast/dataplan-pg/src/index.ts | 9 ++++----- grafast/dataplan-pg/src/interfaces.ts | 4 ++-- grafast/dataplan-pg/src/pgServices.ts | 16 ++++++++-------- .../src/examples/benjies-test-script.ts | 11 ++++------- .../graphile-build-pg/src/examples/config.ts | 11 ++++------- .../graphile-build-pg/src/examples/webpack.ts | 11 ++++------- .../src/plugins/PgIntrospectionPlugin.ts | 8 +++++--- 8 files changed, 40 insertions(+), 45 deletions(-) diff --git a/grafast/dataplan-pg/src/adaptors/pg.ts b/grafast/dataplan-pg/src/adaptors/pg.ts index 37da7f3bc7..c5669b9da8 100644 --- a/grafast/dataplan-pg/src/adaptors/pg.ts +++ b/grafast/dataplan-pg/src/adaptors/pg.ts @@ -354,7 +354,7 @@ export function makeWithPgClientViaPgClientAlreadyInTransaction( return withPgClient; } -export interface PgAdaptorOptions { +export interface PgAdaptorSettings { /** ONLY FOR USE IN TESTS! */ poolClient?: pg.PoolClient; /** ONLY FOR USE IN TESTS! */ @@ -374,8 +374,11 @@ export interface PgAdaptorOptions { superuserConnectionString?: string; } +/** @deprecated Use PgAdaptorSettings instead. */ +export type PgAdaptorOptions = PgAdaptorSettings; + export function createWithPgClient( - options: PgAdaptorOptions = Object.create(null), + options: PgAdaptorSettings = Object.create(null), variant?: "SUPERUSER" | string | null, ): WithPgClient { if (variant === "SUPERUSER") { @@ -414,7 +417,7 @@ export function createWithPgClient( } // This is here as a TypeScript assertion, to ensure we conform to PgAdaptor -const _testValidAdaptor: PgAdaptor["createWithPgClient"] = +const _testValidAdaptor: PgAdaptor["createWithPgClient"] = createWithPgClient; const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); @@ -715,8 +718,8 @@ declare global { } export function makePgService( - options: MakePgServiceOptions & { pool?: pg.Pool }, -): GraphileConfig.PgServiceConfiguration { + options: MakePgServiceOptions & { pool?: pg.Pool }, +): GraphileConfig.PgServiceConfiguration { const { name = "main", connectionString, @@ -757,7 +760,7 @@ export function makePgService( pgSubscriber = new PgSubscriber(pool); releasers.push(() => pgSubscriber!.release?.()); } - const service: GraphileConfig.PgServiceConfiguration = { + const service: GraphileConfig.PgServiceConfiguration = { name, schemas: Array.isArray(schemas) ? schemas : [schemas ?? "public"], withPgClientKey: withPgClientKey as any, diff --git a/grafast/dataplan-pg/src/index.ts b/grafast/dataplan-pg/src/index.ts index 7081dbd614..898e26880a 100644 --- a/grafast/dataplan-pg/src/index.ts +++ b/grafast/dataplan-pg/src/index.ts @@ -1,7 +1,6 @@ import type { GrafastSubscriber } from "grafast"; import { exportAsMany } from "grafast"; -import type { PgAdaptorOptions } from "./adaptors/pg.js"; import { domainOfCodec, enumCodec, @@ -427,12 +426,12 @@ export { version } from "./version.js"; declare global { namespace GraphileConfig { - interface PgServiceConfiguration { + interface PgServiceConfiguration { name: string; schemas?: string[]; - adaptor: PgAdaptor; - adaptorSettings?: TAdaptorOptions; + adaptor: PgAdaptor; + adaptorSettings?: TAdaptorSettings; /** The key on 'context' where the withPgClient function will be sourced */ withPgClientKey: KeysOfType; @@ -468,7 +467,7 @@ declare global { } interface Preset { - pgServices?: ReadonlyArray; + pgServices?: ReadonlyArray>; } } namespace DataplanPg { diff --git a/grafast/dataplan-pg/src/interfaces.ts b/grafast/dataplan-pg/src/interfaces.ts index 3ef31ec6e3..7dd3a361d5 100644 --- a/grafast/dataplan-pg/src/interfaces.ts +++ b/grafast/dataplan-pg/src/interfaces.ts @@ -415,10 +415,10 @@ export type KeysOfType = { [key in keyof TObject]: TObject[key] extends TValueType ? key : never; }[keyof TObject]; -export interface MakePgServiceOptions +export interface MakePgServiceOptions extends Partial< Pick< - GraphileConfig.PgServiceConfiguration, + GraphileConfig.PgServiceConfiguration, | "name" | "pgSettings" | "pgSettingsForIntrospection" diff --git a/grafast/dataplan-pg/src/pgServices.ts b/grafast/dataplan-pg/src/pgServices.ts index 96c2fcea97..9122f83956 100644 --- a/grafast/dataplan-pg/src/pgServices.ts +++ b/grafast/dataplan-pg/src/pgServices.ts @@ -12,8 +12,8 @@ export interface PgAdaptor { variant?: "SUPERUSER" | null, ) => PromiseOrDirect; makePgService: ( - options: MakePgServiceOptions & { pool?: pg.Pool }, - ) => GraphileConfig.PgServiceConfiguration; + options: MakePgServiceOptions & { pool?: pg.Pool }, + ) => GraphileConfig.PgServiceConfiguration; } /** @@ -41,8 +41,8 @@ const withPgClientDetailsByConfigCache = new Map< * Get or build the 'withPgClient' callback function for a given database * config, caching it to make future lookups faster. */ -export function getWithPgClientFromPgService( - config: GraphileConfig.PgServiceConfiguration, +export function getWithPgClientFromPgService( + config: GraphileConfig.PgServiceConfiguration, ): PromiseOrDirect { const existing = withPgClientDetailsByConfigCache.get(config); if (existing) { @@ -103,8 +103,8 @@ export function getWithPgClientFromPgService( } } -export async function withPgClientFromPgService( - config: GraphileConfig.PgServiceConfiguration, +export async function withPgClientFromPgService( + config: GraphileConfig.PgServiceConfiguration, pgSettings: { [key: string]: string } | null, callback: (client: PgClient) => T | Promise, ): Promise { @@ -120,8 +120,8 @@ export async function withPgClientFromPgService( } // We don't cache superuser withPgClients -export async function withSuperuserPgClientFromPgService( - config: GraphileConfig.PgServiceConfiguration, +export async function withSuperuserPgClientFromPgService( + config: GraphileConfig.PgServiceConfiguration, pgSettings: { [key: string]: string } | null, callback: (client: PgClient) => T | Promise, ): Promise { diff --git a/graphile-build/graphile-build-pg/src/examples/benjies-test-script.ts b/graphile-build/graphile-build-pg/src/examples/benjies-test-script.ts index c7c0e885e5..225b792599 100644 --- a/graphile-build/graphile-build-pg/src/examples/benjies-test-script.ts +++ b/graphile-build/graphile-build-pg/src/examples/benjies-test-script.ts @@ -13,7 +13,7 @@ import { pathToFileURL } from "node:url"; import { inspect } from "node:util"; import { getWithPgClientFromPgService } from "@dataplan/pg"; -import { createWithPgClient, makePgService } from "@dataplan/pg/adaptors/pg"; +import { makePgService } from "@dataplan/pg/adaptors/pg"; import { envelop, useExtendContext, useSchema } from "@envelop/core"; import { useParserCache } from "@envelop/parser-cache"; import { useValidationCache } from "@envelop/validation-cache"; @@ -57,16 +57,13 @@ pool.on("error", (e) => { extends: [graphileBuildPreset, graphileBuildPgPreset], plugins: [QueryQueryPlugin, SwallowErrorsPlugin], pgServices: [ - { + makePgService({ name: "main", schemas: ["a", "b", "c"], pgSettingsKey: "pgSettings", withPgClientKey: "withPgClient", - adaptor: { createWithPgClient, makePgService }, - adaptorSettings: { - pool, - }, - }, + pool, + }), ], gather: { pgJwtTypes: "b.jwt_token", diff --git a/graphile-build/graphile-build-pg/src/examples/config.ts b/graphile-build/graphile-build-pg/src/examples/config.ts index e9112f85a0..10a3872a92 100644 --- a/graphile-build/graphile-build-pg/src/examples/config.ts +++ b/graphile-build/graphile-build-pg/src/examples/config.ts @@ -7,7 +7,7 @@ import "graphile-config"; import { getWithPgClientFromPgService } from "@dataplan/pg"; -import { createWithPgClient, makePgService } from "@dataplan/pg/adaptors/pg"; +import { makePgService } from "@dataplan/pg/adaptors/pg"; import { defaultPreset as graphileBuildPreset, QueryQueryPlugin, @@ -61,16 +61,13 @@ export async function makeSharedPresetAndClient(pool: Pool) { extends: [graphileBuildPreset, graphileBuildPgPreset], plugins: [QueryQueryPlugin, SwallowErrorsPlugin, EnumManglingPlugin], pgServices: [ - { + makePgService({ name: "main", schemas: DATABASE_SCHEMAS, pgSettingsKey: "pgSettings", withPgClientKey: "withPgClient", - adaptor: { createWithPgClient, makePgService }, - adaptorSettings: { - pool, - }, - }, + pool, + }), ], gather: { // pgJwtTypes: "jwt_token", diff --git a/graphile-build/graphile-build-pg/src/examples/webpack.ts b/graphile-build/graphile-build-pg/src/examples/webpack.ts index bda60e9335..12db748e32 100644 --- a/graphile-build/graphile-build-pg/src/examples/webpack.ts +++ b/graphile-build/graphile-build-pg/src/examples/webpack.ts @@ -1,6 +1,6 @@ /* eslint-disable no-restricted-syntax */ -import { createWithPgClient, makePgService } from "@dataplan/pg/adaptors/pg"; +import { makePgService } from "@dataplan/pg/adaptors/pg"; import { buildInflection, buildSchema, @@ -40,16 +40,13 @@ async function main() { plugins: [QueryQueryPlugin, SwallowErrorsPlugin], pgServices: [ // Configuration of our main (and only) Postgres database - { + makePgService({ name: "main", schemas: ["public"], pgSettingsKey: "pgSettings", withPgClientKey: "withPgClient", - adaptor: { createWithPgClient, makePgService }, - adaptorSettings: { - pool, - }, - }, + pool, + }), ], gather: {}, schema: { diff --git a/graphile-build/graphile-build-pg/src/plugins/PgIntrospectionPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgIntrospectionPlugin.ts index e1faa14f82..77fe1dcaeb 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgIntrospectionPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgIntrospectionPlugin.ts @@ -66,7 +66,7 @@ declare global { getIntrospection(): PromiseOrDirect; getService(serviceName: string): Promise<{ introspection: Introspection; - pgService: GraphileConfig.PgServiceConfiguration; + pgService: GraphileConfig.PgServiceConfiguration; }>; getExecutorForService(serviceName: string): PgExecutor; @@ -229,7 +229,7 @@ declare global { } type IntrospectionResults = Array<{ - pgService: GraphileConfig.PgServiceConfiguration; + pgService: GraphileConfig.PgServiceConfiguration; introspection: Introspection; }>; @@ -697,7 +697,9 @@ export const PgIntrospectionPlugin: GraphileConfig.Plugin = { }; function introspectPgServices( - pgServices: ReadonlyArray | undefined, + pgServices: + | ReadonlyArray> + | undefined, ): Promise { if (!pgServices) { return Promise.resolve([]);