sgqlc icon indicating copy to clipboard operation
sgqlc copied to clipboard

Presenting `Type`s as dictionaries does not include aliased fields

Open simonrw opened this issue 3 years ago • 1 comments

With the operation

op = Operation(Query, name="Foo")
op.foo(__alias__="bar").value
op.baz()
print(op)
# query Foo {
#   bar: {
#     value
#   }
#   baz
# }

and basic schema:

class Foo(Type):
    value = Field(str, graphql_name="value")

class Query(Type):
    foo = Field(Foo, graphql_name="foo")
    baz = Field(str, graphql_name="baz")

when I perform a query against this, which returns

{
  "data": {
    "baz": "value baz",
    "bar": {
        "value": "value test"
    }
  }
}

and try to convert the resulting type to dictionary:

print(op + endpoint(op))
# Query(bar=Foo(value=value test), baz=value baz)

print((op + endpoint(op)).__to_json_value__())
# {"baz": "value baz"}

the bar field is missing.

I don't know if there's a more suitable way of converting the resulting type to a dict. There is the __json_data__ field, which contains the response from the server, which does contain the baz field - is there a preferred way of doing this operation?

simonrw avatar Aug 29 '21 22:08 simonrw

I have been hitting the same issue. Also it is worth to mention that json_data won't work with nested aliased fields. So, it returns the aliased fields for things only at the same level you're calling from.

grazzolini avatar Sep 27 '21 19:09 grazzolini