Saleor: GraphQL error: Expected a value of type "WeightUnitsEnum" but received: kg

Created on 13 Mar 2019  路  2Comments  路  Source: mirumee/saleor

Steps to reproduce the problem

  1. Change the default language in the storefront to Portuguese.
  2. Go to Dashboard 2.0. The homepage runs a query which fetches defaultWeightUnit field.
    This GraphQL request returns this error: GraphQL error: Expected a value of type "WeightUnitsEnum" but received: kg
bug core graphql

Most helpful comment

Thank's for the clue! Enum values should be independent of the locale. I think we should get rid of enum.Enum occurrences and generate all enums for Graphene such as ones in saleor/graphql/core/enums.py. E.g. I quickly checked that everything works fine if I remove WeightUnitsEnum from weights.py and instead add this in graphql/core/enums.py:

WeightUnitsEnum = graphene.Enum(
    'WeightUnitsEnum',
    [(str_to_enum(unit[0]), unit[0]) for unit in WeightUnits.CHOICES])

All 2 comments

This is also true for all other languages that have translations for any weight units (kg, oz, lb, g). In site settings when you change default weight unit to something else, the GraphQl error changes to '... but received: lb/oz/g'. For example in German:
https://github.com/mirumee/saleor/blob/5b5ed728ef9d7fd4b50305f7367ca5c46fe4fcf6/locale/de/LC_MESSAGES/django.po#L605

Changing this from 'Kg' to 'kg' (or any translated line back to 'kg', 'oz' , etc) and recompiling messages fixes this GraphQL error for me, but I suppose it should work with translated lines also? It is marked with pgettext_lazy:
https://github.com/mirumee/saleor/blob/5b5ed728ef9d7fd4b50305f7367ca5c46fe4fcf6/saleor/core/weight.py#L26

But from what I can see Dashboard 2.0 isn't translated yet, because even after changing language and getting these errors I have only English in there. But I assume that just reverting these lines back to non-translated state isn't a good fix.

Could you tell me if I am on the right track here: for that query to work with translation it must be added to graphql/translations.py?

Thank's for the clue! Enum values should be independent of the locale. I think we should get rid of enum.Enum occurrences and generate all enums for Graphene such as ones in saleor/graphql/core/enums.py. E.g. I quickly checked that everything works fine if I remove WeightUnitsEnum from weights.py and instead add this in graphql/core/enums.py:

WeightUnitsEnum = graphene.Enum(
    'WeightUnitsEnum',
    [(str_to_enum(unit[0]), unit[0]) for unit in WeightUnits.CHOICES])
Was this page helpful?
0 / 5 - 0 ratings