Graphene: "Expected a value of type XXX but received: ..."

Viewed 1254

I'm using graphene-django to build my API. I have a DjangoObjectType named StoreType which represents the model Store. This model has a MultiSelectField named opening_days that indicates what days in the week the store is open. To create new stores, I use this mutation:

class Weekdays(graphene.Enum):
    MO = "Mo"
    TU = "Tu"
    WE = "We"
    TH = "Th"
    FR = "Fr"
    SA = "Sa"
    SU = "Su"

class CreateStore(graphene.Mutation):
    store = graphene.Field(StoreType)

    class Arguments:
        opening_days = graphene.Argument(graphene.List(Weekdays))

    def mutate(self, info, opening_days):
        store = Store(opening_days=opening_days)
        store.save()
        return CreateStore(store=store)

The mutation works perfectly. But then, when I try to query a store, I get the error "Expected a value of type \"StoreOpeningDays\" but received: Monday, Tuesday", which makes sense really, because this field saves the data as a single string with the values separated by commas. The issue is that graphene is expecting the list specified in graphene.List(Weekdays) which is impossible to retrieve.

Any ideas on how to fix this? Thanks in advance!

1 Answers

You have a line that reads:

return CreateStore(store=store)

The lowercase of the store variable on both sides of the = leads me to believe that either the outbound or inbound information points to itself.

Though not familiar with graphene-django...

  • check the scope of the store variable. If the left side is meant to reference the parameter name from the CreateStore function, then trying renaming the right side to ensure that they are not conflicting.

  • Does the CreateStore parameter need to be prefixed with a special character so the program knows it is directly referencing the parameter name as defined in the CreateStore function? Just like an SQL command parameter in C#:

    cmd.Parameters.AddWithValue("@myparmname", mylocalval);
    
Related