graphql-python / graphql-python/graphene
There's some way to set the `source` field param for nested fields?
- Lenguaje dominante
- Python
- Estrellas
- 8.2k
- Forks
- 818
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
## Context
I'm currently trying to set my model user `User` subfield `user.actor.id` as field source of my `DjangoObjectType`.
This happens because there's other kind of users within the application like an `Organization` or even a `Bot` (all of them are actors).
## Example
Lets define a custom `Type`:
```py
class User(DjangoObjectType):
class Meta:
model = UserModel
exclude = ['actor']
filter_fields = ['actor__login']
interfaces = [graphene.relay.Node]
skip_registry = True
# The login field is originated from `actor` relationship, not from the user model itself.
# It doesn't work, actor__login is interpreted as user field [user.actor__login] not [user.actor.login].
login = graphene.Field(graphene.String, source='actor__login')
# ...
```
In the example above I define the login field source as `source='actor__login'` but I [saw that here](https://github.com/graphql-python/graphene/blob/dfece7f65d4bad51a87a8e9f03f9acef1e25d3f8/graphene/types/field.py#L16-L20) we're using `getattr` function to resolve the source so it's currently impossible to set the field without defining a custom resolver.
https://github.com/graphql-python/graphene/blob/dfece7f65d4bad51a87a8e9f03f9acef1e25d3f8/graphene/types/field.py#L16-L20
I would like to know if there's an alternative solution to select a subfield like the example or we're forced to create a custom resolver by using the `resolver=lambda _, __: ...` arg or the class method declaration.
Thanks for the hard work and the library!
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
Línea de trabajo
Start with graphene/types/field.py at the referenced source-resolution code and trace how DjangoObjectType fields use the source value. Compare the `actor__login` example with the current custom-resolver alternative; done means the intended nested relationship behavior is supported or the limitation and expected resolver approach are documented.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- api, backend
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100