@@ -13,24 +13,24 @@ Some fields will use specialised data fetcher code that knows how to go to a dat
1313most simply take data from the returned in memory objects using the field name and Plain Old Java Object (POJO) patterns
1414to get the data.
1515
16- ` Note : Data fetchers are some times called "resolvers" in other graphql implementations.`
16+ Note : Data fetchers are some times called "resolvers" in other graphql implementations.
1717
1818So imagine a type declaration like the one below :
1919
2020
2121``` graphql
22- type Query {
23- products (match : String) : [Product ] # a list of products
24- }
25-
26- type Product {
27- id : ID
28- name : String
29- description : String
30- cost : Float
31- tax : Float
32- launchDate (dateFormat : String = "dd, MMM, yyyy') : String
33- }
22+ type Query {
23+ products (match : String) : [Product ] # a list of products
24+ }
25+
26+ type Product {
27+ id : ID
28+ name : String
29+ description : String
30+ cost : Float
31+ tax : Float
32+ launchDate (dateFormat : String = "dd, MMM, yyyy') : String
33+ }
3434```
3535
3636
@@ -43,21 +43,21 @@ product results if the client specified it.
4343It might look like the following :
4444
4545```java
46- DataFetcher productsDataFetcher = new DataFetcher<List<ProductDTO>>() {
47- @Override
48- public List<ProductDTO> get(DataFetchingEnvironment environment) {
49- DatabaseSecurityCtx ctx = environment.getContext();
50-
51- List<ProductDTO> products;
52- String match = environment.getArgument(" match");
53- if (match != null) {
54- products = fetchProductsFromDatabaseWithMatching(ctx, match);
55- } else {
56- products = fetchAllProductsFromDatabase(ctx);
57- }
58- return products;
59- }
60- };
46+ DataFetcher productsDataFetcher = new DataFetcher<List<ProductDTO>>() {
47+ @Override
48+ public List<ProductDTO> get(DataFetchingEnvironment environment) {
49+ DatabaseSecurityCtx ctx = environment.getContext();
50+
51+ List<ProductDTO> products;
52+ String match = environment.getArgument(" match");
53+ if (match != null) {
54+ products = fetchProductsFromDatabaseWithMatching(ctx, match);
55+ } else {
56+ products = fetchAllProductsFromDatabase(ctx);
57+ }
58+ return products;
59+ }
60+ };
6161```
6262
6363
@@ -82,29 +82,28 @@ argument. We can have the ProductDTO have logic that applies this date formatti
8282
8383
8484```java
85- class ProductDTO {
85+ class ProductDTO {
8686
87- private ID id;
88- private String name;
89- private String description;
90- private Double cost;
91- private Double tax;
92- private LocalDateTime launchDate;
87+ private ID id;
88+ private String name;
89+ private String description;
90+ private Double cost;
91+ private Double tax;
92+ private LocalDateTime launchDate;
9393
94- // ...
94+ // ...
9595
96- public String getName() {
97- return name;
98- }
96+ public String getName() {
97+ return name;
98+ }
9999
100- // ...
100+ // ...
101101
102- public String getLaunchDate(DataFetchingEnvironment environment) {
103- String dateFormat = environment.getArgument(" dateFormat");
104- return yodaTimeFormatter(launchDate,dateFormat);
105- }
102+ public String getLaunchDate(DataFetchingEnvironment environment) {
103+ String dateFormat = environment.getArgument(" dateFormat");
104+ return yodaTimeFormatter(launchDate,dateFormat);
106105 }
107-
106+ }
108107```
109108
110109
@@ -122,36 +121,32 @@ represented as ``getDesc()`` in the runtime backing Java object.
122121If you are using SDL to specify your schema then you can use the ``@fetch`` directive to indicate this remapping.
123122
124123```graphql
125- directive @fetch(from : String!) on FIELD_DEFINITION
126-
127- type Product {
128- id : ID
129- name : String
130- description : String @fetch(from:" desc")
131- cost : Float
132- tax : Float
133- }
134-
124+ directive @fetch(from : String!) on FIELD_DEFINITION
125+
126+ type Product {
127+ id : ID
128+ name : String
129+ description : String @fetch(from:" desc")
130+ cost : Float
131+ tax : Float
132+ }
135133```
136134
137135This will tell the ``graphql.schema.PropertyDataFetcher`` to use the property name ``desc`` when fetching data for the graphql field named ``description``.
138136
139137If you are hand coding your schema then you can just specify it directly by wiring in a field data fetcher.
140138
141139```java
142-
143- GraphQLFieldDefinition descriptionField = GraphQLFieldDefinition.newFieldDefinition()
144- .name(" description")
145- .type(Scalars.GraphQLString)
146- .build();
147-
148- GraphQLCodeRegistry codeRegistry = GraphQLCodeRegistry.newCodeRegistry()
149- .dataFetcher(
150- coordinates(" ObjectType", " description"),
151- PropertyDataFetcher.fetching(" desc"))
152- .build();
153-
154-
140+ GraphQLFieldDefinition descriptionField = GraphQLFieldDefinition.newFieldDefinition()
141+ .name(" description")
142+ .type(Scalars.GraphQLString)
143+ .build();
144+
145+ GraphQLCodeRegistry codeRegistry = GraphQLCodeRegistry.newCodeRegistry()
146+ .dataFetcher(
147+ coordinates(" ObjectType", " description"),
148+ PropertyDataFetcher.fetching(" desc"))
149+ .build();
155150```
156151
157152
@@ -209,16 +204,16 @@ Imagine a query such as the following
209204
210205
211206```graphql
212- query {
213- products {
214- # the fields below represent the selection set
215- name
216- description
217- sellingLocations {
218- state
219- }
207+ query {
208+ products {
209+ # the fields below represent the selection set
210+ name
211+ description
212+ sellingLocations {
213+ state
220214 }
221215 }
216+ }
222217```
223218
224219
0 commit comments