hasura / hasura/graphql-engine

REST Endpoint Response Transforms

Open
#8,154 1 comment 9 reactions 0 assignees View on GitHub
k/enhancement
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

### Is your proposal related to a problem?

When using the GraphQL to REST API feature to expose actions it is not possible to remove the top level field with the action name. This makes sense for GraphQL but isn't great for a REST endpoint with nested resources. Here is a simple example of an action (called `myAction`) converted to a REST endpoint.

This GraphQL:
```graphql
mutation myMutation($magic_string: String!){
myAction(arg1: $magic_string){
output_field1 {
is_string_valid
}
output_field2 {
number_of_letter_a
}
}
}
```
when converted to REST would return:
```json
{
"myAction": {
"output_field1":{
"is_string_valid": true
},
"output_field2": {
"number_of_letter_a": 12
}
}
}
```
This makes it hard to use on an existing endpoint because a semantic action name might not match an existing schema for a REST endpoint (in this example the endpoint might return `output_field1` and `output_field2` as top level items in the JSON).

### Describe the solution you'd like

Ideally the output could be modified so that this was returned from the REST endpoint:
```json
{
"output_field1":{
"is_string_valid": true
},
"output_field2": {
"number_of_letter_a": 12
}
}
```

I imagine that there are not good rules to do this flattening universally, so the user would have to specify a schema. Otherwise flattening could run into conflicting field names (for example - two `id` fields).

A transformer for the REST-ified Graphql response would be great, so to create the output above from the Graphql we could specify something like:
```
{
"output_field1" {
"is_string_valid": $repsonse.myAction.output_field1.is_string_valid
},
"output_field2": {
"number_of_letter_a": $repsonse.myAction.output_field2.number_of_letter_a
}
}
```

I modeled the above so it could be a lot like the outbound request transforms. I imagine that this feature would be useful for organizations with a lot of existing REST apis so they can convert endpoints to actions, but still keep the existing schema even if their new GraphQL schema would create an incompatible response schema.

### Describe alternatives you've considered

If you just need one top level field you can rename the action in my example, but generally this remains an issue for converting lots of endpoints to GraphQL while keeping also exposing REST endpoints up with an existing schema. This allows larger projects to do gradual migrations, or a public API to keep their older version up while exposing GraphQL as well.

### If the feature is approved, would you be willing to submit a PR?

Unfortunately I definitely don't know enough Haskell or have time to learn to implement the core of this feature. I could help with console updates.

Contributor guide

Open the contributing guide

Research direction

The issue names no files or tests; start by locating the GraphQL-to-REST response-generation entry point in the Haskell core. Define how a user-specified response schema maps fields without the action wrapper, then verify the configured output and handling of conflicting field names.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
api, backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.