No way to get requested fields of the object inside `resolve`
Problem
Let's say I have a Database with User entity with a lot of fields: [code block] And I have a GraphQL: [code block] And I do a GraphQL query to get `name` of all `users` [code block] I don't want to fetch all the fields from the database. I want to know what fields to fetch... Is there any way to get a list of fields that were requested in `resolve` method?
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: No way to get requested fields of the object inside `resolve`
Here's one idea... The 4th argument to `resolve` is the AST for the field. In your simple example, you could get the fields with something like: [code block] That would work, as long as you didn't have any fragments/spreads/etc. For more advanced cases, you could probably use the AST `visitor` utils. There's also a curious `// TODO: provide all fieldASTs, not just the first field` for that param
Trust Score
6 verifications
- 1
Here's one idea... The 4th argument to `resolve` is the AST for the field. In yo
Here's one idea... The 4th argument to `resolve` is the AST for the field. In your simple example, you could get the fields with something like:
- 2
That would work, as long as you didn't have any fragments/spreads/etc. For more
That would work, as long as you didn't have any fragments/spreads/etc. For more advanced cases, you could probably use the AST `visitor` utils. There's also a curious `// TODO: provide all fieldASTs, not just the first field` for that param too...
- 3
I believe that I heard that Facebook optimizes things like this internally, so h
I believe that I heard that Facebook optimizes things like this internally, so hopefully Lee will show up and drop some wisdom :smile:
Validation
Resolved in graphql/graphql-js GitHub issue #19. Community reactions: 23 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep