1

Can somebody please explain me how to wrap existing REST API in graphql, and how it will improve the performance ?

Any small working example would help a lot.

Thanks in advance !!!

Abhii
  • 295
  • 2
  • 6
  • 18

1 Answers1

1

Define the Query/Mutation in GraphQL schema.

type Query {
getBook(id: String): Book
}

Inside the Query resolver class use any method to consume rest service.

public class Query implements GraphQLQueryResolver {
public Book getBook(String bookId) {
        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> map = new HashMap<>();
        map.put("bookId", bookId);
        ResponseEntity <Book> response = restTemplate.getForEntity("http://localhost:8080/bookDetails/getBook/{bookId}",Book.class, map);
        Book book = response.getBody();
        return book;
    }
}

In the same way you can define Mutation also by implementing GraphQLMutationResolver.

Rahul
  • 84
  • 1
  • 6
  • Here also we will call the REST API ? How it will improve the performance in this case ? – Abhii Aug 23 '18 at 17:54