0

我在 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 ...
        }
      }
    `;
4

1 回答 1

0

如果其他人需要它,我解决了如下问题。

我已将constructor_info查询更改为:

constructor_info = graphene.Field(ConstructorInfoType, vin=graphene.String(required=True))

并添加了ConstructorInfoType类:

class ConstructorInfoType(graphene.ObjectType):
    model_year = graphene.String()
    short_description = graphene.String()
    long_description = graphene.String()
    ...THE REST...

    class Meta:
        default_resolver = dict_resolver

然后React我可以按如下方式使用它:

const GET_CONSTRUCTOR_INFO = gql`
  query getConstructorInfo($vin: String!) {
    constructorInfo(vin: $vin) {
      modelYear
      shortDescription
      ...THE REST...
    }
  }
`;

我希望它会帮助某人。

于 2019-10-15T06:20:34.413 回答