Skip to content
Advertisement

How to use a django abstract class with graphene-django?

I’m trying to have a unique interface for two concrete classes that are similar and inherit from a common abstract class.

My django model classes:

JavaScript

My graphene-django api:

JavaScript

When querying the graphql mutation corresponding to CreateMetadata, the metadata concrete class is successfully created. ;-)

The problem is that when the query asks for the created concrete Metadata in the result (here either MetadataScalar or MetadataChoices), graphql cannot find a node for the concrete class and outputs the following error message:

JavaScript

For your information, here is one example query:

JavaScript

How to make it work nicely, without having to specify two different result types (metadataScalar and metadataChoices variables) in the second part of the query?

Advertisement

Answer

You can use Union in order to be able to specify multiple different result classes.

In your case, that woud be:

JavaScript

The graphql query wil look like:

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement