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

Fix behaviour of hydrateRelationships with dangling references #6040

Merged
merged 11 commits into from
Jul 2, 2021

Conversation

timleslie
Copy link
Contributor

@timleslie timleslie commented Jul 1, 2021

If an item referenced in a document field is deleted from Keystone, the document field should handle this gracefully. The current behaviour will return an error when querying the field, which prevents the user from being able to manage the issue. This PR fixes this to simply return null for the missing item. This also covers the behaviour for when a user doesn't have read access for an item.

We also need to make a distinction between data.data === null (object not found/access denied) and data.data === {} (no selection fields in the config) in the document renderer.

@vercel
Copy link

vercel bot commented Jul 1, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/keystonejs/keystone-next-docs/BWMt7wXG3f43Q4DJiPyKrwLAb7uM
✅ Preview: https://keystone-next-docs-git-fix-dangling-references-keystonejs.vercel.app

@changeset-bot
Copy link

changeset-bot bot commented Jul 1, 2021

🦋 Changeset detected

Latest commit: fb29c33

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@keystone-next/fields-document Patch
@keystone-next/api-tests-legacy Patch
@keystone-next/document-renderer Major
@keystone-next/example-document-field Patch
@keystone-next/examples-app-basic Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

// If we're unable to find the user (e.g. we have a dangling reference), or access was denied
// then simply return null.
console.error(`Unable to fetch relationship data: relationship: ${JSON.stringify(relationship)}, id: ${id} `);
console.error(err);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm undecided as to whether these console error messages are helpful, or just noise 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i don't think they're helpful though could you look at what it looks like to only do this on access control/does not exist? (the easiest way might be to do a many query and just get the first one from the array and handle if the array is empty?)

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jul 1, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@vercel vercel bot temporarily deployed to Preview July 1, 2021 04:33 Inactive
@timleslie timleslie requested a review from a team July 1, 2021 04:35
packages-next/fields-document/src/relationship-data.tsx Outdated Show resolved Hide resolved
// If we're unable to find the user (e.g. we have a dangling reference), or access was denied
// then simply return null.
console.error(`Unable to fetch relationship data: relationship: ${JSON.stringify(relationship)}, id: ${id} `);
console.error(err);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, i don't think they're helpful though could you look at what it looks like to only do this on access control/does not exist? (the easiest way might be to do a many query and just get the first one from the array and handle if the array is empty?)

packages-next/fields-document/src/relationship-data.tsx Outdated Show resolved Hide resolved
@vercel vercel bot temporarily deployed to Preview July 1, 2021 04:46 Inactive
@vercel vercel bot temporarily deployed to Preview July 1, 2021 05:35 Inactive
@vercel vercel bot temporarily deployed to Preview July 1, 2021 05:51 Inactive
@vercel vercel bot temporarily deployed to Preview July 1, 2021 06:14 Inactive
@vercel vercel bot temporarily deployed to Preview July 1, 2021 06:20 Inactive
@timleslie timleslie requested a review from emmatown July 1, 2021 06:31
@vercel vercel bot temporarily deployed to Preview July 1, 2021 06:54 Inactive
@vercel vercel bot temporarily deployed to Preview July 1, 2021 23:45 Inactive
@vercel vercel bot temporarily deployed to Preview July 2, 2021 00:00 Inactive
packages-next/document-renderer/src/index.tsx Outdated Show resolved Hide resolved
.changeset/rare-ducks-suffer.md Outdated Show resolved Hide resolved
const r = JSON.stringify(relationship);
console.error(`Unable to fetch relationship data: relationship: ${r}, id: ${id} `);
console.error(err);
return { id, data: null };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return { id, data: null };
return { id };

@@ -41,30 +41,36 @@ export function addRelationshipData(
}
return [];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to handle the many case

.changeset/rare-ducks-suffer.md Outdated Show resolved Hide resolved
@emmatown
Copy link
Member

emmatown commented Jul 2, 2021

(oh sorry, missed that the time that the review was requested was before we talked)

@timleslie
Copy link
Contributor Author

(oh sorry, missed that the time that the review was requested was before we talked)

All good, just working through things now. I'm gonna save the many changes until I tackle the component blocks as a whole. Just adding final tests for the non-expected error cases and then will clean up the PR.

@vercel vercel bot temporarily deployed to Preview July 2, 2021 03:12 Inactive
@vercel vercel bot temporarily deployed to Preview July 2, 2021 03:30 Inactive
@vercel vercel bot temporarily deployed to Preview July 2, 2021 03:34 Inactive
@timleslie timleslie requested a review from emmatown July 2, 2021 03:36
@timleslie
Copy link
Contributor Author

@mitchellhamilton OK, Ready for re-review. The only thing I haven't covered here is the many case, but I plan to cover that in a follow up which covers the overall behaviour of the prop kind.

@timleslie timleslie merged commit 890e3d0 into master Jul 2, 2021
@timleslie timleslie deleted the fix-dangling-references branch July 2, 2021 03:53
Nikitoring pushed a commit to Nikitoring/keystone that referenced this pull request Sep 13, 2021
Nikitoring pushed a commit to Nikitoring/keystone that referenced this pull request Sep 14, 2021
Nikitoring pushed a commit to Nikitoring/keystone that referenced this pull request Sep 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants