Skip to content

Commit

Permalink
Reduce warnings and improve backwards compatibility of new behavior s…
Browse files Browse the repository at this point in the history
…ystem (#2207)
  • Loading branch information
benjie authored Oct 7, 2024
2 parents 10ed7e8 + 0b1f7b5 commit 5d58edd
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
6 changes: 6 additions & 0 deletions .changeset/olive-moles-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"graphile-build-pg": patch
"graphile-build": patch
---

Fix overwhelming logs and errors being output by the new behavior system
35 changes: 30 additions & 5 deletions graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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:
Expand All @@ -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",
],
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
},
},
Expand Down
40 changes: 32 additions & 8 deletions graphile-build/graphile-build/src/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -318,7 +338,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(
Expand All @@ -340,7 +360,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(
Expand Down Expand Up @@ -895,6 +915,8 @@ export function isValidBehaviorString(
);
}

const warnedBehaviors: string[] = [];

/*
* 1. Take each behavior from inferred
* 2. Find the matching behaviors from preferences
Expand Down Expand Up @@ -963,11 +985,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;
});
Expand Down

0 comments on commit 5d58edd

Please sign in to comment.