question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No way to get requested fields of the object inside `resolve`

See original GitHub issue

Let’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:closed
  • Created 8 years ago
  • Reactions:62
  • Comments:30 (12 by maintainers)

github_iconTop GitHub Comments

57reactions
benjiecommented, Jun 22, 2017

For anyone else who lands here; fieldASTs was renamed to fieldNodes in v0.8.0

36reactions
devknollcommented, Jul 4, 2015

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:

resolve: (source, args, root, ast) => {
  var args = ast.selectionSet.selections.map(selection => selection.name.value);
  User.all({ attributes: args });
}

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 😄

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found