Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust some of the behavior overhaul changes #2208

Merged
merged 4 commits into from
Oct 7, 2024
Merged
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
7 changes: 7 additions & 0 deletions .changeset/gold-games-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"graphile-build-pg": patch
"graphile-build": patch
---

Behaviors can now be registered by more than one plugin. Apply behaviors to more
entities. Don't log so much.
29 changes: 26 additions & 3 deletions graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ declare global {
}
}

const FILTER_DEF = {
description: "can we filter this resource/codec",
entities: ["pgCodec", "pgResource"],
} as const;

export const PgBasicsPlugin: GraphileConfig.Plugin = {
name: "PgBasicsPlugin",
description:
Expand Down Expand Up @@ -220,17 +225,35 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = {
description: 'should we add this attribute to the "base" input type?',
entities: [],
},
// filter: { description: "can we filter these results?", entities: [] },
filter: FILTER_DEF,
"query:resource:list:filter": FILTER_DEF,
"query:resource:connection:filter": FILTER_DEF,
"manyRelation:resource:list:filter": FILTER_DEF,
"manyRelation:resource:connection:filter": FILTER_DEF,
"singularRelation:resource:list:filter": FILTER_DEF,
"singularRelation:resource:connection:filter": FILTER_DEF,
"typeField:resource:list:filter": FILTER_DEF,
"typeField:resource:connection:filter": FILTER_DEF,
"queryField:resource:list:filter": FILTER_DEF,
"queryField:resource:connection:filter": FILTER_DEF,
filterBy: { description: "can we filter by this thing?", entities: [] },
order: {
description: "can we order these results?",
entities: ["pgCodec", "pgResource"],
},
orderBy: { description: "can we order by this thing?", entities: [] },
orderBy: {
description: "can we order by this thing?",
entities: ["pgCodecAttribute", "pgResource" /* < computed column */],
},

connection: {
description: "should we use a connection field for this?",
entities: ["pgCodec", "pgResource", "pgResourceUnique"],
entities: [
"pgCodec",
"pgCodecRelation",
"pgResource",
"pgResourceUnique",
],
},
list: {
description: "should we use a list field for this?",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ declare global {
}
}

const FILTER_DEF = {
description: "can we filter this resource/codec",
entities: ["pgCodec", "pgResource"],
} as const;

export const PgConditionArgumentPlugin: GraphileConfig.Plugin = {
name: "PgConditionArgumentPlugin",
description: "Adds the 'condition' argument to connections and lists",
Expand All @@ -64,22 +59,6 @@ export const PgConditionArgumentPlugin: GraphileConfig.Plugin = {
},

schema: {
behaviorRegistry: {
add: {
filter: FILTER_DEF,
"query:resource:list:filter": FILTER_DEF,
"query:resource:connection:filter": FILTER_DEF,
"manyRelation:resource:list:filter": FILTER_DEF,
"manyRelation:resource:connection:filter": FILTER_DEF,
"singularRelation:resource:list:filter": FILTER_DEF,
"singularRelation:resource:connection:filter": FILTER_DEF,
"typeField:resource:list:filter": FILTER_DEF,
"typeField:resource:connection:filter": FILTER_DEF,
"queryField:resource:list:filter": FILTER_DEF,
"queryField:resource:connection:filter": FILTER_DEF,
},
},

entityBehavior: {
pgCodec: ["select", "filter"],
pgResource: {
Expand Down
32 changes: 18 additions & 14 deletions graphile-build/graphile-build/src/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ export class Behavior {
[behavior in keyof GraphileBuild.BehaviorStrings]: {
entities: {
[entity in keyof GraphileBuild.BehaviorEntities]?: {
description: string;
pluginName: string | null;
registeredBy: Array<{ pluginName: string; description: string }>;
};
};
};
Expand Down Expand Up @@ -198,16 +197,20 @@ export class Behavior {
allEntities.add(entityType);
if (!this.behaviorRegistry[behaviorString].entities[entityType]) {
this.behaviorRegistry[behaviorString].entities[entityType] = {
description,
pluginName: plugin.name,
registeredBy: [
{
description,
pluginName: plugin.name,
},
],
};
} else {
console.warn(
`Behavior string '${behaviorString}' for entity type '${entityType}' has been registered by more than one plugin! First registered by ${
this.behaviorRegistry[behaviorString].entities[entityType]!
.pluginName
}; and then later again by ${plugin.name}`,
);
this.behaviorRegistry[behaviorString].entities[
entityType
]!.registeredBy.push({
description,
pluginName: plugin.name,
});
}
}
}
Expand Down Expand Up @@ -338,6 +341,8 @@ export class Behavior {
filter: TFilter,
): boolean | undefined {
if (!this.behaviorRegistry[filter]) {
// DIAGNOSTIC: enable for all filters
/*
console.warn(
`Behavior '${filter}' is not registered; please be sure to register it within a plugin via \`plugin.schema.behaviorRegistry.add[${JSON.stringify(
filter,
Expand All @@ -346,11 +351,11 @@ export class Behavior {
)}] }\`.`,
);
// Register it so we don't see this warning again
*/
this.behaviorRegistry[filter] = {
entities: {
[entityType]: {
description: "Unregistered.",
pluginName: null,
registeredBy: [],
},
},
};
Expand All @@ -373,8 +378,7 @@ export class Behavior {
);
// Register it so we don't see this warning again
this.behaviorRegistry[filter].entities[entityType] = {
description: "Unregistered!",
pluginName: null,
registeredBy: [],
};
}
const finalString = this.getBehaviorForEntity(
Expand Down
Loading