strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
https://strawberry.rocks/docs/django
MIT License
404 stars 117 forks source link

Unable to access model's foreign key field directly in Strawberry type #196

Closed feardeers closed 1 year ago

feardeers commented 1 year ago

Currently using strawberry-django-plus

Example django model:

class CastHistory(models.Model):
    id = models.BigAutoField(primary_key=True)
    person = models.ForeignKey(
        "person.Person", on_delete=models.CASCADE, blank=True, null=True
    )
    character_name = models.CharField(max_length=200)

Example strawberry type class:

from strawberry_django_plus import gql

@gql.django.filters.filter(models.CastHistory, lookups=True)
class CastHistoryFilter:
    id: gql.ID
    person_id: gql.ID

@gql.django.type(models.CastHistory, filters=CastHistoryFilter)
class CastHistory:
    id: gql.ID
    person_id: gql.ID = gql.django.field(field_name="person_id")
    character_name: auto

Query:

query MyQuery {
  castHistoryDetails(filters: {personId: "32"}) {
    personId
    characterName
    title
  }
}

Result:

{
  "data": null,
  "errors": [
    {
      "message": "Expected code to be unreachable",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "castHistoryDetails"
      ]
    }
  ]
}

Expectation:

{
  "data": {
    "castHistoryDetails": [
      {
        "personId: "32",
        "characterName": "Arwen",
        "title": "The Lord of the Rings: The Return of the King"
      },
      {
        "personId: "32",
        "characterName": "Arwen",
        "title": "The Lord of the Rings: The Two Towers"
      },
      {
        "personId: "32",
        "characterName": "Arwen",
        "title": "The Lord of the Rings: The Fellowship of the Ring"
      }
    ]
  }
}

Upvote & Fund

Fund with Polar

bellini666 commented 1 year ago

Hey @feardeers ,

strawberry django went through a major refactor recently, and a lot of improvements have been done for it and strawberry itself.

Does this issue still happen on the latest version?

AriAbr commented 10 months ago

I am still seeing this issue on V0.22.0. @feardeers @bellini666 were you able to figure out what caused this / how to fix it?

bellini666 commented 10 months ago

Hi @AriAbr ,

This report is really old and the example is still using strawberry-django-plus.

Could you share more details about your issue? Some reproduction examples and tracebacks would be nice as well :)

I can reopen this issue if you want to share it here or open a new issue if you prefer