strawberry-graphql / strawberry-django

Strawberry GraphQL Django extension
MIT License
391 stars 115 forks source link

Global ID mapping does not work with manual id assignment on type in relay django #541

Open Elawphant opened 3 weeks ago

Elawphant commented 3 weeks ago

According to documentation something like this

@strawberry_django.type(Author, filters=AuthorFilter, order=AuthorOrder)
class AuthorNode(strawberry.relay.Node):
    id: strawberry.relay.GlobalID = strawberry_django.field()
    books_connection: ListConnectionWithTotalCount[Annotated["BookNode", strawberry.lazy(
        "graph_api.gql.nodes.book_node"
    )]] = strawberry_django.connection(
        field_name="books",
        extensions=[IsAuthenticated()],
    )

The response comes with ids with pk values

   {
  "data": {
    "authorsConnection": {
      "edges": [
        {
          "node": {
            "id": "1",
          }
        },
        {
          "node": {
            "id": "2",
          }
        }
      ],
      "totalCount": 2
    }
  }
}

However this works properly

@strawberry_django.type(Author, filters=AuthorFilter, order=AuthorOrder, fields=["id"]) # note that I have set the id via fields parameter instead of assigning it as a field
class AuthorNode(strawberry.relay.Node):
    books_connection: ListConnectionWithTotalCount[Annotated["BookNode", strawberry.lazy(
        "graph_api.gql.nodes.book_node"
    )]] = strawberry_django.connection(
        field_name="books",
        extensions=[IsAuthenticated()],
    )
bellini666 commented 3 weeks ago

Hey @Elawphant ,

When using @strawberry_django.type and inheriting the type from Node it will automatically have an id: GlobalID with a custom resolver that will return the correct value in it.

Is there a reason to why you are trying to define it yourself?

Elawphant commented 3 weeks ago

The reason was to keep the types defined with all fields the same way. I made an addon on top of strawberry_django which makes the modules from the models. Here's the link Strawberry Jam 🍓 @bellini666 I would appreciate some review/feedback on the generated modules, whether they are providing configurations in accordance with the best practices for strawberry django. Also I want to make the tests generation.

bellini666 commented 3 weeks ago

@Elawphant ahhh I see.

I need to think about how to workaround that properly. In the meantime, you can probably skip the id field when creating a type that inherits from Node.

Regarding the lib, very interesting! Will take a look at it as soon as I can 😊