Not able to query single collection on GraphQL with anything other than ID #3178
Replies: 7 comments 1 reply
-
Do you need the slug to be localized? Is that common practice? |
Beta Was this translation helpful? Give feedback.
-
That is in fact a common practice. Here are some webs with that behavior: https://www.whitehouse.gov/es/invertir/ -> ES https://es.wikipedia.org/wiki/Estados_Unidos -> ES https://www.godaddy.com/domains/domain-broker -> EN https://www.hauraton.com/en/products/ -> EN Please use the language switcher to see the slug changing. |
Beta Was this translation helpful? Give feedback.
-
You could use a where query to fetch a single doc though. Yes it will come back in an array of docs, but you will only have 1 document inside. That is normally what we do when we need to fetch documents by slugs, which I know is a common thing. |
Beta Was this translation helpful? Give feedback.
-
Yes, what you suggested is what we ended up doing! However, I think we should leave the issue open until we found a more permanent solution. |
Beta Was this translation helpful? Give feedback.
-
@creative-andrew, I don't know. I feel like this is a feature request and not an issue? Maybe we should convert it to a discussion. I see the value in it, I just am not sure we will keep it open as an "issue". |
Beta Was this translation helpful? Give feedback.
-
Sure thing. Let's do that. |
Beta Was this translation helpful? Give feedback.
-
This is a feature idea that has come up a few times. I propose we add a new property to collections called Internally this would be exactly how the current user Preferences endpoints are written and this behavior can be handled internally using the new The change will affect the REST routes and GraphQL so that querying can be done by the slug value. It also needs to handle localization so that the query value matches on the locale given to return the correct localized document. Indexes should be automatically created for The id field will still be present to support relationships, but routing by id will not be possible as the paths will conflict between ids and the slug field.
This will affect the admin UI as the PATCH request will need to hit the old value path route when saving document edits. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
To Reproduce
It is a common practice to use
slugs
or something similar to identify a collection as an addition to the regular ID.Right now you can only pass the
id
to GraphQL to get a single collection document.Hence, you cannot create a
GetPageBySlug
query.This makes things really hard in the front-end as I cannot query things by using just their slug and I am forced to pass the full id.
Payload provides a way to declare a custom ID.
However, this won't work in more than one language, as you cannot localize a custom id.
The idea here is to be able to declare a field to be used in GraphQL as a unique identifier other than the ID.
Collections support having unique fields, which would work really nicely with this as they are unique within the collection. However, I am not sure if they are also unique also on each language.
idType
is an enum that by default includesId
and other unique identifiers defined in a field.Here is an example from WordPress WPGraphQL solving this issue:
https://www.wpgraphql.com/docs/posts-and-pages#single-post-by-slug
Describe the Bug
Not able to query single collection on GraphQL with anything other than ID
Payload Version
1.9.0
Beta Was this translation helpful? Give feedback.
All reactions