Microservice Communication for Graphql APIs

Microservice Communication for Graphql APIs


1

Problem Statement : I have a GraphQl query defined which is responsible for fetching few details from the database.
This graphql schema let’s say is defined in microservice-B . Now I want this garphql API to be called by an upstream microservice-A.
microservice-A is actually the one which is actually called by our Clients(UI or any other client) Via Gateway
So, all the business logic is thereby written in the resolver written in microservice-B only, microservice-A resolver is just acting like a proxy to make a call to microservice-B resolver.

The Flow is UI -> Gateway -> Service A -> Service B

UI will make a graphql request to service A , service A(resolver) should make a call to service B(resolver).
Now, for the microservice communication I am using OpenFeign Client.

For staying intact with the design pattern, the same graphql schema needs to be present in both the services A & B

Where I am stuck is 2 places 🙁

  1. When I have to make a call to service B from service A , I need to pass the actual query resolved with the arguments to the client

This is how the resolver – B is defined in the feignClient UserAPI

User API Feign Client

@PostMapping(value = "/graphql/users" ,consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    String getUsers(@RequestBody String query
    );

How do I get the actual query in the resolver A ? Please refer to the commented line ?

Resolver of Service A

@Component
public class UserResolverServiceA implements GraphQLQueryResolver {
    
    @Autowired
    private UserAPI userAPI;
    public User getUser( String firstName, String lastName, String phoneNumber , DataFetchingEnvironment environment)  {

        //How to get resolved query in the resolver for passing it to downstream service
        String result = userAPI.getUsers(query);

       //Convert this to actual User object by stripping off the graphql wrapped response.
       //Make changes if needed specific to service A

    }
}
  1. Now , since you see ideally for REST APIs the return type of Upstream Service , Feign Client & Downstream is same or superset of the JavaObject, but since GraphQL resolver responses are intercepted by GraphQL Server and it wraps it into graphql response, the return type from resolver B is actually not User object to the feign client but a wrapped graphQL response String .

That’s why the feign client will receive String as response from the resolver B .

Resolver A should how remove the graphql wrapper response and retrieve the actual User Object ?

Resolver of Service B


@Component
public class UserResolverServiceB implements GraphQLQueryResolver {
    @Autowired
    UserService userService;


    public User getUser(String firstName, String lastName, String phoneNumber){

        try{
            User user = userService.getUsers(firstName, lastName, phoneNumber);
            return user;

        } catch (Exception e){
            // TODO - Add Error Handling
            throw e;
        }
    }

}

I’m using

Java17
Springboot - 2.7.3
graphql-starter - 14.0.0
graphql-java - 19.2.0

Have added schema , endpoint etc details in sprigboot properties hence not created GraphqlProvider explicitly.
Tried to get hold of the query by intercepting the request by one of the custom filter but that didin’t work!
I don’t think it should be that complicated to achieve what I am expecting.

Share
Improve this question

New contributor

Anonymous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


Load 3 more related questions


Show fewer related questions

0

Reset to default



Leave a Reply

Your email address will not be published. Required fields are marked *