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

Add an idPath to relationship field adapters #3741

Merged
merged 2 commits into from Sep 22, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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