Skip to content

Nested PydanticInputObjectType #104

Open
@delemeator

Description

@delemeator

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?

Activity

iromanov-nk

iromanov-nk commented on Nov 22, 2024

@iromanov-nk

If you add A model input BEFORE BInput, your code will work. In my opinion, do not use this package in production!

full code example:

from pydantic import BaseModel
from graphene_pydantic import PydanticInputObjectType
import graphene

class A(BaseModel):
    x: str

class B(BaseModel):
    a: A

class AInput(PydanticInputObjectType):
    class Meta:
        model = A

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

BInput.resolve_placeholders()
c-xlenz

c-xlenz commented on Nov 22, 2024

@c-xlenz

I am not sure, if this solves the problem.

If you run the minimalistic example from #107
`
class A(pydantic.BaseModel):
x: str | None = None
y: list["A"] | None = None

class P_A(PydanticInputObjectType):
class Meta:
model = A

P_A.resolve_placeholders()
`
I think, there is a method called incorrectly in the code.
register_object_field() got an unexpected keyword argument 'model'

this method has clearly no argument model... If you take out the keyword from the call, it is working.
Maybe consider to check this part of the code and see if it can be hotfixed?!

Thanks

linked a pull request that will close this issue on Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Nested PydanticInputObjectType · Issue #104 · graphql-python/graphene-pydantic