graphql-python / graphql-python/graphene-sqlalchemy

Add a way to serve custom properties on model classes

Aberta
#103 18 comentários 11 reações 0 responsáveis Ver no GitHub
enhancement
Linguagem predominante
Python
Estrelas
985
Forks
223
Métricas de merge de PRs
Nenhum PR com merge em 30d

Descrição

In SQLAlchemy, you often want to provide additional custom properties on the model classes, like this:
```python
class User(Base):
__tablename__ = 'users'
first_name = Column(String)
last_name = Column(String)

@property
def full_name(self):
return self.first_name + ' ' + self.last_name
```
Unfortunately, such properties are currently not made accessible in GraphQL by graphene_sqlalchemy. While there is a way to exclude existing column properties using the Meta attributes `exclude_fields` and `only_fields`, there is no way to include custom properties such as `full_name` from the above model.

One possible solution is to use [hybrid properties](http://docs.sqlalchemy.org/en/latest/orm/extensions/hybrid.html) which are now supported by graphene_sqlalchemy since 2.0. However, "ordinary" properties should also be supported. Often you dont really want or need hybrid properties - it is not always necessary or feasible to provide the property on the class level. Also, the conversion of hybrid properties currently has the limitation that it always generates `String` fields.

So I believe we need some mechanism for proxying arbitrary properties from SQLAlchemy to Graphene objects. I'm not yet sure whether all custom public properties (i.e. those not starting with an underscore) except those who are explicitly excluded should be proxied by default (like it is done with hybrid properties), or whether there should be a Meta attribute with a list of properties that must be explicitly set.

We probably also need to add a way to specify which type the (normal or hybrid) properties shall be converted to. This could also be defined in a Meta attribute, or maybe we could check for Python type hints if nothing is defined, like this:
```python
def full_name(self) -> str:
return self.first_name + ' ' + self.last_name

def current_age(self) -> int:
return current_year() - self.year_of_birth
```

I am willing to contribute with code, but first I want to get some feedback regarding this idea from others.

Guia de contribuição

Abrir o guia de contribuição

Direção de pesquisa

Start by reviewing graphene_sqlalchemy's existing hybrid-property support and the Meta options exclude_fields and only_fields. Resolve whether ordinary properties are included by default or explicitly listed, how their GraphQL types are chosen, and define tests that demonstrate property exposure and type conversion as the completion criteria.

Escrita pelo modelo de indexação a partir do texto da issue.

Avaliação

Stack de tecnologia
graphql, python, sqlalchemy
Domínio
api, backend
Tipo de issue
Funcionalidade
Dificuldade
5/5
Tempo estimado
Mais de uma semana
Status de atividade
Estagnada
Clareza
Precisa de esclarecimento
Facilidade para iniciantes
25/100

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.