Skip to content

Commit

Permalink
Implement new split inferred/override behavior system
Browse files Browse the repository at this point in the history
  • Loading branch information
benjie committed Sep 25, 2024
1 parent ec470d5 commit e070614
Show file tree
Hide file tree
Showing 20 changed files with 524 additions and 425 deletions.
74 changes: 38 additions & 36 deletions graphile-build/graphile-build-pg/src/plugins/PgAttributesPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,44 +333,46 @@ export const PgAttributesPlugin: GraphileConfig.Plugin = {

entityBehavior: {
pgCodecAttribute: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, [codec, attributeName]) {
const behaviors = new Set<GraphileBuild.BehaviorString>([
"select",
"base",
"update",
"insert",
"filterBy",
"orderBy",
]);
const attribute = codec.attributes[attributeName];
function walk(codec: PgCodec) {
if (codec.arrayOfCodec) {
behaviors.add("-condition:attribute:filterBy");
behaviors.add(`-attribute:orderBy`);
walk(codec.arrayOfCodec);
} else if (codec.rangeOfCodec) {
behaviors.add(`-condition:attribute:filterBy`);
behaviors.add(`-attribute:orderBy`);
walk(codec.rangeOfCodec);
} else if (codec.domainOfCodec) {
// No need to add a behavior for domain
walk(codec.domainOfCodec);
} else if (codec.attributes) {
behaviors.add(`-condition:attribute:filterBy`);
behaviors.add(`-attribute:orderBy`);
} else if (codec.isBinary) {
// Never filter, not in condition plugin nor any other
behaviors.add(`-attribute:filterBy`);
behaviors.add(`-attribute:orderBy`);
} else {
// Done
inferred: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, [codec, attributeName]) {
const behaviors = new Set<GraphileBuild.BehaviorString>([
"select",
"base",
"update",
"insert",
"filterBy",
"orderBy",
]);
const attribute = codec.attributes[attributeName];
function walk(codec: PgCodec) {
if (codec.arrayOfCodec) {
behaviors.add("-condition:attribute:filterBy");
behaviors.add(`-attribute:orderBy`);
walk(codec.arrayOfCodec);
} else if (codec.rangeOfCodec) {
behaviors.add(`-condition:attribute:filterBy`);
behaviors.add(`-attribute:orderBy`);
walk(codec.rangeOfCodec);
} else if (codec.domainOfCodec) {
// No need to add a behavior for domain
walk(codec.domainOfCodec);
} else if (codec.attributes) {
behaviors.add(`-condition:attribute:filterBy`);
behaviors.add(`-attribute:orderBy`);
} else if (codec.isBinary) {
// Never filter, not in condition plugin nor any other
behaviors.add(`-attribute:filterBy`);
behaviors.add(`-attribute:orderBy`);
} else {
// Done
}
}
}
walk(attribute.codec);
walk(attribute.codec);

return [...behaviors, behavior];
return [...behaviors, behavior];
},
},
},
},
Expand Down
28 changes: 7 additions & 21 deletions graphile-build/graphile-build-pg/src/plugins/PgBasicsPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,12 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = {
},
entityBehavior: {
pgCodec: {
after: ["default", "inferred"],
provides: ["override"],
callback(behavior, codec) {
override(behavior, codec) {
return [behavior, getBehavior(codec.extensions)];
},
},
pgCodecAttribute: {
after: ["default", "inferred"],
provides: ["override"],
callback(behavior, [codec, attributeName]) {
override(behavior, [codec, attributeName]) {
if (typeof attributeName !== "string") {
throw new Error(
`pgCodecAttribute no longer accepts (codec, attribute) - it now accepts (codec, attributeName). Please update your code. Sorry! (Changed in PostGraphile V5 alpha 13.)`,
Expand All @@ -242,19 +238,15 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = {
},
},
pgResource: {
after: ["default", "inferred"],
provides: ["override"],
callback(behavior, resource) {
override(behavior, resource) {
return [
behavior,
getBehavior([resource.codec.extensions, resource.extensions]),
];
},
},
pgResourceUnique: {
after: ["default", "inferred"],
provides: ["override"],
callback(behavior, [resource, unique]) {
override(behavior, [resource, unique]) {
return [
behavior,
getBehavior([
Expand All @@ -266,9 +258,7 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = {
},
},
pgCodecRelation: {
after: ["default", "inferred"],
provides: ["override"],
callback(behavior, relationSpec) {
override(behavior, relationSpec) {
return [
behavior,
// The behavior is the relation behavior PLUS the remote table
Expand All @@ -282,9 +272,7 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = {
},
},
pgCodecRef: {
after: ["default", "inferred"],
provides: ["override"],
callback(behavior, [codec, refName]) {
override(behavior, [codec, refName]) {
const ref = codec.refs?.[refName];
if (!ref) {
throw new Error(`Codec ${codec.name} has no ref '${refName}'`);
Expand All @@ -296,9 +284,7 @@ export const PgBasicsPlugin: GraphileConfig.Plugin = {
},
},
pgRefDefinition: {
after: ["default", "inferred"],
provides: ["override"],
callback(behavior, refSpec) {
override(behavior, refSpec) {
return [behavior, getBehavior(refSpec.extensions)];
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ export const PgConditionArgumentPlugin: GraphileConfig.Plugin = {
entityBehavior: {
pgCodec: ["select", "filter"],
pgResource: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, resource) {
return resource.parameters ? [behavior] : ["filter", behavior];
inferred: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, resource) {
return resource.parameters ? [behavior] : ["filter", behavior];
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,7 @@ export const PgConditionCustomFieldsPlugin: GraphileConfig.Plugin = {
},
entityBehavior: {
pgResource: {
provides: ["inferred"],
after: ["default"],
before: ["override"],
callback(behavior, entity) {
inferred(behavior, entity) {
if (isSimpleScalarComputedColumnLike(entity)) {
return [behavior, "-proc:filterBy"];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ export const PgConnectionArgOrderByPlugin: GraphileConfig.Plugin = {
entityBehavior: {
pgCodec: "order",
pgResource: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, resource) {
if (resource.parameters) {
return behavior;
} else {
return ["order", behavior];
}
inferred: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, resource) {
if (resource.parameters) {
return behavior;
} else {
return ["order", behavior];
}
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,7 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = {
},
entityBehavior: {
pgResource: {
provides: ["inferred"],
after: ["defaults"],
before: ["overrides"],
callback(behavior, entity) {
inferred(behavior, entity) {
if (entity.parameters) {
return [behavior, defaultProcSourceBehavior(entity)];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ export const PgFirstLastBeforeAfterArgsPlugin: GraphileConfig.Plugin = {

schema: {
entityBehavior: {
pgResource: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior) {
return ["resource:connection:backwards", behavior];
},
},
pgResource: "resource:connection:backwards",
},
hooks: {
GraphQLObjectType_fields_field_args: commonFn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,17 @@ export const PgInterfaceModeUnionAllRowsPlugin: GraphileConfig.Plugin = {
schema: {
entityBehavior: {
pgCodec: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, entity) {
if (entity.polymorphism?.mode === "union") {
return ["connection", "-list", behavior];
} else {
return behavior;
}
inferred: {
provides: ["default"],
before: ["inferred"],
callback(behavior, entity) {
if (entity.polymorphism?.mode === "union") {
// TODO: explain why this exists! Also, why is it default and not inferred?
return ["connection", "-list", behavior];
} else {
return behavior;
}
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,25 @@ export const PgMutationCreatePlugin: GraphileConfig.Plugin = {

entityBehavior: {
pgResource: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, resource) {
const newBehavior: GraphileBuild.BehaviorString[] = [
behavior,
"insert:resource:select",
];
if (
!resource.parameters &&
!!resource.codec.attributes &&
!resource.codec.polymorphism &&
!resource.codec.isAnonymous
) {
newBehavior.unshift("insert");
newBehavior.unshift("record");
}
return newBehavior;
inferred: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, resource) {
const newBehavior: GraphileBuild.BehaviorString[] = [
behavior,
"insert:resource:select",
];
if (
!resource.parameters &&
!!resource.codec.attributes &&
!resource.codec.polymorphism &&
!resource.codec.isAnonymous
) {
newBehavior.unshift("insert");
newBehavior.unshift("record");
}
return newBehavior;
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ export const PgOrderCustomFieldsPlugin: GraphileConfig.Plugin = {
},
entityBehavior: {
pgResource: {
provides: ["inferred"],
after: ["default"],
before: ["override"],
callback(behavior, resource) {
inferred(behavior, resource) {
if (isSimpleScalarComputedColumnLike(resource)) {
return [behavior, "-orderBy"];
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,24 +657,23 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = {
schema: {
entityBehavior: {
pgCodec: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, codec) {
return [
"select",
"table",
...((!codec.isAnonymous
? ["insert", "update"]
: []) as GraphileBuild.BehaviorString[]),
behavior,
];
inferred: {
provides: ["default"],
before: ["inferred", "override"],
callback(behavior, codec) {
return [
"select",
"table",
...((!codec.isAnonymous
? ["insert", "update"]
: []) as GraphileBuild.BehaviorString[]),
behavior,
];
},
},
},
pgCodecRelation: {
provides: ["inferred"],
after: ["default", "PgRelationsPlugin"],
before: ["override"],
callback(behavior, entity, build) {
inferred(behavior, entity, build) {
const {
input: {
pgRegistry: { pgRelations },
Expand Down Expand Up @@ -719,10 +718,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = {
},
},
pgCodecAttribute: {
provides: ["inferred"],
after: ["default"],
before: ["override"],
callback(behavior, [codec, attributeName], build) {
inferred(behavior, [codec, attributeName], build) {
// If this is the primary key of a related table of a
// `@interface mode:relational` table, then omit it from the schema
const tbl = build.pgTableResource(codec);
Expand Down Expand Up @@ -758,10 +754,7 @@ export const PgPolymorphismPlugin: GraphileConfig.Plugin = {
},
},
pgResource: {
provides: ["inferred"],
after: ["default"],
before: ["override"],
callback(behavior, resource, build) {
inferred(behavior, resource, build) {
// Disable insert/update/delete on relational tables
const newBehavior = [behavior];
if (
Expand Down
Loading

0 comments on commit e070614

Please sign in to comment.