6 ms·
Is there a way to define attributes as a function of other attributes? I poked through the documentation and tried to prototype it but couldn't get it to work
by dwiel 11y ago
Is there a way to define attributes as a function of other attributes? I poked through the documentation and tried to prototype it but couldn't get it to work. Am I missing something?
- syrusakbary 11y agoHi dwiel, you mean fields inside types? (as class attributes?) If so, putting the attributes inside a function is usually for lazy resolving the fields. Sometimes the types could be defined after the class definition. In this case, you can use strings for reference field types. Something like: class User(graphene.ObjectType): friends = graphene.Field('self') # 'User' would work too.
- dwiel 11y agoYeah I think that is what I mean. I want one class attribute to be a function of another class attribute. This may be naive but here is an example of what I want to be able to do: class User(graphene.ObjectType): name = graphene.String() initials = graphene.String() def resolve_name(self, args, info): return user_service.lookup_name(...) def resolve_initials(self, args, info): # How can I refer to the result of resolve_name above? return ''.join(part for part in name.split(' ')) Perhaps I'm not using graphene in the way it was intended to be used?
- syrusakbary 11y agoUsually you have a self._root object (in Django, for example this _root is the model instance) and then you use self.instance.name in both resolve_name and resolve_initials.