Skip to content

Commit

Permalink
Add an idPath to relationship field adapters (#3741)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Sep 22, 2020
1 parent 48623ae commit a02e699
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/light-pears-smile.md
@@ -0,0 +1,5 @@
---
'@keystonejs/fields': patch
---

Updated internals of `Relationship` internals in preparation for `Prisma` support.
9 changes: 6 additions & 3 deletions packages/fields/src/types/Relationship/Implementation.js
Expand Up @@ -125,10 +125,11 @@ export class Relationship extends Implementation {
return {
[this.path]: (item, _, context, info) => {
// No ID set, so we return null for the value
if (!item[this.path]) {
const id = item && (item[this.adapter.idPath] || (item[this.path] && item[this.path].id));
if (!id) {
return null;
}
const filteredQueryArgs = { where: { id: item[this.path].toString() } };
const filteredQueryArgs = { where: { id: id.toString() } };
// We do a full query to ensure things like access control are applied
return refList
.listQuery(filteredQueryArgs, context, refList.gqlNames.listQueryName, info)
Expand Down Expand Up @@ -207,7 +208,7 @@ export class Relationship extends Implementation {
: [];
currentValue = currentValue.map(({ id }) => id.toString());
} else {
currentValue = item && item[this.path];
currentValue = item && (item[this.adapter.idPath] || (item[this.path] && item[this.path].id));
currentValue = currentValue && currentValue.toString();
}

Expand Down Expand Up @@ -325,6 +326,7 @@ export class Relationship extends Implementation {
export class MongoRelationshipInterface extends MongooseFieldAdapter {
constructor(...args) {
super(...args);
this.idPath = this.dbPath;

// JM: It bugs me this is duplicated in the implementation but initialisation order makes it hard to avoid
const [refListKey, refFieldPath] = this.config.ref.split('.');
Expand Down Expand Up @@ -363,6 +365,7 @@ export class MongoRelationshipInterface extends MongooseFieldAdapter {
export class KnexRelationshipInterface extends KnexFieldAdapter {
constructor() {
super(...arguments);
this.idPath = this.dbPath;
this.isRelationship = true;

// Default isIndexed to true if it's not explicitly provided
Expand Down

0 comments on commit a02e699

Please sign in to comment.