graphql-python / graphene-pydantic

Integrate GraphQL with your Pydantic models
Other
224 stars 45 forks source link

Support for interfaces #106

Open alexamenta opened 3 months ago

alexamenta commented 3 months ago

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.