@graphile-contrib/pg-many-to-many
Advanced tools
Comparing version 2.0.0-1.1 to 2.0.0-alpha.0
@@ -6,3 +6,2 @@ "use strict"; | ||
} | ||
const defaultBehavior = "manyToMany select"; | ||
// Given a `leftTable`, trace through the foreign key relations | ||
@@ -13,10 +12,8 @@ // and identify a `junctionTable` and `rightTable`. | ||
return Object.entries(leftTable.getRelations()).reduce((memoLeft, [leftRelationName, junctionLeftRelation]) => { | ||
const relationBehavior = build.pgGetBehavior(junctionLeftRelation.extensions); | ||
if (!build.behavior.matches(relationBehavior, "manyToMany", defaultBehavior)) { | ||
if (!build.behavior.pgCodecRelationMatches(junctionLeftRelation, "manyToMany")) { | ||
return memoLeft; | ||
} | ||
const junctionTable = junctionLeftRelation.remoteResource; | ||
const junctionBehavior = build.pgGetBehavior(junctionLeftRelation.extensions); | ||
if (!build.behavior.matches(junctionBehavior, "manyToMany", defaultBehavior) || | ||
!build.behavior.matches(junctionBehavior, "select", defaultBehavior)) { | ||
if (!build.behavior.pgCodecRelationMatches(junctionLeftRelation, "manyToMany") || | ||
!build.behavior.pgCodecRelationMatches(junctionLeftRelation, "select")) { | ||
return memoLeft; | ||
@@ -33,4 +30,3 @@ } | ||
} | ||
const otherRelationBehavior = build.pgGetBehavior(rel.extensions); | ||
if (!build.behavior.matches(otherRelationBehavior, "manyToMany", defaultBehavior)) { | ||
if (!build.behavior.pgCodecRelationMatches(rel, "manyToMany")) { | ||
return false; | ||
@@ -42,5 +38,4 @@ } | ||
const rightTable = junctionRightRelation.remoteResource; | ||
const rightTableBehavior = build.pgGetBehavior(rightTable.extensions); | ||
if (!build.behavior.matches(rightTableBehavior, "manyToMany", defaultBehavior) || | ||
!build.behavior.matches(rightTableBehavior, "select", defaultBehavior)) { | ||
if (!build.behavior.pgResourceMatches(rightTable, "manyToMany") || | ||
!build.behavior.pgResourceMatches(rightTable, "select")) { | ||
return memoRight; | ||
@@ -47,0 +42,0 @@ } |
@@ -30,4 +30,3 @@ "use strict"; | ||
return extend(fields, Object.entries(junctionTable.codec.attributes).reduce((memo, [attributeName, attribute]) => build.recoverable(memo, () => { | ||
const behavior = build.pgGetBehavior(attribute.extensions); | ||
if (!build.behavior.matches(behavior, "select", "select")) { | ||
if (!build.behavior.pgCodecAttributeMatches([junctionTable.codec, attribute], "select")) { | ||
return memo; | ||
@@ -34,0 +33,0 @@ } |
@@ -126,10 +126,6 @@ "use strict"; | ||
} | ||
const behavior = build.pgGetBehavior([ | ||
junctionTable.getRelation(rightRelationName).extensions, | ||
junctionTable.extensions, | ||
]); | ||
if (build.behavior.matches(behavior, "connection", "connection -list")) { | ||
if (build.behavior.pgManyToManyMatches(pgManyToManyRelationship, "connection")) { | ||
makeFields(true); | ||
} | ||
if (build.behavior.matches(behavior, "list", "connection -list")) { | ||
if (build.behavior.pgManyToManyMatches(pgManyToManyRelationship, "list")) { | ||
makeFields(false); | ||
@@ -136,0 +132,0 @@ } |
@@ -0,3 +1,14 @@ | ||
import { PgManyToManyRelationDetails, PgTableResource } from "."; | ||
export declare const junctionSymbol: unique symbol; | ||
declare global { | ||
namespace GraphileBuild { | ||
interface BehaviorEntities { | ||
pgManyToMany: PgManyToManyRelationDetails; | ||
} | ||
interface Build { | ||
pgManyToManyRealtionshipsByResource: Map<PgTableResource, PgManyToManyRelationDetails[]>; | ||
} | ||
} | ||
} | ||
export declare const PgManyToManyRelationPlugin: GraphileConfig.Plugin; | ||
//# sourceMappingURL=PgManyToManyRelationPlugin.d.ts.map |
@@ -12,2 +12,5 @@ "use strict"; | ||
exports.junctionSymbol = Symbol("junction"); | ||
function isPgTableResource(r) { | ||
return !!r.codec.attributes && !r.parameters; | ||
} | ||
exports.PgManyToManyRelationPlugin = { | ||
@@ -17,10 +20,31 @@ name: "PgManyToManyRelationPlugin", | ||
schema: { | ||
entityBehavior: { | ||
pgResource: "manyToMany select", | ||
pgCodecRelation: "manyToMany select", | ||
pgManyToMany: { | ||
after: ["default", "inferred"], | ||
provides: ["override"], | ||
callback(behavior, relation, build) { | ||
const { junctionTable, rightTable, rightRelationName } = relation; | ||
const overrides = build.pgGetBehavior([ | ||
junctionTable.extensions, | ||
junctionTable.getRelation(rightRelationName).extensions, | ||
rightTable.extensions, | ||
]); | ||
return ["manyToMany connection -list", behavior, overrides]; | ||
}, | ||
}, | ||
}, | ||
hooks: { | ||
build(build) { | ||
build.pgManyToManyRealtionshipsByResource = new Map(); | ||
return build; | ||
}, | ||
init(_, build, _context) { | ||
for (const rawResource of Object.values(build.input.pgRegistry.pgResources)) { | ||
if (!rawResource.codec.attributes || rawResource.parameters) { | ||
for (const leftTable of Object.values(build.input.pgRegistry.pgResources)) { | ||
if (!isPgTableResource(leftTable)) { | ||
continue; | ||
} | ||
const leftTable = rawResource; | ||
const relationships = (0, manyToManyRelationships_1.default)(leftTable, build); | ||
build.pgManyToManyRealtionshipsByResource.set(leftTable, relationships); | ||
for (const relationship of relationships) { | ||
@@ -46,3 +70,6 @@ (0, createManyToManyConnectionType_1.default)(relationship, build, leftTable); | ||
const leftTable = leftTables[0]; | ||
const relationships = (0, manyToManyRelationships_1.default)(leftTable, build); | ||
const relationships = build.pgManyToManyRealtionshipsByResource.get(leftTable); | ||
if (!relationships || relationships.length === 0) { | ||
return fields; | ||
} | ||
return extend(fields, relationships.reduce((memo, relationship) => build.recoverable(memo, () => { | ||
@@ -165,12 +192,7 @@ const { leftTable, leftRelationName, junctionTable, rightRelationName, rightTable, allowsMultipleEdgesToNode, } = relationship; | ||
} | ||
const behavior = build.pgGetBehavior([ | ||
junctionTable.extensions, | ||
junctionTable.getRelation(rightRelationName).extensions, | ||
rightTable.extensions, | ||
]); | ||
if (build.behavior.matches(behavior, "manyToMany", "manyToMany")) { | ||
if (build.behavior.matches(behavior, "connection", "connection -list")) { | ||
if (build.behavior.pgManyToManyMatches(relationship, "manyToMany")) { | ||
if (build.behavior.pgManyToManyMatches(relationship, "connection")) { | ||
makeFields(true); | ||
} | ||
if (build.behavior.matches(behavior, "list", "connection -list")) { | ||
if (build.behavior.pgManyToManyMatches(relationship, "list")) { | ||
makeFields(false); | ||
@@ -177,0 +199,0 @@ } |
{ | ||
"name": "@graphile-contrib/pg-many-to-many", | ||
"version": "2.0.0-1.1", | ||
"version": "2.0.0-alpha.0", | ||
"description": "Add connection fields for many-to-many relations", | ||
@@ -23,3 +23,3 @@ "main": "dist/index.js", | ||
"devDependencies": { | ||
"@dataplan/pg": "^0.0.1-1.1", | ||
"@dataplan/pg": "^0.0.1-alpha.10", | ||
"@tsconfig/node16": "^1.0.3", | ||
@@ -31,4 +31,4 @@ "eslint": "^8.28.0", | ||
"eslint_d": "^12.2.1", | ||
"grafast": "^0.0.1-1.1", | ||
"graphile-build-pg": "^5.0.0-1.1", | ||
"grafast": "^0.0.1-alpha.9", | ||
"graphile-build-pg": "^5.0.0-alpha.11", | ||
"graphql": "16.1.0-experimental-stream-defer.6", | ||
@@ -38,3 +38,3 @@ "jest": "^29.3.1", | ||
"pg": ">=6.1.0 <9", | ||
"postgraphile": "^5.0.0-1.1", | ||
"postgraphile": "^5.0.0-alpha.11", | ||
"prettier": "2.8.0", | ||
@@ -41,0 +41,0 @@ "typescript": "^5.0.0" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
81055
735