strawberry icon indicating copy to clipboard operation
strawberry copied to clipboard

Support for non-scalar default values is broken

Open patrick91 opened this issue 3 years ago • 2 comments

thanks to @cache-missing for the report!

This:

import strawberry

@strawberry.input
class InputData:
    name: str = "10"

@strawberry.type
class Query:
    @strawberry.field
    def example(self, data: InputData = InputData()) -> str:
        return data.name

schema = strawberry.Schema(query=Query)

Results in an error when not passing anything to data:

argument of type 'InputData' is not iterable

GraphQL supports non scalar types as input (they also show up in the schema), while in python you'd usually use None/UNSET as default. We should support this use case, by either adding direct support for default values or by providing a default factory.

Here's an example on the playground

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

patrick91 avatar Dec 07 '21 17:12 patrick91

It can be done with an empty dict/object. The received value will still be the default input type.

    def example(self, data: InputData = {}) -> str:

coady avatar Mar 07 '22 03:03 coady

It can be done with an empty dict/object. The received value will still be the default input type.

    def example(self, data: InputData = {}) -> str:

Thank you so much! I was running into this problem when testing and this solution worked wonders. I have no idea how the default value of {} converts to the input type but it does 👍

david-appliqloud avatar Mar 07 '24 18:03 david-appliqloud