Skip to content

Commit

Permalink
Add a check for invalid relationship configurations (#2269)
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie authored Jan 30, 2020
1 parent 6f8c42f commit 8f54a4e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/swift-lobsters-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/keystone': patch
---

Added a check for invalid relationship configurations.
5 changes: 5 additions & 0 deletions .changeset/unlucky-swans-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/fields': patch
---

Updated internal relationship configurations of `Content` fields to be self-consistent.
2 changes: 1 addition & 1 deletion packages/fields/src/types/CloudinaryImage/ImageBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class ImageBlock extends Block {
return {
[this.path]: {
type: RelationshipWrapper,
ref: this.auxList.key,
ref: `${this.auxList.key}.from`,
many: true,
schemaDoc: 'Images which have been added to the Content field',
},
Expand Down
2 changes: 1 addition & 1 deletion packages/fields/src/types/OEmbed/OEmbedBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class OEmbedBlock extends Block {
return {
[this.path]: {
type: RelationshipWrapper,
ref: this.auxList.key,
ref: `${this.auxList.key}.from`,
many: true,
schemaDoc: 'Embeds which have been added to the Content field',
},
Expand Down
2 changes: 1 addition & 1 deletion packages/fields/src/types/Unsplash/UnsplashBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class UnsplashBlock extends Block {
return {
[this.path]: {
type: RelationshipWrapper,
ref: this.auxList.key,
ref: `${this.auxList.key}.from`,
many: true,
schemaDoc: 'Unsplash Images which have been added to the Content field',
},
Expand Down
40 changes: 40 additions & 0 deletions packages/keystone/lib/Keystone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,45 @@ module.exports = class Keystone {
this._extendedMutations = this._extendedMutations.concat(mutations.map(_parseAccess));
}

_consolidateRelationships() {
const rels = {};
const otherSides = {};
this.listsArray.forEach(list => {
list.fields
.filter(f => f.isRelationship)
.forEach(f => {
const myRef = `${f.listKey}.${f.path}`;
if (otherSides[myRef]) {
// I'm already there, go and update rels[otherSides[myRef]] with my info
rels[otherSides[myRef]].right = f;

// Make sure I'm actually referencing the thing on the left
const { left } = rels[otherSides[myRef]];
if (f.config.ref !== `${left.listKey}.${left.path}`) {
throw new Error(
`${myRef} refers to ${f.config.ref}. Expected ${left.listKey}.${left.path}`
);
}
} else {
// Got us a new relationship!
rels[myRef] = { left: f };
if (f.refFieldPath) {
// Populate otherSides
otherSides[f.config.ref] = myRef;
}
}
});
});
// See if anything failed to link up.
const badRel = Object.values(rels).find(({ left, right }) => left.refFieldPath && !right);
if (badRel) {
const { left } = badRel;
throw new Error(
`${left.listKey}.${left.path} refers to a non-existant field, ${left.config.ref}`
);
}
}

/**
* @return Promise<null>
*/
Expand Down Expand Up @@ -684,6 +723,7 @@ module.exports = class Keystone {
pinoOptions,
cors = { origin: true, credentials: true },
} = {}) {
this._consolidateRelationships();
const middlewares = flattenDeep([
this.appVersion.addVersionToHttpHeaders &&
((req, res, next) => {
Expand Down

0 comments on commit 8f54a4e

Please sign in to comment.