In the example below, I have to explicitly define all of the 'resolve' methods in the ReturnValue class.
It would be more concise if I could auto define these methods within a loop, seeing as they all pretty much do the same thing, with the only difference being the name of the method which matches the name of the class member variable being returned.
Is this possible with Python 2.7?
import graphene
class ApiObj(object):
@staticmethod
def get_values_as_dictionary():
return {'dog': 'pound', 'cat': 'nip', 'horse': 'fly', 'bear': 'down'}
class ReturnKeys(graphene.Interface):
dog = graphene.String()
cat = graphene.String()
horse = graphene.String()
bear = graphene.String()
class ReturnValue(graphene.ObjectType):
class Meta:
interfaces = (ReturnKeys,)
def resolve_dog(self):
return self.dog
def resolve_cat(self):
return self.cat
def resolve_horse(self):
return self.horse
def resolve_bear(self):
return self.bear
api = ApiObj()
value_dict = api.get_values_as_dictionary()
rv = ReturnValue(**value_dict)
print rv.resolve_cat()
print rv.resolve_dog()
print rv.resolve_horse()
print rv.resolve_bear()