No way to get requested fields of the object inside `resolve`
See original GitHub issueLet’s say I have a Database with User entity with a lot of fields:
var Sequelize = require('sequelize');
var sequelize = new Sequelize('sqlite://database.db');
var User = sequelize.define('User', {
name: Sequelize.STRING,
email: Sequelize.STRING,
otherField: Sequelize.STRING,
});
And I have a GraphQL:
var queryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
user: {
type: userType,
args: { id: { type: GraphQLID } },
resolve: (_, {id}) => User.findById(id)
},
users: {
type: new GraphQLList(userType),
resolve: (source, args) => {
User.all(); // How to get the requested fields?
//User.all({ attributes: args._requestedFields });
}
},
})
});
And I do a GraphQL query to get name
of all users
curl -s -X POST http://localhost:8080/api/graph -d '{ users { name } }' | jq '.'
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?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:62
- Comments:30 (12 by maintainers)
Top Results From Across the Web
How to get requested fields inside GraphQL resolver?
First, I need to get the requested fields. I can already get the whole query as a string. For example, in the resolver,...
Read more >Resolvers - graphql-compose
In terms of graphql-compose this field config is called as Resolver . The main aim of Resolver is to keep available resolve methods...
Read more >Retrieve selected fields from a search | Elasticsearch Guide [8.5]
The fields option returns values in the way that matches how Elasticsearch ... fields like _id or _index are not returned when the...
Read more >Resolvers - Apollo GraphQL Docs
A resolver is a function that's responsible for populating the data for a single field in your schema. It can populate that data...
Read more >Execution - GraphQL
obj The previous object, which for a field on the root Query type is often not used. args The arguments provided to the...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
For anyone else who lands here;
fieldASTs
was renamed tofieldNodes
in v0.8.0Here’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: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…I believe that I heard that Facebook optimizes things like this internally, so hopefully Lee will show up and drop some wisdom 😄