graphql-python / graphql-python/graphene-pydantic

Nested PydanticInputObjectType

Open
#104 2 comments 3 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
249
Forks
47
PR merge metrics
No merged PRs in 30d

Description

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().

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

Traceback (most recent call last):
File "", line 1, in
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().
```

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 "", line 1, in
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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.