-
Notifications
You must be signed in to change notification settings - Fork 765
Feature Request: DjangoInterface #1366
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
Comments
You might find some inspiration for that here: |
I have been working on an implementation of poylmorphic models using graphene_django in the context of the ActivityStreams spec. Based on this discussion I have created a DjangoInterface that mirror the DjangoObjectType definitions and general Meta api. The only change I have found can be necessary is the removal of the "interfaces" meta prop, because of this - in other words interface extension doesn't seem widely supported and/or is not documented a lot. There is additional change needed in the DjangoConnectionField, where we need to update an assertion to In usage, it is important that a registry is used, such as
... where Object is the base polymorphic model to be mapped to the Interface. This approach works even if more testing is necessary. One limitation is that Interface composition doesn't seem very easy, and that there are a few glitches when using the relay Node interface as well I have not taken a deep look at. At the moment, since this is not part of the graphene_django library, I also needed to overwrite the single_dispatch converter function for relation fields, so that a custom connection field can be used to solve the assertion fail in the default DjangoConnectionField. I would love to hear your thoughts on this approach and the viability of a PR that I will be happy to submit for consideration. |
Is your feature request related to a problem? Please describe.
One of the main benefits of
graphene_django
is the de-duplication in field types and descriptions for model fields. However, it's common to have polymorphic models, which are more well suited to GraphQL interfaces. For example, the shape of a JSON field could vary depending on atype
field.There doesn't seem to be a good way to import model field info into interfaces like there is for ObjectTypes at the moment.
Describe the solution you'd like
It would be great if there was a
DjangoInterface
class which could be used for defining interfaces similarly toDjangoObjectType
. This would allow for the following:In this case,
Employee
would have the fields id, name, birthday, job_title, and company.BasePerson
would have only id, name, and birthday.Describe alternatives you've considered
Another option would be to tie all implementations of an interface to the same Django model. But using the model only to generate the field definitions and keeping the
DjangoObjectType
interface the same seems both simpler and more flexible.Right now, the closest approach seems to be something like:
This works okay but is pretty hacky, relying on private interfaces and not working for relation fields.
The text was updated successfully, but these errors were encountered: