FormidableLabs / FormidableLabs/groqd

`conditionalByType` ignores part of the result when validation is missing

Open
#400 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
293
Forks
15
PR merge metrics
No merged PRs in 30d

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Code of Conduct

- [x] I agree to follow this project's Code of Conduct

### Code Sandbox link

_No response_

### Bug report

`conditionalByType` ignores part of the result in a specific scenario.
When one type (e.g. 'heroSection') has its own parser, but another (e.g. 'block') does not, then the result for the latter is ignored during parsing.
This happens because `conditionalParser` returns an [empty object](https://github.com/FormidableLabs/groqd/blob/09d745aa0235c3f4835bf94616a2f1478bd32079/packages/groqd/src/commands/subquery/conditionalByType.ts#L104) in this case.

This does not work:

```
conditionalByType({
block: { // result is { _type: 'block' }
children: true,
style: true,
listItem: true,
markDefs: true,
level: true,
},
heroSection: (heroSection) => ({
title: true,
description: true,
photos: heroSection.field("photos[]").project((photo) => ({
src: photo.field("image").field("asset").deref().field("url"),
alt: true,
caption: true,
})),
columns,
})
})
```
But this does:
```
conditionalByType({
block: { // result is { _type: 'block', children: ..., style: ..., etc... }
children: true,
style: q.string(),
listItem: true,
markDefs: true,
level: true,
},
heroSection: (heroSection) => ({
title: true,
description: true,
photos: heroSection.field("photos[]").project((photo) => ({
src: photo.field("image").field("asset").deref().field("url"),
alt: true,
caption: true,
})),
columns,
})
})
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.