graphql-python / graphql-python/graphene

Implement property-style Graphene fields

Abierto
#1,301 2 comentarios 1 reacción 0 asignados Ver en GitHub
✨ enhancement
Lenguaje dominante
Python
Estrellas
8.2k
Forks
818
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

**Is your feature request related to a problem? Please describe.**
I am trying to learn how to use Graphene with my existing DataModels. The problem I've come across is my use of python's `@property` decorator. My DataModel uses these to ensure proper logic when new attributes are set, and only allowing certain ones to be used. With Graphene's implementation, I am expected to define my properties as *attributes* instead, which eliminates my ability to use logic when a property is set, or decide which properties can be set at all.

The documentation does explain that Graphene objects can be used as real data-retaining objects, but I am not willing to sacrifice my current inheritance and implementation for a simple `ObjectType` class implementation.

Additionally, the documentation also explains that any [dictionary-like object can be used to lookup attributes with the same name](https://docs.graphene-python.org/en/latest/types/objecttypes/#defaultresolver), but now I am tasked with maintaining an additional class just to build the schema. I would prefer to keep my definition as tightly coupled to my actual implementation as I can, meaning I want to define it directly in my data-model.

**Describe the solution you'd like**
I want a way to integrate Graphene's schema building with my model's **properties**. I would prefer not to write custom resolvers and specially name all of the attributes I want to expose. Perhaps a decorator approach could be used
```python3
@property
@graphene_property(type=String, name="custom_name", desc="This is a property-based graphene field")
def serial(self):
...
```
I imagine this decorator can be a subclass of `Field` from `graphene.types.field`. Maybe a name like `@property_field` better suits this. I am definitely willing to try and open a PR for this, but I wanted to have some initial discussion first. I want to explore this later using a descriptor.

**Describe alternatives you've considered**
I am currently not using Django, but I've considered investing more into the Django stack only to be able to use the additional `Meta` class option of `model`. I have not done enough research, and adopting the entire Django stack into my program is **not** what I'd like to do.

**Additional context**
I am currently **not** using Django. I would not like to implement it to fix this problem.

If you can think of a way that works around this issue without implementing a new feature, I'd be happy to discuss. I don't know the ins and outs of Graphene, and I want to express this all very humbly. Please let me know if I'm horribly overlooking something, or you believe my implementation is bonkers. Thanks for your time!

Guía de contribución

No hay ninguna guía de contribución indexada para este repositorio

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.