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

1

10

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!

Strake answered 28/7, 2020 at 19:56 Comment(2)
Can you share the code for the Django model and also for StoreType? If the data is being stored in the db as expected you should just need to create a custom resolver for the opening_days field on StoreType to return the correct type.Mckoy
Please share the Store modelPlace
D
-1

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);
    
Difficile answered 12/4, 2021 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.