From a04461824504099a2cf57637c48d2ccdee162247 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Mon, 7 Oct 2024 11:30:26 +0100 Subject: [PATCH 1/4] Only warn once about behavior strings --- graphile-build/graphile-build/src/behavior.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/graphile-build/graphile-build/src/behavior.ts b/graphile-build/graphile-build/src/behavior.ts index 0b479dbe3..997842bd7 100644 --- a/graphile-build/graphile-build/src/behavior.ts +++ b/graphile-build/graphile-build/src/behavior.ts @@ -318,7 +318,7 @@ export class Behavior { filter: TFilter, ): boolean | undefined { if (!this.behaviorRegistry[filter]) { - console.trace( + console.warn( `Behavior '${filter}' is not registered; please be sure to register it within a plugin via \`plugin.schema.behaviorRegistry.add[${JSON.stringify( filter, )}] = { description: "...", entities: [${JSON.stringify( @@ -340,7 +340,7 @@ export class Behavior { entities[entityType] && stringMatches(bhv, filter), ) ) { - console.trace( + console.warn( `Behavior '${filter}' is not registered for entity type '${entityType}'; it's only expected to be used with '${Object.keys( this.behaviorRegistry[filter].entities, ).join( @@ -895,6 +895,8 @@ export function isValidBehaviorString( ); } +const warnedBehaviors: string[] = []; + /* * 1. Take each behavior from inferred * 2. Find the matching behaviors from preferences @@ -963,11 +965,13 @@ function multiplyBehavior( }); } if (final.length === 0) { - console.warn( - `No matches for behavior '${infEntry.scope.join( - ":", - )}' - please ensure that this behavior is registered for entity type '${entityType}'`, - ); + const behavior = infEntry.scope.join(":"); + if (!warnedBehaviors.includes(behavior)) { + warnedBehaviors.push(behavior); + console.warn( + `No matches for behavior '${behavior}' - please ensure that this behavior is registered for entity type '${entityType}'`, + ); + } } return final; }); From a2886af30b55eb98e0a03840503fc2fa9b6d4115 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Mon, 7 Oct 2024 11:49:10 +0100 Subject: [PATCH 2/4] Apply to more entities --- .../src/plugins/PgBasicsPlugin.ts | 35 ++++++++++++++++--- .../src/plugins/PgIndexBehaviorsPlugin.ts | 2 +- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts index 6f22a6fd7..b2cd8c67a 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts @@ -197,7 +197,12 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = { select: { description: "can select this resource/column/etc. Note this does not necessarily mean you can do `select * from users` but it might mean that it's possible to see details about a `users` when it's returned by a function or similar. (In this case the `codec` has `select` but the `resource` has `-select`.)", - entities: ["pgCodec", "pgResource"], + entities: [ + "pgCodec", + "pgCodecAttribute", + "pgCodecRelation", + "pgResource", + ], }, insert: { description: "can insert into this resource/column/etc", @@ -229,15 +234,30 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = { }, list: { description: "should we use a list field for this?", - entities: ["pgCodec", "pgResource", "pgResourceUnique"], + entities: [ + "pgCodec", + "pgCodecRelation", + "pgResource", + "pgResourceUnique", + ], }, "resource:connection": { description: "should we use a connection field for this?", - entities: ["pgCodec", "pgResource", "pgResourceUnique"], + entities: [ + "pgCodec", + "pgCodecRelation", + "pgResource", + "pgResourceUnique", + ], }, "resource:list": { description: "should we use a list field for this?", - entities: ["pgCodec", "pgResource", "pgResourceUnique"], + entities: [ + "pgCodec", + "pgCodecRelation", + "pgResource", + "pgResourceUnique", + ], }, "resource:array": { description: @@ -246,7 +266,12 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = { }, "resource:single": { description: "can we get one of this thing?", - entities: ["pgCodec", "pgResource", "pgResourceUnique"], + entities: [ + "pgCodec", + "pgCodecRelation", + "pgResource", + "pgResourceUnique", + ], }, }, }, diff --git a/graphile-build/graphile-build-pg/src/plugins/PgIndexBehaviorsPlugin.ts b/graphile-build/graphile-build-pg/src/plugins/PgIndexBehaviorsPlugin.ts index 8dce612ec..f37b06255 100644 --- a/graphile-build/graphile-build-pg/src/plugins/PgIndexBehaviorsPlugin.ts +++ b/graphile-build/graphile-build-pg/src/plugins/PgIndexBehaviorsPlugin.ts @@ -96,7 +96,7 @@ export const PgIndexBehaviorsPlugin: GraphileConfig.Plugin = { add: { // HACK: this impacts a community plugin and isn't part of core. manyToMany: { - entities: [], + entities: ["pgResource", "pgCodecRelation"], description: "(Legacy support hack - many-to-many relationship)", }, }, From 55a7d93f2b05156bd1b5f95052d61fda0e02bc6c Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Mon, 7 Oct 2024 11:49:37 +0100 Subject: [PATCH 3/4] Backwards compatibility with old entityBehaviors --- graphile-build/graphile-build/src/behavior.ts | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/graphile-build/graphile-build/src/behavior.ts b/graphile-build/graphile-build/src/behavior.ts index 997842bd7..dd923850f 100644 --- a/graphile-build/graphile-build/src/behavior.ts +++ b/graphile-build/graphile-build/src/behavior.ts @@ -61,11 +61,31 @@ const getEntityBehaviorHooks = ( } else { // noop } - } else { + } else if (rhs.inferred || rhs.override) { const hook = rhs[type]; if (hook) { result[entityType as keyof GraphileBuild.BehaviorEntities] = hook; } + } else { + console.warn( + `Plugin ${ + plugin.name + } is using a deprecated or unsupported form of 'plugin.schema.entityBehavior[${JSON.stringify( + entityType, + )}]' definition - if this is an object it should have only the keys 'inferred' and/or 'override'. This changed in graphile-build@5.0.0-beta.25.`, + ); + const rhsAny = rhs as any; + if (rhsAny.callback) { + if (rhsAny.provides?.includes?.("override")) { + if (type === "override") { + result[entityType as keyof GraphileBuild.BehaviorEntities] = rhsAny; + } + } else { + if (type === "inferred") { + result[entityType as keyof GraphileBuild.BehaviorEntities] = rhsAny; + } + } + } } } return result; From 0b1f7b577114a49b8e3283823845ec6e37484240 Mon Sep 17 00:00:00 2001 From: Benjie Gillam Date: Mon, 7 Oct 2024 11:54:08 +0100 Subject: [PATCH 4/4] docs(changeset): Fix overwhelming logs and errors being output by the new behavior system --- .changeset/olive-moles-sniff.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changeset/olive-moles-sniff.md diff --git a/.changeset/olive-moles-sniff.md b/.changeset/olive-moles-sniff.md new file mode 100644 index 000000000..5e80c326f --- /dev/null +++ b/.changeset/olive-moles-sniff.md @@ -0,0 +1,6 @@ +--- +"graphile-build-pg": patch +"graphile-build": patch +--- + +Fix overwhelming logs and errors being output by the new behavior system