I've the following model:
class TripSuggestedGadget(models.Model):
trip = models.ForeignKey(
Trip,
related_name='suggested_gadgets'
)
type = models.CharField(
max_length=50,
choices=SUGGESTED_GADGET_TYPES
)
description = models.TextField()
Here are the choices referred above:
SUGGESTED_GADGET_TYPES = (
('camera', u'C谩mara'),
('flash', u'Flash'),
('tripod', u'Tr铆pode'),
('umbrella', u'Sombrilla'),
)
In the graph response here's what I'm getting:
...
"edges": [
{
"node": {
"id": "VHJpcFN1Z2dlc3RlZEdhZGdldE5vZGU6MQ==",
"type": "CAMERA",
"description": "C谩mara de formato medio",
"__typename": "TripSuggestedGadgetNode"
},
"__typename": "TripSuggestedGadgetNodeEdge"
},
{
"node": {
"id": "VHJpcFN1Z2dlc3RlZEdhZGdldE5vZGU6Mg==",
"type": "FLASH",
"description": "Flash",
"__typename": "TripSuggestedGadgetNode"
},
"__typename": "TripSuggestedGadgetNodeEdge"
},
{
"node": {
"id": "VHJpcFN1Z2dlc3RlZEdhZGdldE5vZGU6Mw==",
"type": "TRIPOD",
"description": "Tr铆pode liviano para poderlo llevar",
"__typename": "TripSuggestedGadgetNode"
},
"__typename": "TripSuggestedGadgetNodeEdge"
}
]
...
As you may notice my choices are lowercased, but response uppercased.
I've seen @BrianChapman comment in #67 regarding the same problem, but preferred to open this new one because #67 is not directly related.
Hi @cristiangrojas,
The main reason choice enums are uppercase is because are following recommended styling in GraphQL (all Enums in the provided examples and demos are always uppercase).
As the Enum values are served as "strings" over the wire it helps to identify them.
See example: http://graphql.org/graphql-js/type/#example-4
Is there a way to change this behavior, if my application requires case-sensitive choices? (e.g. with tripod, triPod and TRIPOD having three different meanings in the Django model?)
Right now, your best bet is to override the type and make your own graphene.Enum.
MyEnum = graphene.Enum(
'MyEnum',
[(value, value) for value, _ in SOME_DJANGO_MODEL_CHOICES]
)
class MyObject(DjangoObjectType):
some_choices_field = MyEnum()
I have been thinking about how we can solve this problem without breaking backwards compatibility, and I have not yet found a clear answer. Especially after seeing your case of three different values that resolve to the same thing after going through to_const.
Maybe someone adds GRAPHENE_CONVERT_CHOICES_TO_ENUMS = False setting to graphene_django once.
Until then, you can opt out with monkey-patch somewhere before schema is imported, for example in settings file.
Patch: https://gist.github.com/denis-ryzhkov/fcb944f6ebfad4775efb74848703f0d7
https://github.com/graphql-python/graphene-django/issues/133 seems to be related.
Adding the field as a String, seems to be enough to solve the problem.
class MyObject(DjangoObjectType):
type = graphene.String()
Just ran into this issue too and was very confused that Graphene was forcing all my enum string values to uppercase. Given that Django itself treats enum strings used as choices as being case-sensitive, and doesn't force them to be uppercase, I think it's important that Graphene doesn't try to force them to be uppercase.
If GraphQL recommends string constants be uppercase, perhaps it's worth mentioning this as a suggestion in the Graphene docs somewhere, but I think the choice needs to be left to the developer.
In the meantime, @Hispar's fix of marking the field as being a String() is working for me.
Discussion is focusing on #67 so closing this to avoid duplicate conversations.
Most helpful comment
Adding the field as a String, seems to be enough to solve the problem.