hasura / hasura/graphql-engine

Action Codegen: python-flask issue with optional inputs

Open
#6,736 0 comments 2 reactions 0 assignees View on GitHub
c/console
Dominant language
TypeScript
Stars
32.1k
Forks
3k
PR merge metrics
PR metrics pending

Description

# Issue with Hasura python-flask Codegen
I use a _Query_ action on Hasura. It queries a Flask REST server generated with Hasura in the Codegen tab. I would like my arguments to be optional, though that doesn't work as intended.

**E.g.** With this _action definition_
```GraphQL
type Query {
actionName (arg1: SampleInput): SampleOutput
}
```
I get 2 Python files from the Hasura Codegen feature, that are **actionName.py** and **actionNameTypes.py**. The latter is in my opinion the one to blame. It uses the _typing_ Python library to create a Python class structure that will resemble the structure from the JSON request.

In particular, I tried both to define the action with an _optional_ and a _mandatory_ **arg1** parameter. The only difference in the Python code is in the **Optional** keyword.

**Mandatory** version:
```Python
@dataclass
class actionNameArgs(RequestMixin):
arg1: SampleInput
```

**Optional** version:
```Python
@dataclass
class actionNameArgs(RequestMixin):
arg1: Optional[SampleInput]
```

This is though not enough to make _arg1_ actually optional, as stated [here (docs.python.org)](https://docs.python.org/3/library/typing.html#typing.Optional). A default value is in fact required.

**Example** of a working version:
```Python
@dataclass
class actionNameArgs(RequestMixin):
arg1: Optional[SampleInput] = None
```

This way, Python won't throw any Exceptions, since the **None** default value will be set to **arg1** when it will not be explicitly passed through the JSON call.

Do you think you can integrate this fix into the Python Codegen feature?

Contributor guide

Open the contributing guide

Research direction

Start by locating the Python-Flask Codegen templates or entry points that generate actionNameTypes.py, then inspect how optional action arguments are rendered in the dataclass. Done means an optional arg1 receives a None default when omitted, with a regression test covering the generated output.

Written by the indexing model from the issue text.

Assessment

Tech stack
flask, python
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.