I am using graphql-tools
. After receiving a GraphQL query, I execute a search using ElasticSearch and return the data.
However, usually the requested query includes only a few of the possible fields, not all. I want to pass only the requested fields to ElasticSearch. First, I need to get the requested fields.
I can already get the whole query as a string. For example, in the resolver,
const resolvers = {
Query: {
async user(p, args, context) {
//can print query as following
console.log(context.query)
}
.....
}
}
It prints as
query User { user(id:"111") { id name address } }
Is there any way to get the requested fields in a format like
{ id:"", name:"", address:"" }
In graphql-js resolvers expose a forth argument called resolve info. This field contains more information about the field.
From the GraphQL docs
GraphQLObjectType
config parameter type definition:In the
fieldNodes
field you can search for your field and get theselectionSet
for the particular field. From here it gets tricky since theselections
can be normal field selections, fragments or inline fragments. You would have to merge all of them to know all fields that are selected on a field.Similarly for
graphql-java
you may do the same by extending the field parameters withmyGetUsersResolverMethod(... DataFetchingEnvironment env)
.This
DataFetchingEnvironment
would be injected for you and you can traverse through thisDataFetchingEnvironment
object for any part of thegraph
/query.This Object allows you to know more about what is being fetched and what arguments have been provided.
Example:
In fact you may be alluding to look ahead data fetching. The above should give you enough insights into the fields requested and you can take it from there to tailor you downstream calls manually. But also you may look into a more efficient way to do this by using the data fetchers for Building efficient data fetchers by looking ahead
There is an
info
object passed as the 4th argument in the resolver. This argument contains the information you're looking for.It can be helpful to use a library as
graphql-fields
to help you parse the graphql query data: