From 3ce6fbb9fb1fd0a3de91376850305880a719b667 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Tue, 10 Dec 2019 19:27:38 +0000 Subject: [PATCH] fix(smart-tags): run smart-tags against raw introspection results --- packages/graphile-build-pg/src/index.ts | 1 + .../src/plugins/PgBasicsPlugin.ts | 10 +++ .../src/plugins/PgIntrospectionPlugin.ts | 19 ++-- .../src/makePgSmartTagsPlugin.ts | 88 ++++++++++++------- 4 files changed, 73 insertions(+), 45 deletions(-) diff --git a/packages/graphile-build-pg/src/index.ts b/packages/graphile-build-pg/src/index.ts index 05392049d..4ff852ed5 100644 --- a/packages/graphile-build-pg/src/index.ts +++ b/packages/graphile-build-pg/src/index.ts @@ -132,6 +132,7 @@ export { PgConstraint, PgExtension, PgIndex, + RawIntrospectionResults, PgIntrospectionResultsByKind, PgEntity, PgEntityKind, diff --git a/packages/graphile-build-pg/src/plugins/PgBasicsPlugin.ts b/packages/graphile-build-pg/src/plugins/PgBasicsPlugin.ts index fcc176a09..b4b0490ff 100644 --- a/packages/graphile-build-pg/src/plugins/PgBasicsPlugin.ts +++ b/packages/graphile-build-pg/src/plugins/PgBasicsPlugin.ts @@ -12,6 +12,7 @@ import { PgEntity, SmartTagValue, SmartTags, + RawIntrospectionResults, } from "./PgIntrospectionPlugin"; import pgField from "./pgField"; @@ -85,6 +86,10 @@ declare module "graphile-build" { pgIgnoreIndexes?: boolean; pgHideIndexWarnings?: boolean; pgLegacyJsonUuid?: boolean; + + pgAugmentIntrospectionResults?: ( + introspectionResult: RawIntrospectionResults + ) => RawIntrospectionResults; } interface Build { @@ -102,6 +107,9 @@ declare module "graphile-build" { pgField: typeof pgField; sqlCommentByAddingTags: typeof sqlCommentByAddingTags; pgPrepareAndRun: typeof pgPrepareAndRun; + pgAugmentIntrospectionResults?: ( + introspectionResult: RawIntrospectionResults + ) => RawIntrospectionResults; } interface Inflection { @@ -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; @@ -1071,6 +1080,7 @@ export default (function PgBasicsPlugin( pgField, sqlCommentByAddingTags, pgPrepareAndRun, + pgAugmentIntrospectionResults, }; return build.extend( build, diff --git a/packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.ts b/packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.ts index a70d0211c..7240bba46 100644 --- a/packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.ts +++ b/packages/graphile-build-pg/src/plugins/PgIntrospectionPlugin.ts @@ -19,8 +19,6 @@ 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; @@ -28,9 +26,6 @@ declare module "graphile-build" { pgIncludeExtensionResources?: boolean; pgLegacyFunctionsOnly?: boolean; pgSkipInstallingWatchFixtures?: boolean; - pgAugmentIntrospectionResults?: ( - introspectionResult: RawishIntrospectionResults - ) => RawishIntrospectionResults; pgOwnerConnectionString?: string; } @@ -311,7 +306,7 @@ function parseConstraintSpec(rawSpec: string) { } function smartCommentConstraints( - introspectionResults: RawishIntrospectionResults + introspectionResults: RawIntrospectionResults ) { const attributesByNames = ( tbl: PgClass, @@ -525,7 +520,7 @@ function smartCommentConstraints( }); } -type RawishIntrospectionResults = Pick< +export type RawIntrospectionResults = Pick< PgIntrospectionResultsByKind, | "__pgVersion" | "namespace" @@ -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 ); @@ -582,10 +577,10 @@ export default (async function PgIntrospectionPlugin( const rawishIntrospectionResultsByKind = cloneResults( await persistentMemoizeWithKey( cacheKey, - (): Promise => + (): Promise => withPgClient( pgConfig, - async (pgClient): Promise => { + async (pgClient): Promise => { const versionResult = await pgClient.query( "show server_version_num;" ); @@ -607,7 +602,7 @@ export default (async function PgIntrospectionPlugin( pgIncludeExtensionResources, ]); - const result: RawishIntrospectionResults = { + const result: RawIntrospectionResults = { __pgVersion: serverVersionNum, namespace: [], class: [], diff --git a/packages/graphile-utils/src/makePgSmartTagsPlugin.ts b/packages/graphile-utils/src/makePgSmartTagsPlugin.ts index 250e15a53..e9d8f1852 100644 --- a/packages/graphile-utils/src/makePgSmartTagsPlugin.ts +++ b/packages/graphile-utils/src/makePgSmartTagsPlugin.ts @@ -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"; @@ -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; }