You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DataFetchingEnvironment should always have a way to get field sub-selection #377
Currently, DataFetchingEnvironment has no way to fetch only the required fields if the query uses fragments. DataFetchingEnvironment#getFields currently has populated SelectionSet only for Field and InlineFragment, while it is always blank for Fragment. Additionally, fragment definitions are inaccessible as they're only kept in ExecutionContext.
One other concern I had was around query proxiying.
Imagine you have this query
query {
handledByGraphqlProxyServer1 {
name
address {
street
postcode
}
age
}
handledByGraphqlProxyServer2 {
height
weight
}
}
Now imagine the backing data fetcher of handledByGraphqlProxyServer1 is in fact going to call out to a remote graphql server and pass on the inner query part
eg: effectively
query {
name
address {
street
postcode
}
age
}
So how could we implement this? I dont think today we can.
Because the selection set (including deeply beyond this objecy) is available to the data fetcher.
query UseFragment {
luke: human(id: "1000") {
...HumanFragment
homePlanet
}
leia: human(id: "1003") {
...HumanFragment
appearsIn
}
}
fragment HumanFragment on Human {
name
...FriendsAndFriendsFragment
}
fragment FriendsAndFriendsFragment on Character {
friends {
name
friends {
name
}
}
}
The DataFetchingEnvironment now has the selection set via fields and fragments. You would need to combine them to synthesize a pure list of fields.
The system output is given below
ields :
luke: human(id: "1000") {
...HumanFragment
homePlanet
}
fragments :
fragment HumanFragment on Human {
name
...FriendsAndFriendsFragment
}
fragment FriendsAndFriendsFragment on Character {
friends {
name
friends {
name
}
}
}
fields :
friends {
name
friends {
name
}
}
fragments :
fragment HumanFragment on Human {
name
...FriendsAndFriendsFragment
}
fragment FriendsAndFriendsFragment on Character {
friends {
name
friends {
name
}
}
}
fields :
leia: human(id: "1003") {
...HumanFragment
appearsIn
}
fragments :
fragment HumanFragment on Human {
name
...FriendsAndFriendsFragment
}
fragment FriendsAndFriendsFragment on Character {
friends {
name
friends {
name
}
}
}
fields :
friends {
name
friends {
name
}
}
fragments :
fragment HumanFragment on Human {
name
...FriendsAndFriendsFragment
}
fragment FriendsAndFriendsFragment on Character {
friends {
name
friends {
name
}
}
}
Notice how the fields are calculated as we go down but the fragments are global in the query. This is defined in the grammar
Oh, missed your last observation. I'm all for adding the ExecutionContext to the DataFetchingEnvironment, but there was a comment on the last PR that tried to fix a similar issue about not expanding the API surface by exposing the ExecutionContext, so that's why I didn't in my PR.
Ah, perfect, this has actually been solved already! Thanks for the explanations @bbakerman and @apottere !
And this issue is basically just a duplicate of #303 so I'll close it.
Actually, when I think about it better, maybe you wanted to keep it open until the decision on FieldCollector and the query proxying use-case from above.
Thinking of FieldCollector, perhaps it can be refactored a little to have methods that allow collecting based on fragment definitions only (when directives had already been processed) so that it can be used from within DataFetcher?
Thinking of FieldCollector, perhaps it can be refactored a little to have methods that allow collecting based on fragment definitions only (when directives had already been processed) so that it can be used from within DataFetcher?
Can you give examples of this. Having the specific use cases allows for unit tests to be more easily written
@bbakerman I was having similar ideas to what you ended up doing in your PR. I was thinking of refactoring FieldCollector in way that would allow it to be created ("initialized") with the variables, and passed to DataFetchingEnvironement, which would then be able to use it without having to pass variables again. It could even hide FieldCollector internally, just exposing the field collection methods itself. Not sure if this is smart, was just thinking aloud.
Activity
bbakerman commentedon Apr 27, 2017
One other concern I had was around query proxiying.
Imagine you have this query
Now imagine the backing data fetcher of handledByGraphqlProxyServer1 is in fact going to call out to a remote graphql server and pass on the inner query part
eg: effectively
So how could we implement this? I dont think today we can.
Because the selection set (including deeply beyond this objecy) is available to the data fetcher.
bbakerman commentedon May 2, 2017
I looked further into this issue and I think this is available now
@apottere added #392
I confirmed this locally using this little wrapper class
I put it over the starwars schema like this :
The DataFetchingEnvironment now has the selection set via fields and fragments. You would need to combine them to synthesize a pure list of fields.
The system output is given below
Notice how the fields are calculated as we go down but the fragments are global in the query. This is defined in the grammar
So they will always be "global" and need replacing as your data fetcher executes.
You could nominally use
FieldCollectorbut it requires ExecutionContext which you dont havePerhaps we should fix DataFetchingEnvironement so it has the executionContext and hence can call fieldCollector
apottere commentedon May 2, 2017
Yeah, fragment definitions are available on the
DataFetchingEnvironmentnow.apottere commentedon May 2, 2017
Oh, missed your last observation. I'm all for adding the
ExecutionContextto theDataFetchingEnvironment, but there was a comment on the last PR that tried to fix a similar issue about not expanding the API surface by exposing theExecutionContext, so that's why I didn't in my PR.kaqqao commentedon May 2, 2017
Ah, perfect, this has actually been solved already! Thanks for the explanations @bbakerman and @apottere !
And this issue is basically just a duplicate of #303 so I'll close it.
kaqqao commentedon May 2, 2017
Actually, when I think about it better, maybe you wanted to keep it open until the decision on
FieldCollectorand the query proxying use-case from above.Thinking of
FieldCollector, perhaps it can be refactored a little to have methods that allow collecting based on fragment definitions only (when directives had already been processed) so that it can be used from withinDataFetcher?graphql-java#377 - have the ability to know and capture all fields in…
bbakerman commentedon May 2, 2017
Can you give examples of this. Having the specific use cases allows for unit tests to be more easily written
kaqqao commentedon May 4, 2017
@bbakerman I was having similar ideas to what you ended up doing in your PR. I was thinking of refactoring
FieldCollectorin way that would allow it to be created ("initialized") with the variables, and passed toDataFetchingEnvironement, which would then be able to use it without having to pass variables again. It could even hideFieldCollectorinternally, just exposing the field collection methods itself. Not sure if this is smart, was just thinking aloud.Merge pull request #401 from bbakerman/377-field-selection-in-data-fe…
bbakerman commentedon May 10, 2017
Thanks for the idea. I have implemented it in the way you suggested
Merge remote-tracking branch 'upstream/master'