graphql-python / graphql-python/graphene-sqlalchemy

Generate Input Arguments from SQLAlchemy Class?

未關閉
#112 3 則留言 0 個 reaction 已指派 0 人 在 GitHub 檢視
主要語言
Python
星號
985
分支
223
PR 合併指標
30 天內沒有已合併 PR

描述

Hello,
Do you know if it's possible to generate input arguments dynamically from the SQLAlchemy class that will be transformed by the mutation?
Example:

My input arguments for a `CreatePerson` mutation look like this:

```python
class CreatePersonInput(graphene.InputObjectType):
"""Arguments to create a person."""
name = graphene.String(required=True, description="Name of the person to be created.")
height = graphene.String(default_value="unknown", description="Height of the person to be created.")
mass = graphene.String(default_value="unknown", description="Mass of the person to be created.")
hair_color = graphene.String(default_value="unknown", description="Hair color of the person to be created.")
skin_color = graphene.String(default_value="unknown", description="Skin color of the person to be created.")
eye_color = graphene.String(default_value="unknown", description="Eye color of the person to be created.")
birth_year = graphene.String(default_value="unknown", description="Birth year of the person to be created.")
gender = graphene.String(default_value="unknown", description="Gender of the person to be created.")
planet_id = graphene.ID(default_value="unknown", description="Global Id of the planet from which the person to be created comes from.")
url = graphene.String(default_value="unknown", description="URL of the person in the Star Wars API.")

class CreatePerson(graphene.Mutation):
"""Mutation to create a person."""
person = graphene.Field(lambda: People, description="Person created by this mutation.")

class Arguments:
input = CreatePersonInput(required=True)

...
```

In the meantime, the input arguments for my `UpdatePerson` mutation look like this:

```python
class UpdatePersonInput(graphene.InputObjectType):
"""Arguments to update a person."""
id = graphene.ID(required=True)
name = graphene.String()
height = graphene.String()
mass = graphene.String()
hair_color = graphene.String()
skin_color = graphene.String()
eye_color = graphene.String()
birth_year = graphene.String()
gender = graphene.String()
planet_id = graphene.ID()
url = graphene.String()

class UpdatePerson(graphene.Mutation):
"""Update a person."""
person = graphene.Field(lambda: People, description="Person updated by this mutation.")

class Arguments:
input = UpdatePersonInput(required=True)

...
```

Finally, my SQLAlchemy class look like this:

```python
class ModelPeople(Base):
"""People model."""

__tablename__ = 'people'

id = Column('id', Integer, primary_key=True)
name = Column('name', String)
height = Column('height', String)
mass = Column('mass', String)
hair_color = Column('hair_color', String)
skin_color = Column('skin_color', String)
eye_color = Column('eye_color', String)
birth_year = Column('birth_year', String)
gender = Column('gender', String)
planet_id = Column('planet_id', Integer, ForeignKey('planet.id'))
created = Column('created', String)
edited = Column('edited', String)
url = Column('url', String)

...
```

This is all pretty redundant and it would be ideal if we could just reuse the SQLAlchemy class attributes in the `InputObjectType`

貢獻指南

開啟貢獻指南

研究方向

No repository files, tests, or entry points are named. Start by tracing how graphene.InputObjectType and SQLAlchemy model columns are currently represented, using the CreatePersonInput, UpdatePersonInput, and ModelPeople examples; done means establishing whether their attributes can be reused for generated mutation inputs and defining the supported behavior.

由索引模型根據 Issue 內容生成。

評估

技術堆疊
graphql, python, sqlalchemy
領域
api, backend, database
Issue 類型
功能
難度
5/5
預估耗時
一週以上
活躍度
停滯
描述清晰度
需要釐清
新手友好度
30/100

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。