Skip to content

Commit 11acec8

Browse files
committed
Blog on DataFetcherResult PR comments
1 parent 931ac8b commit 11acec8

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

content/blog/deep-dive-data-fetcher-results.md

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ categories = []
66
date = 2019-04-11T00:00:00+10:00
77
+++
88

9-
# graphql-java - In Depth series
10-
11-
Welcome to the new series "graphql-java - In Depth" where we will explore more specific graphql-java topics.
12-
139
# DataFetcherResult
1410

1511
Today we are looking into the `graphql.execution.DataFetcherResult` object.
@@ -68,33 +64,34 @@ Imagine this is backed by an SQL system we might be able to use this field look
6864
{{< / highlight >}}
6965

7066
So we have looked ahead and returned different data depending on the field sub selection. We have made our system more efficient by using look ahead
71-
to fetch data just the 1 time and not N+1 times.
67+
to fetch data just the `1` time and not `N+1` times.
7268

7369
# Code Challenges
7470

75-
The challenge with this is that the shapes of the returned data is now field sub selection specific. We needed a `IssueAndCommentsDTO` for one sub selection
71+
The challenge with this code design is that the shapes of the returned data is now field sub selection specific. We needed a `IssueAndCommentsDTO` for one sub selection
7672
path and a simpler `IssueDTO` for another path.
7773

7874
With enough paths this becomes problematic as it adds new DTO classes per path and makes out child data fetchers more complex
7975

80-
Also the standard graphql pattern is that the returned object becomes the `source` aka `graphql.schema.DataFetchingEnvironment#getSource` of the next child
81-
data fetcher. But we might have pre fetched data that is need 2 levels deep and this is challenging to do since each data fetcher would need to capture and copy
82-
that down to the layers below.
76+
Also the standard graphql pattern is that the returned object becomes the `source` ie. `graphql.schema.DataFetchingEnvironment#getSource` of the next child
77+
data fetcher. But we might have pre fetched data that is needed 2 levels deep and this is challenging to do since each data fetcher would need to capture and copy
78+
that data down to the layers below via new TDOs classes per level.
8379

8480

8581
# Passing Data and Local Context
8682

87-
graphql-java offers a capability that helps with this pattern. We go beyond what the reference graphql-js system gives you where the object you
88-
returned is automatically and only the `source` of the next child fetcher and that's it.
83+
GraphQL Java offers a capability that helps with this pattern. GraphQL Java goes beyond what the reference graphql-js system gives you where the object you
84+
returned is automatically the `source` of the next child fetcher and that's all it can be.
8985

90-
In graphql-java you can use `graphql.execution.DataFetcherResult` to return three sets of values
86+
In GraphQL Java you can use well known `graphql.execution.DataFetcherResult` to return three sets of values
9187

9288
* `data` - which will be used as the source on the next set of sub fields
9389
* `errors` - allowing you to return data as well as errors
9490
* `localContext` - which allows you to pass down field specific context
9591

96-
In our example case we will be use `data` and `localContext` to communicate between fields easily.
92+
When the engine sees the `graphql.execution.DataFetcherResult` object, it automatically unpacks it and handles it three classes of data in specific ways.
9793

94+
In our example case we will be use `data` and `localContext` to communicate between fields easily.
9895

9996
{{< highlight Java "linenos=table" >}}
10097

@@ -120,13 +117,13 @@ DataFetcher issueDataFetcher = environment -> {
120117
};
121118
{{< / highlight >}}
122119

123-
If you look now you will see that our data fetcher returns a compound `DataFetcherResult` object that contains data for the child data fetchers which is the
124-
list of `issueDTO` objects as per usual.
120+
If you look now you will see that our data fetcher returns a `DataFetcherResult` object that contains `data` for the child data fetchers which is the
121+
list of `issueDTO` objects as per usual. It will be their `source` object when they run.
125122

126123
It also passes down field specific `localContext` which is the pre-fetched comment data.
127124

128125
Unlike the global context object, local context objects are passed down from a specific field to its children and are not shared across to peer fields. This means
129-
a parent field has a "back channel" to talk to the child fields without having to "pollute" the DTO source objects with that information and it is local in the sense
126+
a parent field has a "back channel" to talk to the child fields without having to "pollute" the DTO source objects with that information and it is "local" in the sense
130127
that it given only to this field and its children and not any other field in the query.
131128

132129
Now lets look at the `comments` data fetcher and how it consumes this back channel of data
@@ -149,16 +146,15 @@ Notice how it got the `issueDTO` as its source object as expected but it also go
149146
to pass on new local context OR if it passes nothing then the previous value will bubble down to the next lot of child fields. So you can think of `localContext`
150147
as being inherited unless a fields data fetcher explicitly overrides it.
151148

152-
Our data fetcher is a smidge more complex because of the data pre-fetching but 'localContext' allows us a nice back channel to pass data without modifying our DTO objects
149+
Our data fetcher is a bit more complex because of the data pre-fetching but 'localContext' allows us a nice back channel to pass data without modifying our DTO objects
153150
that are being used in more simple data fetchers.
154151

155152

156153
# Passing back Errors or Data or Both
157154

158155
For completeness we will show you that you can also pass down errors or data or local context or all of them at once.
159156

160-
It is perfectly valid to fetch data in graphql and to ALSO send back errors. Its not common but its valid. For example you might be able to select issues data but
161-
not the associated comment data. Some data is better than no data.
157+
It is perfectly valid to fetch data in graphql and to ALSO send back errors. Its not common but its valid. Some data is better than no data.
162158

163159
{{< highlight Java "linenos=table" >}}
164160

0 commit comments

Comments
 (0)