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
Copy file name to clipboardExpand all lines: content/blog/deep-dive-data-fetcher-results.md
+15-19Lines changed: 15 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,10 +6,6 @@ categories = []
6
6
date = 2019-04-11T00:00:00+10:00
7
7
+++
8
8
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
-
13
9
# DataFetcherResult
14
10
15
11
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
68
64
{{< / highlight >}}
69
65
70
66
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.
72
68
73
69
# Code Challenges
74
70
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
76
72
path and a simpler `IssueDTO` for another path.
77
73
78
74
With enough paths this becomes problematic as it adds new DTO classes per path and makes out child data fetchers more complex
79
75
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.
83
79
84
80
85
81
# Passing Data and Local Context
86
82
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.
89
85
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
91
87
92
88
*`data` - which will be used as the source on the next set of sub fields
93
89
*`errors` - allowing you to return data as well as errors
94
90
*`localContext` - which allows you to pass down field specific context
95
91
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.
97
93
94
+
In our example case we will be use `data` and `localContext` to communicate between fields easily.
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.
125
122
126
123
It also passes down field specific `localContext` which is the pre-fetched comment data.
127
124
128
125
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
130
127
that it given only to this field and its children and not any other field in the query.
131
128
132
129
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
149
146
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`
150
147
as being inherited unless a fields data fetcher explicitly overrides it.
151
148
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
153
150
that are being used in more simple data fetchers.
154
151
155
152
156
153
# Passing back Errors or Data or Both
157
154
158
155
For completeness we will show you that you can also pass down errors or data or local context or all of them at once.
159
156
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.
0 commit comments