profusion / sgqlc

Simple GraphQL Client
https://sgqlc.readthedocs.io/
ISC License
513 stars 85 forks source link

Using None produces empty object for input types #200

Closed madiganz closed 2 years ago

madiganz commented 2 years ago

Description:

For input types that have a field that is another input type, when setting the field to None, it produces an empty object. This causes the GraphQL to fail when it needs to be an object with required properties, or null.

Reproducible example:

import sgqlc.types

test_schema = sgqlc.types.Schema()

class TestInput(sgqlc.types.Input):
    __schema__ = test_schema
    __field_names__ = ("field1",)

    field1 = sgqlc.types.Field("FooInput", graphql_name="field1")

class FooInput(sgqlc.types.Input):
    __schema__ = test_schema
    __field_names__ = ("name",)

    name = sgqlc.types.Field(
        sgqlc.types.non_null(sgqlc.types.String), graphql_name="name"
    )

input = TestInput(field1=None)

print(TestInput.__to_graphql_input__(input))

Output: {field1: {}}

barbieri commented 2 years ago

@madiganz took me bit more than anticipated since the input handling was also broken in some other cases, such as passing [None] in a list of non-nullables wasn't raising an exception.

I hope I did cover all the cases in the doctests, also made the code more uniform to avoid inconsistencies.

Let me know if #202 works for you, then I can merge that PR. The GitHub actions confirmed it works in all supported Python versions.

madiganz commented 2 years ago

@barbieri no problem and thanks for getting this done so quickly. Your PR looks good to me

barbieri commented 2 years ago

https://pypi.org/project/sgqlc/16.0/ thanks!