Skip to content

Commit

Permalink
Clarify subquery usages with array (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonrobot authored Oct 31, 2023
1 parent 6b3e900 commit 0847736
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions docs/query-building.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ const data = await runQuery(q("*").filter("_type == 'pokemon'"));

### Starting with an array

Sometimes your base query returns an array. `groqd` has no way of knowing when this occurs, so you'll need to give it a hint by passing `isArray: true` to the second arg of `q`.
Sometimes your base query returns an array. `groqd` has no way of knowing when this occurs, so you'll need to give it a hint by passing `isArray: true` to the second arg of `q`. The `isArray` option only changes the return type of `q()` to `ArrayQuery` so the TS types can be more accurately represented. It _does not_ change the actual output query.

```ts
q("*[_type == 'pokemon']", { isArray: true })
.grab$({ name: q.string() })
// translates to: *[_type == 'pokemon']
```

## `.filter`
Expand Down Expand Up @@ -384,6 +385,37 @@ q("*")
});
```

:::note
When using a subquery it can be easy to mistake using the `isArray` option for selecting the array in the GROQ query. The following subqueries produce the same output and show the correct way to dereference an array as a subquery.

By using array select

```ts
hosts: q('hosts[]', { isArray: true })
.deref()
.grab({
_id: q.string(),
name: q.string(),
}),

// translates to "hosts": hosts[]->{ _id, name }
```

Or by using an empty `filter` method

```ts
hosts: q('hosts')
.filter()
.deref()
.grab({
_id: q.string(),
name: q.string(),
}),

// translates to "hosts": hosts[]->{ _id, name }
```
:::

## `.score`

Used to pipe a list of results through the `score` GROQ function.
Expand All @@ -410,4 +442,3 @@ q("*")
.grab({ name: q.string() })
.nullable(); // 👈 we're okay with a null value here
```

1 comment on commit 0847736

@vercel
Copy link

@vercel vercel bot commented on 0847736 Oct 31, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

groqd – ./

groqd-formidable-labs.vercel.app
groqd-git-main-formidable-labs.vercel.app
groqd.vercel.app

Please sign in to comment.