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

Support for interfaces #106

Open
alexamenta opened this issue Jun 19, 2024 · 0 comments
Open

Support for interfaces #106

alexamenta opened this issue Jun 19, 2024 · 0 comments

Comments

@alexamenta
Copy link

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.

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