profusion / sgqlc

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

Initializing types using field arugments #198

Closed madiganz closed 2 years ago

madiganz commented 2 years ago

The examples show that you can initialize a type using:

json_data = { 'aInt': 1, 'aFloat': 2.1 }
obj = global_schema.TypeUsingPython(json_data)

However this returns an error:

obj = global_schema.TypeUsingPython(
    a_int=1,
    a_float=2.1
)
TypeError: __init__() got an unexpected keyword argument 'a_int'

Is this the expected behavior? I would think it should be possible to initialize types with the fields and not using a dictionary.

barbieri commented 2 years ago

return/output types are not supposed to be used directly like the second case, the constructor signature is: https://github.com/profusion/sgqlc/blob/master/sgqlc/types/__init__.py#L1769

    def __init__(self, json_data, selection_list=None):

the json_data is what comes from the server, just like your first example.

However, for input types the constructor is slightly different and is to be used as you said: https://github.com/profusion/sgqlc/blob/master/sgqlc/types/__init__.py#L2619

    def __init__(self, _json_obj=None, _selection_list=None, **kwargs):

it's expected to be used like your second example.

Does it answers your question?

madiganz commented 2 years ago

Yes, this answers my question. Thanks!