question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Schema incorrectly typing ManyToMany field

See original GitHub issue

Hi,

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:closed
  • Created 6 years ago
  • Reactions:6
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

14reactions
grimborgcommented, May 17, 2017

Adding a resolver for the m2m field may be a simpler workaround.

class BookingType(DjangoObjectType):
    people = graphene.List(PersonType)

    @graphene.resolve_only_args
    def resolve_people(self):
        return self.people.all()

    class Meta:
        model = Booking
6reactions
LegoStormtrooprcommented, May 4, 2017

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:

class Person(models.Model):
    name = models.CharField(max_length=100, blank=False)

class BookingPersonRelation(models.Model)
    person = models.ForeignKey('Person')
    booking = models.ForeignKey('Booking')

class Booking(models.Model):
    people = models.ManyToManyField(Person, through=BookingPersonRelation, related_name="bookings")

I got the resolution to work, only once the intermediate model had a type

class BookingPersonRelationType(DjangoObjectType):
    class Meta:
        model = BookingPersonRelationType

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found