Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphQL Any type return value no longer mapped to JSON in 0.17 #316

Closed
thomascobb opened this issue Jan 7, 2022 · 1 comment · Fixed by #317
Closed

GraphQL Any type return value no longer mapped to JSON in 0.17 #316

thomascobb opened this issue Jan 7, 2022 · 1 comment · Fixed by #317
Labels
bug Something isn't working

Comments

@thomascobb
Copy link
Contributor

The following code:

from dataclasses import dataclass
from typing import Any, Optional

from apischema import serialize
from apischema.graphql import graphql_schema
from graphql import print_schema

@dataclass
class Foo:
    bar: str
    foo: Optional["Foo"]

def serialize_foo(foo: Foo) -> Any:
    return serialize(foo)

def test_schema():
    schema = graphql_schema(query=[serialize_foo])
    assert (
        print_schema(schema)
        == """\
type Query {
  serializeFoo(foo: FooInput!): JSON!
}

scalar JSON @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")

input FooInput {
  bar: String!
  foo: FooInput
}
"""
    )

used to work in 0.16.6, but fails in 0.17.0:

Traceback (most recent call last):
  File "/home/tom/Programming/scanspec/tests/test_graphql_any.py", line 20, in test_schema
    schema = graphql_schema(query=[serialize_foo])
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/utils.py", line 397, in wrapper
    return wrapped(*args, **kwargs)
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/graphql/schema.py", line 1033, in graphql_schema
    query=root_type("Query", query_fields),
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/graphql/schema.py", line 1030, in root_type
    return output_builder.object(tp, (), fields).merge(type_name, None).raw_type  # type: ignore
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/graphql/schema.py", line 225, in wrapper
    factory = method(self, *args, **kwargs)
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/graphql/schema.py", line 759, in object
    self._resolver(tp, resolver_field),
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/graphql/schema.py", line 640, in _resolver
    resolver_resolve(
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/graphql/resolvers.py", line 307, in resolver_resolve
    serialize_result = method_factory(types["return"]).serialize
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/graphql/resolvers.py", line 108, in factory
    return PartialSerializationMethodVisitor(
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/conversions/visitor.py", line 96, in visit_with_conv
    return self.visit(tp)
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/graphql/resolvers.py", line 97, in visit
    return METHODS[NoneType] if tp is UndefinedType else super().visit(tp)
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/recursion.py", line 175, in visit
    return super().visit(tp)
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/conversions/visitor.py", line 122, in visit
    return self.visit_conversion(tp, None, False, self._conversion)
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/serialization/__init__.py", line 509, in visit_conversion
    return super().visit_conversion(tp, conversion, dynamic, next_conversion)
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/conversions/visitor.py", line 118, in visit_conversion
    return super().visit(tp)
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/visitor.py", line 185, in visit
    return self.any()
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/serialization/__init__.py", line 278, in any
    return AnyMethod(self._factory)
  File "/home/tom/.local/share/virtualenvs/scanspec-L6XIN2FT/lib/python3.9/site-packages/apischema/graphql/resolvers.py", line 88, in _factory
    raise NotImplementedError
NotImplementedError

Is this expected?

@wyfo wyfo added the bug Something isn't working label Jan 7, 2022
@wyfo
Copy link
Owner

wyfo commented Jan 7, 2022

Indeed, this is a regression. I will fix it and release a patch tonight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants