-
Notifications
You must be signed in to change notification settings - Fork 766
Mapping to models #1271
-
Hi dears,
First, I want to thanks all the team of graphene for the incredible work that is done. It's a wonderful tool and I'd like to contribute in my way, so here is a proposal:
My Django models are quite complicated and I'd like to expose more simple models to the external world. For example, I have models like thats:
class Car(models.Model):
name = Charfield(max_length=123)
data = OneToOne(CarData)
class CarData(models.Model):
value1=models.CharField(choices=foo)
There can be many reason I made a separation between Car and CarData, but for me its for performance reasons or simply for decoupling things.
An API is a 'view' of my models for the external world, it has not to reflect the reality of my models. However, I have to use them to build my api and I'd like to be able to map the 'view' I want to give to my actual model.
For example I'd like to show it flat like that:
Car:
- name
- value1
For the moment, to make my API I have to make something like that:
class CarType(DjangoObjectType):
value1 = Enum(...)
def resolve_value1(self, info):
return self.data.value1
class Meta:
model = Ingredient
fields = ("id", "name", "value1")
So in fact, because I need to simplify the view of my models (And for me, a view, or an API is a simplification of a model), I can't benefit anymore of the automatic behavior of graphene, such as turning the choices as an enum automatically, defining automatically what type is value1 etc..
- I'd like to be able to make something like that (source is a keyword inspired by DRF):
class CarType(DjangoObjectType):
value = MappedField(source='data.value1')
class Meta:
model = Ingredient
fields = ("id", "name", "value")
- More simply by repeating the type (but we loose the Enum magic):
class CarType(DjangoObjectType):
value = Enum(foo, source='data.value1')
class Meta:
model = Ingredient
fields = ("id", "name", "value")
- or something like this:
class CarType(DjangoObjectType):
class Meta:
model = Ingredient
fields = ("id", "name", ("value", "data.value1"))
You can see that I also used my magic tool to rename the field value1 to value, which is quite handful!
For me, an API doesn't have to reflect the complexity of a model, and we shouldn't loose all the magic when we are in cases like that, which happens all the time. This kind of notation let me also stay DRY (And I my age its better).
So I'd like to discuss about what you think of my point of view, the functionality i'd like to have, maybe I missed something that would help me achieving what I want. If you like it, we can discuss about the best form (Solution 1: MappedField, Solution 2: reapeting type (allow name mapping) or solution 3: tuple for source), and I will write my first PR to join the team!
Best regards, and thanks in advance for all the readers and commenters.
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment
-
Beta Was this translation helpful? Give feedback.