Writing fetchers for GraphQL queries
You are going to write data fetchers in this section. Data fetchers, as the name suggests, retrieve the information from the source that is from a persistent store such as a database or a third-party API/document store. You will learn how to write data fetchers to retrieve a single field of data, a single object, and a collection of objects.
Writing the data fetcher for product
The data fetcher is a critical DSG component for serving GraphQL requests that fetches the data and the DSG internally resolves each of the fields. You mark them with the special @DgsComponent
DGS annotation. These are types of Spring components that the DGS framework scans and uses for serving requests.
Let’s create a new file called ProductDatafetcher.java
in the datafetchers
package to represent a DGS data fetcher component. It will have a data fetcher method for serving the product
query. You can add the following code to it:
@DgsComponentpublic class...