mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Grab Values From An Array Of References as a GROQ til
This commit is contained in:
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1191 TILs and counting..._
|
_1192 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -28,6 +28,7 @@ _1191 TILs and counting..._
|
|||||||
* [Git](#git)
|
* [Git](#git)
|
||||||
* [GitHub Actions](#github-actions)
|
* [GitHub Actions](#github-actions)
|
||||||
* [Go](#go)
|
* [Go](#go)
|
||||||
|
* [GROQ](#groq)
|
||||||
* [Heroku](#heroku)
|
* [Heroku](#heroku)
|
||||||
* [HTML](#html)
|
* [HTML](#html)
|
||||||
* [HTTP](#http)
|
* [HTTP](#http)
|
||||||
@@ -336,6 +337,10 @@ _1191 TILs and counting..._
|
|||||||
- [Sleep For A Duration](go/sleep-for-a-duration.md)
|
- [Sleep For A Duration](go/sleep-for-a-duration.md)
|
||||||
- [Upgrading From An Older Version On Mac](go/upgrading-from-an-older-version-on-mac.md)
|
- [Upgrading From An Older Version On Mac](go/upgrading-from-an-older-version-on-mac.md)
|
||||||
|
|
||||||
|
### GROQ
|
||||||
|
|
||||||
|
- [Grab Values From An Array Of References](groq/grab-values-from-an-array-of-references.md)
|
||||||
|
|
||||||
### Heroku
|
### Heroku
|
||||||
|
|
||||||
- [Deploy A Review App To A Different Stack](heroku/deploy-a-review-app-to-a-different-stack.md)
|
- [Deploy A Review App To A Different Stack](heroku/deploy-a-review-app-to-a-different-stack.md)
|
||||||
|
|||||||
29
groq/grab-values-from-an-array-of-references.md
Normal file
29
groq/grab-values-from-an-array-of-references.md
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
# Grab Values From An Array Of References
|
||||||
|
|
||||||
|
Let's say we have a `post` object in our schema. A `post` can have an array of
|
||||||
|
references to `tags` telling you what topics the post covers. Each `tag` has a
|
||||||
|
slug and we want to get the `string` value for each slug.
|
||||||
|
|
||||||
|
Let's say we are interested in the post with `_id` of `123`.
|
||||||
|
|
||||||
|
Here is how we can achieve that with a `groq` query:
|
||||||
|
|
||||||
|
```groq
|
||||||
|
*[
|
||||||
|
_type == 'post' && _id == 123
|
||||||
|
]{
|
||||||
|
'tags': tags[]->slug.current
|
||||||
|
}.tags
|
||||||
|
|
||||||
|
=> ["javascript", "react-js"]
|
||||||
|
```
|
||||||
|
|
||||||
|
If the schema was such that each `post` just had a single tag reference, then
|
||||||
|
you could write the chain of references as `tag->slug.current`. Because it is
|
||||||
|
an array of references, we need the `[]` to declare that we want each value.
|
||||||
|
|
||||||
|
The `->` operator follows the reference. Otherwise we'd just have access to the
|
||||||
|
`_ref` and `_type` values.
|
||||||
|
|
||||||
|
The final `.tags` unnests the `tags` value we gathered into an object. Then the
|
||||||
|
result is just the array of slug values.
|
||||||
Reference in New Issue
Block a user