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

Nested PydanticInputObjectType #104

Open
delemeator opened this issue May 1, 2024 · 0 comments
Open

Nested PydanticInputObjectType #104

delemeator opened this issue May 1, 2024 · 0 comments

Comments

@delemeator
Copy link

Hello,

I wanted to use nested pydantic.BaseModel with PydanticInputObjectType, but I struggle to make it work

Something like this:

from pydantic import BaseModel
from graphene_pydantic import PydanticInputObjectType
import graphene

class A(BaseModel):
    x: str

class B(BaseModel):
    a: A

class BInput(PydanticInputObjectType):
    class Meta:
        model = B

class createB(graphene.Mutation):
    class Arguments:
        b = BInput()

    output = graphene.String()

    def mutate(self, info, b):
        return "42"

class Mutation(graphene.ObjectType):
    createB = createB.Field()

schema = graphene.Schema(mutation=Mutation)

Gives me:

Traceback (most recent call last):
  File "[PATH]/lib/python3.9/site-packages/graphene/types/schema.py", line 122, in add_type
    name = graphene_type._meta.name
AttributeError: 'Placeholder' object has no attribute '_meta'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "[PATH]/lib/python3.9/site-packages/graphql/type/definition.py", line 1456, in fields
    fields = resolve_thunk(self._fields)
  File "[PATH]/lib/python3.9/site-packages/graphql/type/definition.py", line 300, in resolve_thunk
    return thunk() if callable(thunk) else thunk
  File "[PATH]/lib/python3.9/site-packages/graphene/types/schema.py", line 312, in create_fields_for_type
    field_type = create_graphql_type(field.type)
  File "[PATH]/lib/python3.9/site-packages/graphene/types/schema.py", line 120, in add_type
    return GraphQLNonNull(self.add_type(graphene_type.of_type))
  File "[PATH]/lib/python3.9/site-packages/graphene/types/schema.py", line 124, in add_type
    raise TypeError(f"Expected Graphene type, but received: {graphene_type}.")
TypeError: Expected Graphene type, but received: Placeholder(<class '__main__.A'>).

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[PATH]/lib/python3.9/site-packages/graphene/types/schema.py", line 440, in __init__
    self.graphql_schema = GraphQLSchema(
  File "[PATH]/lib/python3.9/site-packages/graphql/type/schema.py", line 226, in __init__
    collect_referenced_types(mutation)
  File "[PATH]/lib/python3.9/site-packages/graphql/type/schema.py", line 435, in collect_referenced_types
    collect_referenced_types(arg.type)
  File "[PATH]/lib/python3.9/site-packages/graphql/type/schema.py", line 438, in collect_referenced_types
    for field in named_type.fields.values():
  File "[PATH]/lib/python3.9/functools.py", line 993, in __get__
    val = self.func(instance)
  File "[PATH]/lib/python3.9/site-packages/graphql/type/definition.py", line 1459, in fields
    raise cls(f"{self.name} fields cannot be resolved. {error}") from error
TypeError: BInput fields cannot be resolved. Expected Graphene type, but received: Placeholder(<class '__main__.A'>).

I tried using resolve_placeholders, but then I get an error:

from pydantic import BaseModel
from graphene_pydantic import PydanticInputObjectType
import graphene

class A(BaseModel):
    x: str

class B(BaseModel):
    a: A

class BInput(PydanticInputObjectType):
    class Meta:
        model = B

BInput.resolve_placeholders()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "[PATH]/lib/python3.9/site-packages/graphene_pydantic/inputobjecttype.py", line 152, in resolve_placeholders
    meta.registry.register_object_field(
TypeError: register_object_field() got an unexpected keyword argument 'model'

My versions are:

graphene==3.3
pydantic==2.7.1
graphene_pdantic==0.6.1

Is there any way to use PydanticInputObjectType in nested case?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant