sanity icon indicating copy to clipboard operation
sanity copied to clipboard

Automatically reference both ways

Open dnlmzw opened this issue 3 years ago • 53 comments

Is your feature request related to a problem? Please describe. I'd like to automatically make a reference show up, if the reference has already been made on another document on the same field.

Describe the solution you'd like In my webshop, I'm connecting products based on color. I'd like it to automatically make the connection. E.g. I have a product color "Product A - Red" and I connect it with the "Product A - Green". I'd like the connection automatically be made the other way around, if I navigate to the "Product A - Green".

Additional context So, on the "Beige" product I'd like it to automatically show the black one as well. Skærmbillede 2021-06-08 kl  12 46 59

dnlmzw avatar Jun 08 '21 10:06 dnlmzw

Do you need to automatically insert the reference into the other document, or is this more about being able to see incoming references in Studio?

judofyr avatar Jun 29 '21 08:06 judofyr

Seeing the incoming references would actually be enough, and then I could just do some magic with GROQ to show it in the frontend.

dnlmzw avatar Jul 02 '21 08:07 dnlmzw

Can you check if thedocuments-pane plugin is good enough for you? https://www.sanity.io/plugins/documents-pane. This lets you write a GROQ query that will be displayed in a pane. There's an example for showing incoming references in the README.

judofyr avatar Jul 02 '21 09:07 judofyr

Not exactly what I was looking for and seeing the client now use the CMS, the 2-way reference being added automatically is probably what they would need.

dnlmzw avatar Jul 08 '21 07:07 dnlmzw

+1

rascode avatar Jul 28 '21 22:07 rascode

Automatic 2-way reference is an interesting feature. It hasn't really come up as important for us since GROQ is expressive enough to query for "reverse references" easily. Do you want it to be 2-way due to Studio or in your front-end application?

judofyr avatar Jul 30 '21 08:07 judofyr

But it's really important if you are using GraphQL to query the data. It's anti-pattern that it's not possible to access the referenced data from pretty much every type.

But to automatically match references to other field names could result in some problems/conflicts. If you want to know more about it I can explain it further but I would recommend to look at Prisma's approach for relations first.

sapkra avatar Oct 25 '21 20:10 sapkra

+1 the ability to have two-way references would be incredibly useful. Here's one use-case to illustrate the value –

Let's say we have a document type called artist that represents a musical performer. In the schema for artist, there's a field called relatedArtists that is an array of references to other artist documents. Editorially, it represents artists that are similar in style, etc. A pretty common pattern – granted it can be computed in many situations, in some cases these need to be curated carefully.

Let's say we're editing Artist A and add Artist B and Artist C as relatedArtists. When going to edit Artist B, Artist A should already be in the relatedArtists field for Artist B. This is important because by its very nature this reference represents a two-way link. If these artists are similar, they are similar no matter which direction you look at it from.

Of course on the client-side there's no issue, this can be achieved with GROQ. But it's critical for editorial purposes, as editors need to be able to see a given connection regardless of which side of it they are looking at. Otherwise, to go back to the above example, if an editor is looking at Artist B they would have no idea that Artist A is "related" (similar) – perhaps they disagree with the linkage and need to break it.

I suppose one work-around to address the lack of awareness for incoming connections is to set up a custom input that would display incoming connections, but that feels hacky and would still require them to go to the other record to make a change.

Allowing for many-to-many references on a schema level seems to be the cleanest approach that's already common in databases and other CMS platforms.

In the interim, has anyone discovered any workaround to implement this sort of thing? Doesn't seem like there's any sort of "hook" that can be used to programmatically mirror this functionality, right? Particularly considering the need to add/remove/modify records via API and not the Studio exclusively.

DmiPet avatar Dec 25 '21 01:12 DmiPet

I'm finding this out exactly as you've described @DmiPet... it's easy to handle this situation on the front end but for the editors experience it's imperative (IMO) that they're able to add/remove references from either side of the relationship.

Ojay avatar Apr 07 '22 15:04 Ojay

+1 our project would really benefit from being able to see and add / edit references from both linked documents

Denis-NV avatar Jun 27 '22 13:06 Denis-NV

I think this is already present in the data model of the content lake. References are always bi-directional as per the documentation found here: https://www.sanity.io/docs/connected-content.

They are, however, only shown on the document on which it is defined in the studio. Being able to see these bi-directional references from the opposite side, in a one-to-many array view would be great. cc: @dpfavand

MagneH avatar Aug 24 '22 08:08 MagneH

+1

josephkleier avatar Oct 21 '22 02:10 josephkleier

I'll add another perspective. For editors who are putting a movie catalog together. If 3 Movies were created by a team member who referenced Matthew McConaughey in all 3, it would be great if other team members could automatically see Matthew McConaughey is referenced in 3 Movies so they don't accidentally double-reference him. Or so that they don't forget Matthew McConaughey played in a specific movie when reviewing his document.

Incoming references are close, but it would be great to merge together incoming references in the same field as a specific reference field and delete incoming references from that specific field.

Example of Bi-Directional Field from Movie Document { name: 'castMembers', title: 'Cast Members', type: 'array', of: [ { type: 'reference', to: [ {type: 'person'}, {incoming: 'person'}, ] } ], },

Example of Bi-Directional Field from Person Document { name: 'movies', title: 'Movies', type: 'array', of: [ { type: 'reference', to: [ {type: 'movies'}, {incoming: 'movies'}, ] } ], },

josephkleier avatar Oct 21 '22 02:10 josephkleier

Another use would be in a form of comments system.

Say we have comment schema that references to post IDs, which is fine, but then the editor should have a list of comments attached to the post he is editing, with ability to delete or moderate them right there.

EvGreen avatar Nov 23 '22 09:11 EvGreen

+1 on this feature

byebyers avatar Dec 05 '22 18:12 byebyers

+1 would greatly help me. i'm creating a system for cataloguing Help Articles. Having the ability to see what articles fall into each category and vice-versa easily would be awesome.

Pablokiryu avatar Dec 06 '22 21:12 Pablokiryu

+1 on this feature

Or if any wizards have temporary implementations.

quitequinn avatar Jan 10 '23 20:01 quitequinn

+1 on this as well

omattman avatar Feb 04 '23 15:02 omattman

+1 Native bi-directional references would be super helpful!

gitinthehole avatar Feb 07 '23 05:02 gitinthehole

+1

smehdii avatar Feb 19 '23 09:02 smehdii

+1 also need this feature

kjprince avatar Feb 20 '23 16:02 kjprince

+1

cbrichau avatar Feb 21 '23 10:02 cbrichau

+1

Colton avatar Mar 09 '23 19:03 Colton

+1 Building Music catalog with may tracks genres and moods and artists. Now trying to understand how to solve this problem because there are 1000 tracks and 60 moods+ganres

kapyar avatar Mar 10 '23 10:03 kapyar

+1

roblom avatar Mar 21 '23 09:03 roblom

+1

kluplau avatar Mar 21 '23 10:03 kluplau

+1

jacksondoherty avatar Mar 22 '23 21:03 jacksondoherty

+1

alec-bfa avatar Mar 31 '23 09:03 alec-bfa

+1, two way references quiet nice done on strapi, agree that it is very usefull for editors. For example at my store, I do have some products content, and separate content for categories and discounts, as far as my products nuber is growing, it became more difficult to search what kind of product object has reference to discount. And that would be nice that it'd be autoupdated, and accessed by editor from both ways. So that you can edit the reference from any part.

neo-noire avatar Apr 12 '23 23:04 neo-noire

+1, it really tripped me up when I tried to query references with graphql and could only go one direction. Had to rethink my content model pretty substantially. :(

bryantcodesart avatar Apr 18 '23 16:04 bryantcodesart