Reputation: 5147
I know this question has been asked before but none of the questions were helpful hence asking again..
I am using graphene and parsing some Elasticsearch data before passing it to Graphene
PFB :- my resolved function
def resolve_freelancers(self, info):
session = get_session()
[ids, scores] = self._get_freelancers()
freelancers = session.query(FreelancerModel).filter(FreelancerModel.id.in_(ids)).all()
for index in range(len(ids)):
print("index", scores[index])
freelancers[index].score = scores[index]
if self.sort:
reverse = self.sort.startswith("-")
self.sort = self.sort.replace("-", "")
if self.sort == "alphabetical":
freelancers = sorted(freelancers, key=lambda f: f.name if f.name else "", reverse=reverse)
if self.sort == "created":
freelancers = sorted(freelancers, key=lambda f: f.created_on, reverse=reverse)
if self.sort == "modified":
freelancers = sorted(freelancers, key=lambda f: f.modified_at, reverse=reverse)
freelancers = [Freelancer(f) for f in freelancers[self.start:self.end]]
session.close()
return freelancers
now if I do
print(freelancers[index].score)
it gives me 10.989184
and the type of this is <class 'float'>
In my class Freelancer(graphene.ObjectType):
I have added score = graphene.Float()
Now when I try to add score
to my query it gives the error .. otherwise there is no issue .. all I am interested is in getting that score value in the json response .. I do not understand what is causing this error and I am fairly new to Python so any advise will be appreciated.
Please feel free to ask for additional code or information as I have tried to paste whatever I thought was relevant
Upvotes: 0
Views: 596
Reputation: 5147
Actually I cannot pass the fields directly to the Graphene Object and we need to pass it within the init method of the class which has the Graphene Object and then we need to return in a resolver method ( in my case resolve_score )
Upvotes: 0
Reputation: 66
So I can't comment or I would, and I very well may be wrong, but here goes.
My guess is that somewhere you are calling float(score)
, but the graphene.Float()
type cannot be directly converted to a Python float via float()
. This is probably due to the graphene.Float
type having so much data it can hold in its data structure due to inheriting from graphene.Scalar
(graphene GH/Scalars).
My guess would be to hunt down the float()
call and remove it. If that doesn't work, I would then move onto Float.num
field in your query.
Again, all conjecture here, but I hope it helped.
Upvotes: 0