Spring Boot GraphQL Fetch from External Service

An application in production needs to call other APIs and retrieve data. GraphQL is a query manipulation language and agnostic of the source of the data, The application can consume data from a database or an external service.

package org.wesome.graphql.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;
import org.wesome.graphql.entity.User;
import org.wesome.graphql.service.UserService;
import reactor.core.publisher.Flux;

@Controller
public class UserGraphQLController {
    @Autowired
    private UserService userService;

    @QueryMapping("findAllUser")
    Flux<User> findAllUser() {
        return userService.findAllUser();
    }
}
package org.wesome.graphql.entity;

public record User(Long id, String name, String username, String email, Address address, String phone, String website, Company company) {
}

record Address(String street, String suite, String city, String zipcode, Geo geo) {
}

record Geo(String lat, String lng) {
}

record Company(String name, String catchPhrase, String bs) {
}
package org.wesome.graphql.service;

import org.wesome.graphql.entity.User;
import reactor.core.publisher.Flux;

public interface UserService {
    Flux<User> findAllUser();
}
package org.wesome.graphql.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import org.wesome.graphql.entity.User;
import reactor.core.publisher.Flux;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private WebClient petWebClient;

    @QueryMapping
    public Flux<User> findAllUser() {
        return petWebClient.get()
                .uri("/users")
                .retrieve()
                .bodyToFlux(User.class);
    }
}
package org.wesome.graphql;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.reactive.function.client.WebClient;

@SpringBootApplication
public class GraphqlProjectApplication {
    public static void main(String[] args) {
        SpringApplication.run(GraphqlProjectApplication.class, args);
    }

    @Bean
    public WebClient webClient() {
        WebClient webClient = WebClient.builder().baseUrl("https://jsonplaceholder.typicode.com/").build();
        return webClient;
    }
}
type Company {
    name: String
    catchPhrase: String
    bs: String
}

type Geo {
    lat: String
    lng: String
}

type Address {
    street: String
    suite: String
    city: String
    zipcode: String
    geo: Geo
}

type User {
    id: Int
    name: String
    username: String
    email: String
    phone: String
    website: String
    company: Company
    address: Address
}

type Query{
    # query to get all users
    findAllUser:[User]
}

\src\main\resources\application.properties

spring.graphql.graphiql.enabled=true

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.5</version>
        <relativePath/>
    </parent>
    <groupId>org.wesome</groupId>
    <artifactId>spring-boot-graphql</artifactId>
    <version>0.0.1-snapshot</version>
    <name>spring boot graphql</name>
    <description>implementing graphql in spring boot</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-graphql</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.graphql</groupId>
            <artifactId>spring-graphql-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Rest API

curl --location 'http://localhost:8080/findAllApple?page=0&size=10'

GraphiQL

GraphQL provides an inbuild UserInterface GraphiQL, which can be accessed via http://localhost:8080/graphiql or access GraphQL via Postmanin the query section add the below query

query FindAllUser {
    findAllUser {
        id
        name
        username
        email
        phone
        website
        company {
            name
            catchPhrase
            bs
        }
        address {
            street
            suite
            city
            zipcode
            geo {
                lat
                lng
            }
        }
    }
}

follow us on