I have a GraphQL query similar to this:
query {
getPosts {
...PostFragment
}
}
fragment SpecificPostFragment on SpecificPost {
owner {
id
name
}
}
fragment PostFragment on Post {
id
object
... on SpecificPost {
...SpecificPostFragment
}
}
I try to know if:
- the field object is requested
- the field owner is requested
I try to apply what is written here: https://www.graphql-java.com/documentation/v11/fieldselection/
But dataFetchingEnvironment.getSelectionSet().contains("XXX") does not seem to work well with fragments.
How to do that ?
I haven't found any built-in solution, so I wrote my own. Here is my code
And you call it like this:
Note that this solution does not support wild card (* or ?). And I haven't tested it if the main query contains multiple entries (getPost + getPeople in the same query for example), but that probably does not work in that case.