Skip to content

Commit

Permalink
Remove invalid default for PgServiceConfiguration generic and address…
Browse files Browse the repository at this point in the history
… some cascading issues
  • Loading branch information
benjie authored and hannesj committed Mar 27, 2024
1 parent c16e519 commit 582759e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 45 deletions.
15 changes: 9 additions & 6 deletions grafast/dataplan-pg/src/adaptors/pg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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! */
Expand All @@ -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<NodePostgresPgClient> {
if (variant === "SUPERUSER") {
Expand Down Expand Up @@ -414,7 +417,7 @@ export function createWithPgClient(
}

// This is here as a TypeScript assertion, to ensure we conform to PgAdaptor
const _testValidAdaptor: PgAdaptor<PgAdaptorOptions>["createWithPgClient"] =
const _testValidAdaptor: PgAdaptor<PgAdaptorSettings>["createWithPgClient"] =
createWithPgClient;

const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));
Expand Down Expand Up @@ -715,8 +718,8 @@ declare global {
}

export function makePgService(
options: MakePgServiceOptions & { pool?: pg.Pool },
): GraphileConfig.PgServiceConfiguration {
options: MakePgServiceOptions<PgAdaptorSettings> & { pool?: pg.Pool },
): GraphileConfig.PgServiceConfiguration<PgAdaptorSettings> {
const {
name = "main",
connectionString,
Expand Down Expand Up @@ -757,7 +760,7 @@ export function makePgService(
pgSubscriber = new PgSubscriber(pool);
releasers.push(() => pgSubscriber!.release?.());
}
const service: GraphileConfig.PgServiceConfiguration = {
const service: GraphileConfig.PgServiceConfiguration<PgAdaptorSettings> = {
name,
schemas: Array.isArray(schemas) ? schemas : [schemas ?? "public"],
withPgClientKey: withPgClientKey as any,
Expand Down
9 changes: 4 additions & 5 deletions grafast/dataplan-pg/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { GrafastSubscriber } from "grafast";
import { exportAsMany } from "grafast";

import type { PgAdaptorOptions } from "./adaptors/pg.js";
import {
domainOfCodec,
enumCodec,
Expand Down Expand Up @@ -427,12 +426,12 @@ export { version } from "./version.js";

declare global {
namespace GraphileConfig {
interface PgServiceConfiguration<TAdaptorOptions = PgAdaptorOptions> {
interface PgServiceConfiguration<TAdaptorSettings> {
name: string;
schemas?: string[];

adaptor: PgAdaptor<TAdaptorOptions>;
adaptorSettings?: TAdaptorOptions;
adaptor: PgAdaptor<TAdaptorSettings>;
adaptorSettings?: TAdaptorSettings;

/** The key on 'context' where the withPgClient function will be sourced */
withPgClientKey: KeysOfType<Grafast.Context & object, WithPgClient>;
Expand Down Expand Up @@ -468,7 +467,7 @@ declare global {
}

interface Preset {
pgServices?: ReadonlyArray<PgServiceConfiguration>;
pgServices?: ReadonlyArray<PgServiceConfiguration<unknown>>;
}
}
namespace DataplanPg {
Expand Down
4 changes: 2 additions & 2 deletions grafast/dataplan-pg/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,10 +415,10 @@ export type KeysOfType<TObject, TValueType> = {
[key in keyof TObject]: TObject[key] extends TValueType ? key : never;
}[keyof TObject];

export interface MakePgServiceOptions
export interface MakePgServiceOptions<TAdaptorSettings>
extends Partial<
Pick<
GraphileConfig.PgServiceConfiguration,
GraphileConfig.PgServiceConfiguration<TAdaptorSettings>,
| "name"
| "pgSettings"
| "pgSettingsForIntrospection"
Expand Down
16 changes: 8 additions & 8 deletions grafast/dataplan-pg/src/pgServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export interface PgAdaptor<TAdaptorSettings> {
variant?: "SUPERUSER" | null,
) => PromiseOrDirect<WithPgClient>;
makePgService: (
options: MakePgServiceOptions & { pool?: pg.Pool },
) => GraphileConfig.PgServiceConfiguration;
options: MakePgServiceOptions<TAdaptorSettings> & { pool?: pg.Pool },
) => GraphileConfig.PgServiceConfiguration<TAdaptorSettings>;
}

/**
Expand Down Expand Up @@ -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<TAdaptorOptions>(
config: GraphileConfig.PgServiceConfiguration<TAdaptorOptions>,
): PromiseOrDirect<WithPgClient> {
const existing = withPgClientDetailsByConfigCache.get(config);
if (existing) {
Expand Down Expand Up @@ -103,8 +103,8 @@ export function getWithPgClientFromPgService(
}
}

export async function withPgClientFromPgService<T>(
config: GraphileConfig.PgServiceConfiguration,
export async function withPgClientFromPgService<T, TAdaptorSettings>(
config: GraphileConfig.PgServiceConfiguration<TAdaptorSettings>,
pgSettings: { [key: string]: string } | null,
callback: (client: PgClient) => T | Promise<T>,
): Promise<T> {
Expand All @@ -120,8 +120,8 @@ export async function withPgClientFromPgService<T>(
}

// We don't cache superuser withPgClients
export async function withSuperuserPgClientFromPgService<T>(
config: GraphileConfig.PgServiceConfiguration,
export async function withSuperuserPgClientFromPgService<T, TAdaptorSettings>(
config: GraphileConfig.PgServiceConfiguration<TAdaptorSettings>,
pgSettings: { [key: string]: string } | null,
callback: (client: PgClient) => T | Promise<T>,
): Promise<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 4 additions & 7 deletions graphile-build/graphile-build-pg/src/examples/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 4 additions & 7 deletions graphile-build/graphile-build-pg/src/examples/webpack.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ declare global {
getIntrospection(): PromiseOrDirect<IntrospectionResults>;
getService(serviceName: string): Promise<{
introspection: Introspection;
pgService: GraphileConfig.PgServiceConfiguration;
pgService: GraphileConfig.PgServiceConfiguration<unknown>;
}>;
getExecutorForService(serviceName: string): PgExecutor;

Expand Down Expand Up @@ -229,7 +229,7 @@ declare global {
}

type IntrospectionResults = Array<{
pgService: GraphileConfig.PgServiceConfiguration;
pgService: GraphileConfig.PgServiceConfiguration<unknown>;
introspection: Introspection;
}>;

Expand Down Expand Up @@ -697,7 +697,9 @@ export const PgIntrospectionPlugin: GraphileConfig.Plugin = {
};

function introspectPgServices(
pgServices: ReadonlyArray<GraphileConfig.PgServiceConfiguration> | undefined,
pgServices:
| ReadonlyArray<GraphileConfig.PgServiceConfiguration<unknown>>
| undefined,
): Promise<IntrospectionResults> {
if (!pgServices) {
return Promise.resolve([]);
Expand Down

0 comments on commit 582759e

Please sign in to comment.