Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposing a simple list of the selection set in the resolve function #458

Closed
papigers opened this issue Jul 31, 2016 · 2 comments
Closed

Comments

@papigers
Copy link

Hey,
One of the steps that are required to support querying databases using GraphQL, is to expose the required selectionSet in the resolve functions. That way, users could use that to project only the required fields, thus optimizing the database's query performance and minimizing the network traffic between the GraphQL server and the DB server.

For example, for the following query:

{
  user(id: 1) {
    name,
    email
  }
}

I would get this as an argument for the resolve function on the user field in the GraphQL Schema: [ 'name', 'email' ] (or something similar). This way I could project only those fields from the DB query.

I realize I can access the "root" selectionSet on the fourth paremeter (operation.selectionSet) in the resolve function, but this can get a bit messy for complex query. I'm looking for a way to expose only the required selection: under the user's resolve I would get only the selected user fields, and under "whatever"'s resolve I would get only "whatever"'s selected fields and so on...

I realize that supporting databases isn't your top priority right now, but I'm just looking for directions, maybe I'd could implement this on my own somehow.

@robrichard
Copy link
Contributor

@papigers I created a package called graphql-fields that does something very similar to what you describe.

@leebyron
Copy link
Contributor

I definitely suggest the approach taken by graphql-fields if this in the information you're looking for.

I would also suggest you consider performance tests for larger real-world queries under that assumption - what I've found is that querying an entire row (or at least the common subset available) can often result in improved global performance due to the opportunity for batching and caching when multiple values or types of values are fetched more than once during the operation of a query.

A lot of this may depend on your exact use case, schema, or database config, so YMMV.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants