Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/graphile-build-pg/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export {
PgConstraint,
PgExtension,
PgIndex,
RawIntrospectionResults,
PgIntrospectionResultsByKind,
PgEntity,
PgEntityKind,
Expand Down
10 changes: 10 additions & 0 deletions packages/graphile-build-pg/src/plugins/PgBasicsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
PgEntity,
SmartTagValue,
SmartTags,
RawIntrospectionResults,
} from "./PgIntrospectionPlugin";
import pgField from "./pgField";

Expand Down Expand Up @@ -85,6 +86,10 @@ declare module "graphile-build" {
pgIgnoreIndexes?: boolean;
pgHideIndexWarnings?: boolean;
pgLegacyJsonUuid?: boolean;

pgAugmentIntrospectionResults?: (
introspectionResult: RawIntrospectionResults
) => RawIntrospectionResults;
}

interface Build {
Expand All @@ -102,6 +107,9 @@ declare module "graphile-build" {
pgField: typeof pgField;
sqlCommentByAddingTags: typeof sqlCommentByAddingTags;
pgPrepareAndRun: typeof pgPrepareAndRun;
pgAugmentIntrospectionResults?: (
introspectionResult: RawIntrospectionResults
) => RawIntrospectionResults;
}

interface Inflection {
Expand Down Expand Up @@ -1038,6 +1046,7 @@ export default (function PgBasicsPlugin(
pgIgnoreIndexes = true, // TODO:v5: change this to false
pgHideIndexWarnings = false,
pgLegacyJsonUuid = false, // TODO:v5: remove this
pgAugmentIntrospectionResults,
}
) {
let pgOmit = baseOmit;
Expand Down Expand Up @@ -1071,6 +1080,7 @@ export default (function PgBasicsPlugin(
pgField,
sqlCommentByAddingTags,
pgPrepareAndRun,
pgAugmentIntrospectionResults,
};
return build.extend(
build,
Expand Down
19 changes: 7 additions & 12 deletions packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,13 @@ import queryFromResolveDataFactory from "../queryFromResolveDataFactory";
const debug = debugFactory("graphile-build-pg");
const WATCH_FIXTURES_PATH = `${__dirname}/../../res/watch-fixtures.sql`;

// TODO: rename RawishIntrospectionResults

declare module "graphile-build" {
interface GraphileBuildOptions {
pgEnableTags?: boolean;
pgThrowOnMissingSchema?: boolean;
pgIncludeExtensionResources?: boolean;
pgLegacyFunctionsOnly?: boolean;
pgSkipInstallingWatchFixtures?: boolean;
pgAugmentIntrospectionResults?: (
introspectionResult: RawishIntrospectionResults
) => RawishIntrospectionResults;
pgOwnerConnectionString?: string;
}

Expand Down Expand Up @@ -311,7 +306,7 @@ function parseConstraintSpec(rawSpec: string) {
}

function smartCommentConstraints(
introspectionResults: RawishIntrospectionResults
introspectionResults: RawIntrospectionResults
) {
const attributesByNames = (
tbl: PgClass,
Expand Down Expand Up @@ -525,7 +520,7 @@ function smartCommentConstraints(
});
}

type RawishIntrospectionResults = Pick<
export type RawIntrospectionResults = Pick<
PgIntrospectionResultsByKind,
| "__pgVersion"
| "namespace"
Expand All @@ -549,11 +544,11 @@ export default (async function PgIntrospectionPlugin(
pgIncludeExtensionResources = false,
pgLegacyFunctionsOnly = false,
pgSkipInstallingWatchFixtures = false,
pgAugmentIntrospectionResults,
pgOwnerConnectionString,
pgAugmentIntrospectionResults,
}
) {
const augment = (introspectionResults: RawishIntrospectionResults) => {
const augment = (introspectionResults: RawIntrospectionResults) => {
[pgAugmentIntrospectionResults, smartCommentConstraints].forEach(fn =>
fn ? fn(introspectionResults) : null
);
Expand Down Expand Up @@ -582,10 +577,10 @@ export default (async function PgIntrospectionPlugin(
const rawishIntrospectionResultsByKind = cloneResults(
await persistentMemoizeWithKey(
cacheKey,
(): Promise<RawishIntrospectionResults> =>
(): Promise<RawIntrospectionResults> =>
withPgClient(
pgConfig,
async (pgClient): Promise<RawishIntrospectionResults> => {
async (pgClient): Promise<RawIntrospectionResults> => {
const versionResult = await pgClient.query(
"show server_version_num;"
);
Expand All @@ -607,7 +602,7 @@ export default (async function PgIntrospectionPlugin(
pgIncludeExtensionResources,
]);

const result: RawishIntrospectionResults = {
const result: RawIntrospectionResults = {
__pgVersion: serverVersionNum,
namespace: [],
class: [],
Expand Down
88 changes: 55 additions & 33 deletions packages/graphile-utils/src/makePgSmartTagsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Build, Plugin } from "graphile-build";
import { PgEntityKind, PgEntity } from "graphile-build-pg";
import { Plugin } from "graphile-build";
import {
PgEntityKind,
PgEntity,
RawIntrospectionResults,
} from "graphile-build-pg";
import { inspect } from "util";
import { entityIsIdentifiedBy } from "./introspectionHelpers";

Expand Down Expand Up @@ -116,39 +120,57 @@ export function makePgSmartTagsPlugin(
);
}

builder.hook("build", (build: Build) => {
const { pgIntrospectionResultsByKind } = build;
rules.forEach((rule, idx) => {
const relevantIntrospectionResults: PgEntity[] =
pgIntrospectionResultsByKind[rule.kind];

let hits = 0;
relevantIntrospectionResults.forEach(entity => {
if (!rule.match(entity)) {
return;
}
hits++;
if (rule.tags) {
// Overwrite relevant tags
Object.assign(entity.tags, rule.tags);
}
if (rule.description != null) {
// Overwrite comment if specified
entity.description = rule.description;
builder.hook(
"build",
build => {
const oldPgAugmentIntrospectionResults =
build.pgAugmentIntrospectionResults;
build.pgAugmentIntrospectionResults = (
inIntrospectionResult: RawIntrospectionResults
): RawIntrospectionResults => {
let introspectionResult = inIntrospectionResult;
if (oldPgAugmentIntrospectionResults) {
introspectionResult = oldPgAugmentIntrospectionResults(
introspectionResult
);
}
});
rules.forEach((rule, idx) => {
const relevantIntrospectionResults: PgEntity[] =
introspectionResult[rule.kind];

// Let people know if their rules don't match; it's probably a mistake.
if (hits === 0) {
console.warn(
`WARNING: there were no matches for makePgSmartTagsPlugin rule ${idx} - ${inspect(
rawRules[idx]
)}`
);
}
});
return build;
});
let hits = 0;
relevantIntrospectionResults.forEach(entity => {
if (!rule.match(entity)) {
return;
}
hits++;
if (rule.tags) {
// Overwrite relevant tags
Object.assign(entity.tags, rule.tags);
}
if (rule.description != null) {
// Overwrite comment if specified
entity.description = rule.description;
}
});

// Let people know if their rules don't match; it's probably a mistake.
if (hits === 0) {
console.warn(
`WARNING: there were no matches for makePgSmartTagsPlugin rule ${idx} - ${inspect(
rawRules[idx]
)}`
);
}
});
return introspectionResult;
};
return build;
},
[],
["PgIntrospection"],
["PgBasics"]
);
};
return plugin;
}
Expand Down