strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
4.01k stars 533 forks source link

Support for non-scalar default values is broken #1483

Open patrick91 opened 2 years ago

patrick91 commented 2 years ago

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

Fund with Polar

coady commented 2 years ago

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:
david-appliqloud commented 8 months ago

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 👍