0

I'm using Graphene in Django as a backend.

I have Query as follows:

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)

And UnTypedDataType is as follows:

class UnTypedDataType(graphene.ObjectType):
    data = graphene.Field(UnTypedObject)

On the frontend I get result as:

const GET_CONSTRUCTOR_INFO = gql`
  query getConstructorInfo($vin: String!) {
    constructorInfo(vin: $vin) {
      data
    }
  }
`;

And in data I get the vehicle object.

But, is there any way to remove data and to get the result as follows:

const GET_CONSTRUCTOR_INFO = gql`
      query getConstructorInfo($vin: String!) {
        constructorInfo(vin: $vin) {
          model_year
          short_description
          long_description
          ... THE REST ...
        }
      }
    `;
Boky
  • 11,554
  • 28
  • 93
  • 163

1 Answers1

0

In case someone else need it, I solved the problem as follows.

I've changed constructor_info in the query to:

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

And added ConstructorInfoType class:

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

    class Meta:
        default_resolver = dict_resolver

And then on the React side I could use it as follows:

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

I hope it will help someone.

Boky
  • 11,554
  • 28
  • 93
  • 163