is there a way to retrieve the list of the fields were requested in a GraphQL query by a client?
Lets assume I have the following types:
type Book {
isbn: String
title: String
genre: String
author: Author
}
type Author {
name: String
surname: String
age: Int
}
Is there a way on Java side inside a method annotated with @GraphQLQuery to know the fields were requested by the client?
For example, having the following queries:
query {
book ( isbn = "12345" ) {
title
genre
}
}
query {
book ( isbn = "12345" ) {
title
author {
name
surname
}
}
}
I can know the first query requested the fields title and genre of Book and the second required title from Book and also name and surname of the Author ?
Thanks Massimo
You can obtain this info via @GraphQLEnvironment
. If you just need the immediate subfields, you can inject the names like this:
@GraphQLQuery
public Book book(String isbn, @GraphQLEnvironment Set<String> subfields) {
...
}
If you want all the subfields, or other selection info, you can inject ResolutionEnvironment
and get DataFetcherEnvironment
from there. That in turn gives you access to the whole SelectionSet
.
@GraphQLQuery
public Book book(String isbn, @GraphQLEnvironment ResolutionEnvironment env) {
DataFetchingFieldSelectionSet selection = env.dataFetchingEnvironment.getSelectionSet();
...
}