<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhay</title>
    <description>The latest articles on DEV Community by Abhay (@aparoha).</description>
    <link>https://dev.to/aparoha</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1182700%2F4ae59fb5-c6dd-4eca-a2c3-665d9650cd4d.png</url>
      <title>DEV Community: Abhay</title>
      <link>https://dev.to/aparoha</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/aparoha"/>
    <language>en</language>
    <item>
      <title>How to create a GraphQL API using Spring Boot – A Beginner's Guide</title>
      <dc:creator>Abhay</dc:creator>
      <pubDate>Wed, 11 Oct 2023 22:20:38 +0000</pubDate>
      <link>https://dev.to/aparoha/how-to-write-graphql-apis-in-spring-boot-a-beginners-guide-2bc3</link>
      <guid>https://dev.to/aparoha/how-to-write-graphql-apis-in-spring-boot-a-beginners-guide-2bc3</guid>
      <description>&lt;h2&gt;
  
  
  What is GraphQL?
&lt;/h2&gt;

&lt;p&gt;According to its &lt;a href="https://graphql.org/learn/" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;, “GraphQL is a query language for your API and a server-side runtime for executing queries using a type system you define for your data.” It was developed and open-sourced by Meta and is now maintained by a large community of companies and individuals.&lt;/p&gt;

&lt;p&gt;GraphQL consists of 2 main parts: -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Schema Definition Language&lt;/strong&gt; - GraphQL schemas for a service specified using GraphQL SDL (schema definition language), also sometimes referred to as just GraphQL schema language.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Query Language&lt;/strong&gt; - A query language consists of 3 parts, Query, Mutations and Subscription&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We’ll discuss these points in detail later in this article.&lt;/p&gt;

&lt;p&gt;GraphQL has a lot of good features, like:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It enables &lt;strong&gt;declarative data fetching&lt;/strong&gt; and provides a &lt;strong&gt;single endpoint&lt;/strong&gt; where a client can specify exactly what’s needed, no more over-fetching of information.&lt;/li&gt;
&lt;li&gt;It provides &lt;strong&gt;validation and type checking&lt;/strong&gt; out of the box, you can validate a query within the GraphQL strongly typed system before execution. &lt;/li&gt;
&lt;li&gt;It keeps &lt;strong&gt;API documentation in sync&lt;/strong&gt; with API changes. This benefits developers, since they must spend less time documenting an API.&lt;/li&gt;
&lt;li&gt;GraphQL removes the need for &lt;strong&gt;versioning&lt;/strong&gt; by deprecating APIs on a field level. Old fields can be later removed from the schema without impacting the existing queries.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;There are some disadvantages also: -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;GraphQL requires &lt;strong&gt;heavier tooling support&lt;/strong&gt;, both on the client and server sides. This requires a sizable upfront investment. It is not a suitable alternative for very simple use cases of API.&lt;/li&gt;
&lt;li&gt;GrpahQL has a single point of entry and uses HTTP POST by default. This prevents the full use of client-side &lt;strong&gt;HTTP caching&lt;/strong&gt; and makes it a non-trivial task to implement caching.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  GraphQL Schema
&lt;/h2&gt;

&lt;p&gt;Schema is mandatory in GraphQL service. The GraphQL Schema defines how data is requested and returned from the server and is controlled by GraphQL SDL. Let’s try to understand some of the most important concepts with one sample schema: -&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# root Query Type
type Query {
    fetchBooks: [Book]
}

# Book type
type Book {
    id : ID
    name : String
    author: String
    ratings: [Rating]
}

# Rating type
type Rating {
    id: ID
    rating: Int
    comment: String
    user: String
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Object Types&lt;/strong&gt; – Object types are specific to GraphQL service, and are defined with the type keyword. They define the type name and the fields under that type. Each field in an object type can be resolved to either scaler types or other object types, e.g., type Book has scaler fields of type String and another object field of type Rating.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Built-In Scaler Types&lt;/strong&gt; - There are 5 built-in scalar types in GraphQL: Int, Float, String, Boolean, and ID. The ID type resolves to a string but expects a unique value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Type Modifiers&lt;/strong&gt; - Modifiers can be used on the type that a field resolves to by using characters like […], e.g. root Query has type modifier [Book], it represents a nullable list of non-nullable string values. The entire value can be null, or specific list elements can be null.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Operations&lt;/strong&gt; - A GraphQL service can support the following operations: -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Queries&lt;/strong&gt;: Queries represent READ operations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mutation&lt;/strong&gt;: Mutation comprises WRITE then READ operation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Subscription&lt;/strong&gt;: A subscription is used for continuous READ (e.g. over WebSocket).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Getting started
&lt;/h2&gt;

&lt;p&gt;In this article, you’re going to build a book catalog service using Spring Boot GraphQL. This service will support a single query “fetchBooks” to return the top 5 books, authors, and ratings of all times.&lt;/p&gt;

&lt;p&gt;We are using &lt;a href="https://spring.io/projects/spring-graphql" rel="noopener noreferrer"&gt;Spring for GraphQL&lt;/a&gt; to build this service. It is built on &lt;a href="https://www.graphql-java.com/" rel="noopener noreferrer"&gt;GraphQL Java&lt;/a&gt; and supports GraphQL request handling over HTTP and WebSocket. GraphQL specification is not tied to any transport layer protocol. I am using &lt;a href="https://github.com/graphql/graphql-over-http/blob/main/spec/GraphQLOverHTTP.md" rel="noopener noreferrer"&gt;GraphQL over HTTP&lt;/a&gt; specification to build this API. &lt;/p&gt;

&lt;p&gt;Before diving into code, let’s spend some time understanding Spring GraphQL. It has 3 main components, transport, GraphQL Java engine and data fetchers.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1vetx1j41vy21pjj0vk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1vetx1j41vy21pjj0vk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Transport&lt;/strong&gt; - GraphQL Java doesn’t provide any transport by default. You can use any transport mechanism provided by the Spring framework e.g. Spring MVC, Spring WebFlux, and WebSocket etc. These handlers tunnel all requests GraphQL Java Engine.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GraphQL Java Engine&lt;/strong&gt; – It has support to bootstrap service from scratch and context propagation from the web layer to the data layer. Exceptions are also handled at this layer.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data Fetchers&lt;/strong&gt; – It provides annotated controllers (@Controller), query Domain Specific Language (DSL) and repositories&lt;/p&gt;

&lt;h2&gt;
  
  
  Service Implementation steps
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Create a Spring Boot project with dependencies.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can use &lt;a href="https://start.spring.io/" rel="noopener noreferrer"&gt;spring initializer&lt;/a&gt; to create an empty spring boot project with the below dependencies: -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fahk7yr2eomu7joxcn3c9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fahk7yr2eomu7joxcn3c9.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;spring-boot-starter-graphql – main dependency&lt;/li&gt;
&lt;li&gt;spring-boot-starter-web – add this dependency for building the transport layer&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Create a schema file for our books catalog service.&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

type Query {
    fetchBooks: [Book]
}

type Book {
    id : ID
    name : String
    author: String
    ratings: [Rating]
}

type Rating {
    id: ID
    rating: Int
    comment: String
    user: String
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Create a data fetcher.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.graphql-java.com/documentation/data-fetching/" rel="noopener noreferrer"&gt;Data fetcher&lt;/a&gt; is a callback function, and it fetches data to include the result of a query. When an incoming GraphQL query is received, the library will call the registered DataFetcher for query. Every query must have an associated DataFetcher, e.g., we’ll create a data fetcher for the “fetchBooks” query in our schema defined in step 2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Type Wiring and RunTime wiring&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;GraphQL Java uses &lt;a href="https://javadoc.io/static/com.graphql-java/graphql-java/9.5/graphql/schema/idl/RuntimeWiring.Builder.html" rel="noopener noreferrer"&gt;RuntimeWiring&lt;/a&gt; builder to register data fetchers, type resolvers, and more. We’ll create RuntimeWiringConfigurer beans in our Spring config to get access to the RuntimeWiring.Builder. Below code snippet shows data fetcher for “fetchBooks” query and it’s linking with RuntimeWiring builder.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

@Configuration
public class GraphQLServiceConfiguration {

    @Bean
    public RuntimeWiringConfigurer runtimeWiringConfigurer(BooksCatalogService bookCatalogService) {
        return builder -&amp;gt; {
            builder.type(
                    "Query",
                    wiring -&amp;gt;
                            wiring
                                    .dataFetcher("fetchBooks", environment -&amp;gt; bookCatalogService.fetchBooks()));
            builder.type(
                    "Book",
                    wiring -&amp;gt;
                            wiring.dataFetcher("ratings", env -&amp;gt; bookCatalogService.fetchRatings(env.getSource())));
        };
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above code, Data Fetcher for field fetchBooks fetches all books and rating by calling books catalog service’s methods fetchBooks() and fetchRatings().&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Running and Testing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You can start the GraphQL service from IDE by running the main method of BooksCatalogServiceApplication class&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsn607p8p0hj7r8gs0xe4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsn607p8p0hj7r8gs0xe4.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can use Insomnia client to test your API. &lt;br&gt;
POST : &lt;a href="http://localhost:8080/graphql" rel="noopener noreferrer"&gt;http://localhost:8080/graphql&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgwfqcgv5jcbrk3obwns.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkgwfqcgv5jcbrk3obwns.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also use GraphiQL editor in the browser using the link &lt;a href="http://localhost:8080/graphiql?path=/graphql" rel="noopener noreferrer"&gt;http://localhost:8080/graphiql?path=/graphql&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F223j2o660khoz6oyty16.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F223j2o660khoz6oyty16.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To enable GraphiQL editor, you must set &lt;strong&gt;spring.graphql.graphiql.enabled=true&lt;/strong&gt; in application.properties.&lt;/p&gt;

&lt;p&gt;The working code example of this article is listed on &lt;a href="https://github.com/aparoha/books-catalog-service" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;. To run the example, clone the repository and import books-catalog-service as a project in your favorite IDE as a Maven project.&lt;/p&gt;

</description>
      <category>graphql</category>
      <category>springboot</category>
      <category>beginners</category>
      <category>java</category>
    </item>
  </channel>
</rss>
