strawberry-graphql / strawberry-django

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

Relay and model relationships #263

Closed salcedo closed 1 year ago

salcedo commented 1 year ago

How to handle relationships with Relay implementation?

@strawberry.type
class Post(relay.Node):
    id: relay.NodeID[int]
    body: auto
    comments: List["Comment"] # This doesn't seem to be correct.
    comments: relay.ListConnection["Comment"] # Nor this.

Upvote & Fund

Fund with Polar

bellini666 commented 1 year ago

Hey @salcedo ,

What kind of relationship are you trying to create? Can you explain it a bit more?

salcedo commented 1 year ago

Hi @bellini666 . Apologies for taking up anyone's time with this. Thank you for your response.

Assuming the standard Post with Comment Django models examples (where Comment model has a ForeignKey to Post and thus a Post can have comments), is there currently support for this with Relay?

And also apologies for the XY, but the problem I'm trying to solve here is getting a total count of records in the database along with the response. With strawberry-django-plus, I can use ListConnectionWithTotalCount, but that appears to only work when using Relay, and I think it works well.

Currently my app works great with all of the nodes and edges WITHOUT relay, but it lacks the ability to return a total count (accounting for any filters applied). Using Relay seems like overkill because our front-end is Vue.js, but I am willing to dirty-hack our front-end to utilize "Relay" simply for access to this much needed totalCount. However, I cannot figure out how to make the types and queries under Relay return consistent edges { node { ... } } structures while nesting down through relationships.

Thus, the tl;dr: When using Relay - We can get a total count. But working with relationships of django models are lacking documentation or examples. When not using Relay - Everything except being able to get a total count works great.

bellini666 commented 1 year ago

Apologies for taking up anyone's time with this

No need to apologize :)

Thus, the tl;dr: When using Relay - We can get a total count. But working with relationships of django > models are lacking documentation or examples. When not using Relay - Everything except being able to get a total count works great.

Yeah, there's actually a lot of things missing documentation/examples T_T

Regarding totalCount not working, it has a special handling there. Could you share an example of what you are trying to do so maybe I can spot what might be wrong?

salcedo commented 1 year ago

Will continue this in #266