Issue
I'm using django graphene for building a graphql server, it consume a RESTfull API to get data, following this schema:
class DeviceType(graphene.ObjectType):
id = graphene.ID()
reference = graphene.String()
serial = graphene.Float()
class InstallationType(graphene.ObjectType):
id = graphene.ID()
company = graphene.Int()
device = graphene.ID()
class AllDataType(graphene.ObjectType):
device = graphene.Field(DeviceType)
installation = graphene.Field(InstallationType)
class Query(graphene.ObjectType):
installation = graphene.Field(
InstallationType,
device_id=graphene.Int(required=True)
)
all = graphene.Field(
AllDataType,
serial=graphene.Float(required=True)
)
def resolve_installation(self, info, device_id):
response = api_call('installations/?device=%s' % device_id)['results'][0]
return json2obj(json.dumps(response))
def resolve_all(self, info, serial):
response = api_call('devices/?serial=%s' % serial)['results'][0]
return json2obj(json.dumps(response))
The query I need to do is like this:
query {
all(serial:201002000856){
device{
id
serial
reference
}
installation{
company
device
}
}
}
So, my problem is how to make a relation with this two types, as described in AllDataType
, the resolve_installation
needs a device id
and resolve_all
needs a serial number of device.
To resolve the installation I need the device id
returned by resolve_all
resolver.
How can I achieve this?
Solution
The resolve_
methods in Query
need to return the right type of data, as defined in Query
. For example, resolve_all
should return an AllDataType
object. So you'll need to take the results of your api_call
method to build AllDataType
and InstallationType
objects. Here's an example with some made up methods to get the device and installation from the REST-fetched data:
def resolve_all(self, info, serial):
response = api_call('devices/?serial=%s' % serial)['results'][0]
# need to process the response to get the values you need
device = get_device_from_response(response)
installation = get_installation_from_response(response)
return AllDataType(device=device, installation=installation)
You might also need to add resolve methods to your Type classes. There's an example here.
Answered By - Mark Chackerian
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.