graphql-python / graphql-python/graphene-pydantic
Support for interfaces
- Dominant language
- Python
- Stars
- 249
- Forks
- 47
- PR merge metrics
- No merged PRs in 30d
Description
I see the following comment in `PydanticObjectType.__init_subclass_with_meta__`:
```
# TODO: We don't currently do anything with interfaces, and it would
# be great to handle them as well. Some options include:
# - throwing an error if they're present, because we _can't_ handle them
# - finding a model class with that name and generating an interface
# from it
# - using the nearest common ancestor of multiple types in a Union
```
Are there any concrete plans to add support for interfaces in the near future? For my particular use case, I'd be happy with manually defining interfaces rather than deducing them. For example, I'd like to be able to do this:
```
# this model will be used to define an interface
class HasIdentityModel(pydantic.BaseModel):
name: str
id: str
class IdentityListModel(pydantic.BaseModel):
things_with_ids: list[HasIdentityModel]
class PersonModel(HasIdentityModel):
age: int
class BusinessModel(HasIdentityModel):
owner: PersonModel
# would have to define PydanticInterfaceType similarly to PydanticObjectType
class HasIdentity(PydanticInterfaceType):
class Meta:
model = HasIdentityModel
class IdentityList(PydanticObjectType):
class Meta:
model = IdentityListModel
class Person(PydanticObjectType):
class Meta:
model = PersonModel
interfaces = (HasIdentity,) # or (HasIdentityModel, ) if that's more appropriate
class Business(PydanticObjectType):
class Meta:
model = BusinessModel
interfaces = (HasIdentity,) # as above
```
and then query the `thingsWithIds` field as
```
thingsWithId: {
name
id
...on Person {
age
}
...on Business {
owner {
name
}
}
}
```
without having to put the `name` and `id` fields on the `Person` or `Business` fragments.
Contributor guide
Assessment
This issue has not been assessed yet.