hasura / hasura/graphql-engine
Differentiate between an action parameter being absent vs being present but null with Kriti
- Dominant language
- TypeScript
- Stars
- 32.1k
- Forks
- 3k
- PR merge metrics
- PR metrics pending
Description
### Is your proposal related to a problem?
I am using Custom Actions to transform GraphQL mutations received by Hasura into REST API requests to an existing API server. Some of these actions are to update a record. For example, I have an action `updateArticle` with two optional parameters: `name` and `description`.
I need this example action to be able to:
- Update both `name` and `description`
- Update just `name` but not change `description` (and vice versa)
- Update `name` with some value and set `description` to `null`
When using request body transformation to generate JSON from the action input, Kriti does not (as far as I can tell) provide any functionality that would allow me to distinguish between a request where the input contains `description: null` (where a consumer wants to nullify the field) and a request where the input does not contain a `description` parameter at all (where the consumer wants to leave `description` unchanged).
Without this functionality, I'm not able to support optional parameters for custom actions that update a record. I am forced to make all parameters required instead. Keeping with the previous example, an action consumer who wants to update `name` but leave `description` unchanged would have to know the value of `description` and pass it along with the request.
### Describe the solution you'd like
I would like to be able to generate different JSON request bodies based on the absence or presence of a parameter.
I'd like be able to write a request body transformation that will transform
```
{
"action": {
"name": "updateArticle"
},
"input": {
"name": "New Name",
"description": null,
}
}
```
into
```
{
"name": "New Name",
"description": null
}
```
and will transform
```
{
"action": {
"name": "updateArticle"
},
"input": {
"name": "New Name"
}
}
```
into
```
{
"name": "New Name"
}
```
### Describe alternatives you've considered
I've considered using request options transform to support optional parameters using the query string, but I run into basically the same problem - I can't differentiate between key/value pairs that are absent and key/value pairs where the value is `null`. And any `null` values that I would want to pass on would be dropped anyway.
I've reviewed my options in Kriti and am aware of the optional lookup operator, the defaulting operator, and if statements. These are the tools I'd like to use, but they do not differentiate between a key/value which is absent and a key/value whose value is `null`.
Contributor guide
Research direction
Start by locating Kriti's request body transformation implementation and the handling of optional lookups, defaulting, and null values. Define and test behavior that preserves a field when an input key is absent while emitting it when the key is present with a null value, using the two JSON examples as the completion criteria.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- graphql
- Domain
- api, backend-api-design
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100