0
votes

If I have the schema:

type Query {
  posts: [Post!]!
}
type Post {
  title: String!
  lotsofdata: String
}

and a resolver:

function posts(parent, args, context, info) {
  return readAllPosts(/*?*/)
}

And two possible queries. Query #1:

query {
  posts{
    title        
  }
}

and query #2:

query {
  posts{
    title 
    lotsofdata
  }
}

Is it possible to optimise the resolver so with query #1 readAllPosts only pulls back titles from the database but for query #2 it pulls back both titles and lotsofdata?

I've looked at the parent, args, context, and info arguments but can't see anything to indicate whether the resolver is being called in response to a query like #1 or like #2.

1
Just found this github.com/graphql/graphql-js/issues/19 which seems relevant but a couple of years old. I’d appreciate any advice on doing this with the latest graphql tools.Adam Griffiths

1 Answers

1
votes

not sure if it is still relevant for you, but it should be possible, you can take a look at library called https://github.com/robrichard/graphql-fields#readme. It will parse info argument in your resolver function. This way you can gain information about executed fields into your resolver. The other part is to use this information to build proper SQL statement or projection or whatever (dependent on what db you use). I hope, that it helps. Best David