我在 Django 中使用 Graphene 作为后端。
我有如下查询:
class Query(object):
constructor_info = graphene.Field(UnTypedDataType, vin=graphene.String(required=True))
def resolve_constructor_info(self, info, **kwargs):
vin = kwargs.get('vin')
locale = info.context.LANGUAGE_CODE
constructor_info = get_kp_dict(vin, locale)
options = []
for option in constructor_info["vehicle"]["vehicleEquipment"]:
options.append(
{
'code': option['codeEquipment'],
'description': option['equipmentDescription']
}
)
vehicle = {
'model_year': constructor_info["vehicle"]['voExterior']['modelYear'],
'short_description': constructor_info["vehicle"]['voExterior']['shortModelTypeDescription'],
'long_description': "",
'exterior': "",
'interior': constructor_info["vehicle"]['voExterior']['descriptionUpholstery'],
'places': constructor_info["vehicle"]['voExterior']['numberOfPlaces'],
'doors': constructor_info["vehicle"]['voExterior']['numberOfDoors'],
'catalog_price': constructor_info["vehicle"]['voExterior']['priceCatalogueWithoutOptions'],
'catalog_price_with_options': constructor_info["vehicle"]['voExterior']['priceCatalogue'],
'first_registartion': constructor_info["vehicle"]['entryDate'],
'vin': constructor_info["vehicle"]['chassisNumber'],
'commission': constructor_info["vehicle"]['commissionNumber'],
'power_kw': constructor_info["vehicle"]['powerKW'],
'model_code': constructor_info["vehicle"]['modelCode'],
'options': options
}
return UnTypedDataType(data=vehicle)
而UnTypedDataType如下:
class UnTypedDataType(graphene.ObjectType):
data = graphene.Field(UnTypedObject)
在前端,我得到的结果为:
const GET_CONSTRUCTOR_INFO = gql`
query getConstructorInfo($vin: String!) {
constructorInfo(vin: $vin) {
data
}
}
`;
在data
我得到车辆对象。
但是,有没有办法删除数据并获得如下结果:
const GET_CONSTRUCTOR_INFO = gql`
query getConstructorInfo($vin: String!) {
constructorInfo(vin: $vin) {
model_year
short_description
long_description
... THE REST ...
}
}
`;