diff --git a/README.md b/README.md index 2bbc717..85a1c2f 100644 --- a/README.md +++ b/README.md @@ -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). -_1230 TILs and counting..._ +_1231 TILs and counting..._ --- @@ -342,6 +342,7 @@ _1230 TILs and counting..._ ### GROQ +- [Grab Multiple Values From A Reference](groq/grab-multiple-values-from-a-reference.md) - [Grab Values From An Array Of References](groq/grab-values-from-an-array-of-references.md) - [Include Type Of Operation In Webhook Response](groq/include-type-of-operation-in-webhook-response.md) diff --git a/groq/grab-multiple-values-from-a-reference.md b/groq/grab-multiple-values-from-a-reference.md new file mode 100644 index 0000000..fe285a1 --- /dev/null +++ b/groq/grab-multiple-values-from-a-reference.md @@ -0,0 +1,43 @@ +# Grab Multiple Values From A Reference + +Let's say we have an `author` with some attributes including a reference to a +`person` which contains more data about the person. Here is one way to write a +query to access that data. + +```groq +*[_type == 'author' && slug.current == 'donna-tartt']{ + website, + 'firstName': person->firstName, + 'lastName': person->lastName, + 'age': person->age +} +``` + +Here is another way to write this query that doesn't do three separate accesses +on the `person` reference. + +```groq +*[_type == 'author' && slug.current == 'donna-tartt']{ + website, + person-> { + 'firstName': firstName, + 'lastName': lastName, + 'age': age + } +} +``` + +This isn't quite right though because it leaves the three reference values +nested under `person`. We can get back to the original shape of our query by +flattening the `person` object using familiar looking spread syntax (`...`). + +```groq +*[_type == 'author' && slug.current == 'donna-tartt']{ + website, + ...person-> { + 'firstName': firstName, + 'lastName': lastName, + 'age': age + } +} +```