Skip to content

Commit

Permalink
Neater export of resource options (#2056)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie authored May 9, 2024
2 parents e12af43 + 73aecae commit 73a059e
Show file tree
Hide file tree
Showing 66 changed files with 2,757 additions and 2,041 deletions.
7 changes: 7 additions & 0 deletions .changeset/afraid-starfishes-wink.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"graphile-build-pg": patch
"graphile-build": patch
"postgraphile": patch
---

Improve exporting of resource options (neater export code)
13 changes: 6 additions & 7 deletions graphile-build/graphile-build-pg/src/plugins/PgTablesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type {
} from "@dataplan/pg";
import { assertPgClassSingleStep, makePgResourceOptions } from "@dataplan/pg";
import { object } from "grafast";
import { EXPORTABLE, gatherConfig } from "graphile-build";
import {
EXPORTABLE,
EXPORTABLE_OBJECT_CLONE,
gatherConfig,
} from "graphile-build";
import type { PgClass, PgConstraint, PgNamespace } from "pg-introspection";

import { addBehaviorToTags, exportNameHint } from "../utils.js";
Expand Down Expand Up @@ -484,12 +488,7 @@ export const PgTablesPlugin: GraphileConfig.Plugin = {

// Need to mark this exportable to avoid out-of-order access to
// variables in the export
const entries = Object.entries(options);
const finalOptions = EXPORTABLE(
(entries) =>
Object.fromEntries(entries) as unknown as PgResourceOptions,
[entries],
);
const finalOptions = EXPORTABLE_OBJECT_CLONE(options);

const resourceOptions = EXPORTABLE(
(finalOptions, makePgResourceOptions) =>
Expand Down
1 change: 1 addition & 0 deletions graphile-build/graphile-build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"lodash": "^4.17.21",
"pluralize": "^7.0.0",
"semver": "^7.5.4",
"tamedevil": "workspace:^",
"tslib": "^2.6.2"
},
"engines": {
Expand Down
1 change: 1 addition & 0 deletions graphile-build/graphile-build/src/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ declare global {
args: [...TScope],
nameHint?: string,
): T;
EXPORTABLE_OBJECT_CLONE<T extends object>(obj: T): T;
exportNameHint(obj: any, nameHint: string): void;

/**
Expand Down
1 change: 1 addition & 0 deletions graphile-build/graphile-build/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export {
constantCase,
constantCaseAll,
EXPORTABLE,
EXPORTABLE_OBJECT_CLONE,
formatInsideUnderscores,
gatherConfig,
pluralize,
Expand Down
2 changes: 2 additions & 0 deletions graphile-build/graphile-build/src/makeNewBuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import extend, { indent } from "./extend.js";
import type SchemaBuilder from "./SchemaBuilder.js";
import {
EXPORTABLE,
EXPORTABLE_OBJECT_CLONE,
exportNameHint,
stringTypeSpec,
wrapDescription,
Expand Down Expand Up @@ -146,6 +147,7 @@ export default function makeNewBuild(

EXPORTABLE,
exportNameHint,
EXPORTABLE_OBJECT_CLONE,
grafast,
graphql,

Expand Down
26 changes: 26 additions & 0 deletions graphile-build/graphile-build/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GraphQLError, GraphQLObjectType, Kind } from "grafast/graphql";
import camelCaseAll from "lodash/camelCase.js";
import upperFirstAll from "lodash/upperFirst.js";
import plz from "pluralize";
import te from "tamedevil";

export function EXPORTABLE<T, TScope extends any[]>(
factory: (...args: TScope) => T,
Expand All @@ -28,6 +29,31 @@ export function EXPORTABLE<T, TScope extends any[]>(
return fn;
}

export function EXPORTABLE_OBJECT_CLONE<T extends object>(obj: T): T {
if (Object.getPrototypeOf(obj) === Object.prototype) {
const keys = Object.keys(obj);
const values = Object.values(obj);
const fn = te.eval<any>`return (${te.join(
keys.map((_key, i) => te.identifier(`key${i}`)),
", ",
)}) => ({${te.indent(
te.join(
keys.map(
(key, i) =>
te`${te.safeKeyOrThrow(key)}: ${te.identifier(`key${i}`)}`,
),
",\n",
),
)}});`;
// eslint-disable-next-line graphile-export/exhaustive-deps
return EXPORTABLE(fn, values);
} else {
throw new Error(
"EXPORTABLE_OBJECT_CLONE can currently only be used with POJOs.",
);
}
}

export function exportNameHint(obj: any, nameHint: string): void {
if ((typeof obj === "object" && obj != null) || typeof obj === "function") {
if (!("$exporter$name" in obj)) {
Expand Down
Loading

0 comments on commit 73a059e

Please sign in to comment.