How to share models for calling methods for different resolver? #1179
-
This question is somewhat related to this issue: graphql-python/graphene#803
The query currently looks like this:
Here, I am wondering how I can share my LocationData object such that it is initialized only once and than its methods available for both resolve functions? Like or similiar to this:
|
Beta Was this translation helpful? Give feedback.
Answered by
tricoder42
Aug 16, 2018
Replies: 2 comments
-
What about different schema? query {
location(geo: {lat: 30, lng: 20, tmp: 2}) {
first {
firstMetric
}
second {
secondMetric
thirdMetric
}
}
} class LocationType(graphene.ObjectType):
first = graphene.Field(FirstField)
second = graphene.Field(SecondField)
def resolve_first(self, info, geo):
# self is LocationData
return FirstField(first_metric=self.first_metric())
def resolve_second(self, info, geo):
# self is LocationData
value1 = self.second_metric()
value2 = value1+300
return SecondField(second_metric=value1,
third_metric=value2)
class Query(graphene.ObjectType):
location = graphene.Field(LocationType, geo=GeoInput(required=True))
def resolve_location(self, info, geo):
# return value becomes self in LocationType resolvers
return LocationData(geo.lat, geo.lng, geo.tmp) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
zbyte64
-
Closing as @tricoder42 solution should work. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What about different schema?