Skip to content

Commit

Permalink
Add more missing behaviors, and fix behavior of connection-incapable …
Browse files Browse the repository at this point in the history
…lists (use 'array' instead)
  • Loading branch information
benjie committed Sep 24, 2024
1 parent d8b9808 commit 19b2001
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ declare global {
queryField: true;
mutationField: true;
typeField: true;
"typeField:resource:connection": true;
"typeField:resource:list": true;
"typeField:resource:array": true;
"queryField:resource:connection": true;
"queryField:resource:list": true;
"queryField:resource:array": true;
"typeField:single": true;
"queryField:single": true;
}
interface Build {
pgGetArgDetailsFromParameters(
Expand Down Expand Up @@ -195,7 +203,7 @@ function shouldUseCustomConnection(
function defaultProcSourceBehavior(
s: PgResource<any, any, any, any, any>,
): GraphileBuild.BehaviorString {
const behavior: GraphileBuild.BehaviorString[] = [];
const behavior: GraphileBuild.BehaviorString[] = ["-array"];
const firstParameter = (
s as PgResource<any, any, any, readonly PgResourceParameter[], any>
).parameters[0];
Expand Down Expand Up @@ -233,7 +241,7 @@ function defaultProcSourceBehavior(
const canUseConnection =
!s.sqlPartitionByIndex && !s.isList && !s.codec.arrayOfCodec;
if (!canUseConnection) {
behavior.push("-connection", "list");
behavior.push("-connection", "-list", "array");
}
}

Expand Down Expand Up @@ -358,6 +366,30 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = {
'a "custom field function" - add it to a specific type (aka "computed column")',
entities: ["pgResource"],
},
"typeField:resource:connection": {
description: "",
entities: ["pgResource"],
},
"typeField:resource:list": {
description: "",
entities: ["pgResource"],
},
"queryField:resource:connection": {
description: "",
entities: ["pgResource"],
},
"queryField:resource:list": {
description: "",
entities: ["pgResource"],
},
"typeField:single": {
description: "",
entities: ["pgResource"],
},
"queryField:single": {
description: "",
entities: ["pgResource"],
},
},
},
entityBehavior: {
Expand Down Expand Up @@ -1154,7 +1186,9 @@ export const PgCustomTypeFieldPlugin: GraphileConfig.Plugin = {

const baseScope = isRootQuery ? `queryField` : `typeField`;
const connectionFieldBehaviorScope = `${baseScope}:resource:connection`;
const listFieldBehaviorScope = `${baseScope}:resource:list`;
const listFieldBehaviorScope = canUseConnection
? `${baseScope}:resource:list`
: `${baseScope}:resource:array`;
if (
canUseConnection &&
build.behavior.pgResourceMatches(
Expand Down
25 changes: 17 additions & 8 deletions graphile-build/graphile-build/src/behavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ export class Behavior {
for (const [behaviorString, spec] of Object.entries(
this.behaviorRegistry,
)) {
if (spec.entities[entityType]) {
if (
spec.entities[entityType] ||
true /* This ` || true` is because of inheritance (e.g. unique inherits from resource inherits from codec); it causes a headache if we factor it in */
) {
const parts = behaviorString.split(":");
const l = parts.length;
for (let i = 0; i < l; i++) {
Expand Down Expand Up @@ -216,7 +219,7 @@ export class Behavior {
// ... then remove everything ...
"-*",
// ... then replace it with what we actually want.
multiplyBehavior(defaultBehavior, behavior),
multiplyBehavior(defaultBehavior, behavior, entityType),
];
},
},
Expand Down Expand Up @@ -487,17 +490,19 @@ export class Behavior {
}
for (const { scope } of result) {
const behavior = scope.join(":") as keyof GraphileBuild.BehaviorStrings;
if (!this.behaviorRegistry[behavior]) {
if (
!Object.keys(this.behaviorRegistry).some((bhv) =>
stringMatches(bhv, behavior),
)
) {
console.trace(
`Behavior '${behavior}' has not been registered! (Source: ${source})`,
);
}

if (
!Object.entries(this.behaviorRegistry).some(
([bhv, { entities }]) =>
(entities[entityType] && bhv === behavior) ||
bhv.endsWith(":" + behavior),
!Object.entries(this.behaviorRegistry).some(([bhv, { entities }]) =>
/*entities[entityType] &&*/ stringMatches(bhv, behavior),
)
) {
console.trace(
Expand Down Expand Up @@ -704,7 +709,11 @@ export function isValidBehaviorString(
* - Result: concatenate these:
* - "-connection -resource:connection +query:resource:connection -list +resource:list -query:resource:list"
*/
function multiplyBehavior(preferences: string, inferred: string) {
function multiplyBehavior(
preferences: string,
inferred: string,
entityType: string,
) {
const pref = parseSpecs(preferences);
const inf = parseSpecs(inferred);
const result = inf.flatMap((infEntry) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import { version } from "../version.js";
declare global {
namespace GraphileBuild {
interface BehaviorStrings {
// 'connection' and 'list' are for connection-capable collections. If
// your collection is not connection-capable, it should use 'array'
// instead.
connection: true;
list: true;
array: true;

single: true;

"interface:node": true;
Expand All @@ -28,7 +33,13 @@ export const CommonBehaviorsPlugin: GraphileConfig.Plugin = {
entities: [],
},
list: {
description: "represent collection as a list (simple collections)",
description:
"represent collection as a list - only use with collections that can be represented as a connection too",
entities: [],
},
array: {
description:
"represent an array as a list - use with collections which are not connection-capable (otherwise use list)",
entities: [],
},
single: {
Expand Down

0 comments on commit 19b2001

Please sign in to comment.