graphql-python / graphene-sqlalchemy

Graphene SQLAlchemy integration
http://docs.graphene-python.org/projects/sqlalchemy/en/latest/
MIT License
974 stars 225 forks source link

How to remove "edges" and "node" from query? #381

Closed MattKleinsmith closed 1 year ago

MattKleinsmith commented 1 year ago

I would like to be able to use syntax like this:

image

But instead I must do something like this:

{
    pets {
        edges {
            node {
                name
            }
        }
    }
}

Another example:

For e.g., now: query { authors { edges { node { id name books { edges { node { id name } } } } } } } What i want: query { authors { id name books { id name } } }

From another person wanted the same thing here: https://github.com/graphql-python/graphene-sqlalchemy/issues/374

Will keep poking at examples and the code to try to find a way. Which package is responsible for the edges-node syntax? Is it graphene-sqlalchemy or graphene itself?

MattKleinsmith commented 1 year ago

https://github.com/graphql-python/graphene/issues/1073

MattKleinsmith commented 1 year ago

Relay wants it:

https://relay.dev/graphql/connections.htm

To conform with pagination best practices:

https://graphql.org/learn/pagination/

MattKleinsmith commented 1 year ago

Re-opening because I'd like to know how to turn off pagination, if it's possible. The edge-node syntax makes sense to me for relationships, but I would like to turn it off for basic queries, like getting all product prices.

Instead of this:

{
    "data": {
        "allProducts": {
            "edges": [
                {
                    "node": {
                        "price": 39.99
                    }
                },
                {
                    "node": {
                        "price": 49.95
                    }
                }
            ]
        }
    }
}

Would prefer:

{
    "data": {
        "allProducts": [
                {
                    "price": 39.99
                },
                {
                    "price": 49.95
                }
          ]
    }
}
MattKleinsmith commented 1 year ago

Okay, I am learning. This is a feature of Relay.

Here's the mental model that Relay wants:

image

And here's how they implement it in GraphQL (which doesn't allow properties on edges):

image

https://relay.dev/docs/tutorial/connections-pagination/

MattKleinsmith commented 1 year ago

This diagram clicks even more with the graphene-sqlalchemy behavior I'm seeing:

image

erikwrede commented 1 year ago

Hey Matt, good to see that you've familiarized yourself with the Relay spec 😊 I agree that it is confusing at first, but once you've got the intuition, it makes a lot more sense- However, I think you've got a point with your statements. Sometimes you don't need all the pagination info but just want to get the benefits of a connection (e.g. filtering or other constraints), but don't need the edges. Currently prototyping an optional nodes field on the connections:

{
    pets {
        nodes {
              name
        }
    }
}

This way you can get rid of the additional nesting layer in cases where you really don't need it. Please LMK if you're interested in that, so I can tag you for PR review once it's ready in a few weeks. 🙂

If you have some specific points of that could be improved on the graphene-sqlalchemy docs, I'd appreciate any PRs bringing improvements.

Cheers

MattKleinsmith commented 1 year ago

Hi @erikwrede, thank you for replying. Yes, please tag me in the PR.

erikwrede commented 1 year ago

Hi @MattKleinsmith, the first PR is here: https://github.com/graphql-python/graphene/pull/1499 Another PR will be necessary to add native support in graphene-sqlalchemy, but this change enables us to work with the native graphene-relay Connections. Please LMK what you think :)

github-actions[bot] commented 10 months ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related topics referencing this issue.