Schema incorrectly typing ManyToMany field
See original GitHub issueHi,
I’m new to graphene (and django, really) but have come across a stumbling block.
I have the following django models:
class Person(models.Model):
name = models.CharField(max_length=100, blank=False)
class Booking(models.Model):
people = models.ManyToManyField(Person, related_name="bookings", blank=False)
One person has many bookings; one booking has many people.
Then I have the following object types in my graphene schema:
class PersonType(DjangoObjectType):
class Meta:
model = Person
class BookingType(DjangoObjectType):
class Meta:
model = Booking
class Query(graphene.AbstractType):
all_people = graphene.List(PeopleType)
all_bookings = graphene.List(BookingType)
def resolve_all_people(self, *args, **kwargs):
return Person.objects.all()
def resolve_all_bookings(self, *args, **kwargs):
return Booking.objects.all()
everything else is boilerplate from the graphene/django getting started tutorials.
When I make my query with graphiql:
{
allBookings {
people {
id
}
}
}
I get the following error:
{
"errors": [
{
"message": "Expected value of type \"BookingType\" but got: Person.",
"locations": [
{
"line": 3,
"column": 5
}
]
}
],
"data": {
"allBookings": [
{
"people": [
null
]
}
]
}
}
Any ideas what i’m doing wrong? all my other foreign key (one-to-many) relations work fine out of the box. Also; if I query for a person’s bookings then it works fine too. Just when I query for a booking’s people do I get the error.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Assigning Value(s) to ManyToMany Field - Stack Overflow
I'm trying to assign a value onto a ManyToMany field in the Order class. ... The problem with this schema is that the...
Read more >The right way to use a ManyToManyField in Django
A detailed explanation of how a many-to-many relationship works in Django and all the caveats associated with using the ManyToManyField.
Read more >ManyToMany field creation problem - Google Groups
in Property table. After adding the field, I tried doing a schemamigration, but output says "No change has been done".
Read more >Creating forms from models - Django documentation
If you're building a database-driven app, chances are you'll have forms that map ... A model ManyToManyField is represented as a MultipleChoiceField ....
Read more >Video: Create many-to-many relationships - Microsoft Support
Create fields in the intermediate table · In Datasheet View, select the ID column heading and then type the new name for the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Adding a resolver for the m2m field may be a simpler workaround.
I don’t think you are doing anything wrong. I came up against a similar issue, however I was using a
through
model. Take this terribly contrived example:I got the resolution to work, only once the intermediate model had a type
Long term, I’m not sure what the resolution is - but if you can figure out how to get the generated ManyToMany model, you can probably create a type over that. I just started using this, but will no doubt run into the same problem and will come back with more info.