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
+7-6Lines changed: 7 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,15 @@
1
1
+++
2
-
title = "graphql-java - In Depth - Part 1: DataFetcherResult"
2
+
title = "Building efficient data fetchers by looking ahead"
3
3
author = "Brad Baker"
4
4
tags = []
5
5
categories = []
6
6
date = 2019-04-11T00:00:00+10:00
7
7
+++
8
8
9
-
# DataFetcherResult
9
+
# Efficient data fetchers
10
10
11
-
Today we are looking into the `graphql.execution.DataFetcherResult` object.
11
+
Today we are looking into the `graphql.schema.DataFetchingFieldSelectionSet` and `graphql.execution.DataFetcherResult` objects as means
12
+
to build efficient data fetchers.
12
13
13
14
# The scenario
14
15
@@ -31,16 +32,16 @@ But first lets set the scene. Imagine we have a system that can return `issues`
31
32
Nominally we would have a `graphql.schema.DataFetcher` on `issues` that returns a list of issues and one on the field `comments` that returns the list of comments
32
33
for each issue `source` object.
33
34
34
-
As you can see this naively creates an N+1 problem where we need to fetch data multiple times, one for each `issue` object in isolation.
35
+
As you can see this naively creates an *N+1 problem* where we need to fetch data multiple times, one for each `issue` object in isolation.
35
36
36
37
We could attack this using the `org.dataloader.DataLoader` pattern but there is another way which will discuss in this article.
37
38
38
-
# Look ahead
39
+
# Look ahead via DataFetchingFieldSelectionSet
39
40
40
41
The data fetcher behind the `issues` field is able to look ahead and see what sub fields are being asked for. In this case it knows that `comments` are being asked
41
42
for and hence it could prefetch them at the same time.
42
43
43
-
`graphql.schema.DataFetchingEnvironment#getSelectionSet` can be used by data fetcher code to get the selection set of fields for a given parent field.
44
+
`graphql.schema.DataFetchingEnvironment#getSelectionSet`(aka `graphql.schema.DataFetchingFieldSelectionSet`) can be used by data fetcher code to get the selection set of fields for a given parent field.
0 commit comments