1

有人可以解释一下如何在 graphql 中包装现有的 REST API,以及它将如何提高性能?

任何小的工作示例都会有很大帮助。

提前致谢 !!!

4

1 回答 1

1

在 GraphQL 模式中定义查询/突变。

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

在查询解析器类中使用任何方法来使用休息服务。

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;
    }
}

同样,您也可以通过实现 GraphQLMutationResolver 来定义 Mutation。

于 2018-08-23T08:40:20.570 回答