2

In the context of server to server communication and more precisely within different microservices, is it a good idea to use GraphQL?

The idea is to allow the consumers to only fetch relevant fields for them. One can build the field projections for REST GET endpoints themselves or can use a library to do so e.g. Rest.li.

In the era where GraphQL exists to solve similar problems, would it make more sense to use GraphQL for READs (This is where GraphQL brings a lot of value) and still use REST for write operations (There are some compelling reasons for which moving to GraphQL for writes is not possible at the moment)?

GSSwain
  • 5,787
  • 2
  • 19
  • 24
  • How does the client know which data is relevant to it? Without a-priori knowledge of the available properties of a certain resource I doubt that this is really a good design. In [this answer](https://stackoverflow.com/questions/57723123/is-a-graphql-api-restful-by-default/58558884#58558884) I summarized Fieldings take on how GraphQL could be combined with REST, where Fielding suggest: `A client can POST to a REST service to create a resource that is a GraphQL query, and then GET that resource with all benefits of REST…` – Roman Vottner Aug 05 '20 at 14:38
  • There is an existing REST resource say with 10-20 fields as part of the GET response. Most of the services would need 1-3 fields (mostly different based on the client). In a way the client knows what fields it needs. Would putting a GraphQL api for READs in this case would make sense? Essentially we'll end up with REST and GraphQL in our stack. Is having both for server to server communication a bad idea? – GSSwain Aug 06 '20 at 03:48
  • Technically it might, though whether this solution is "RESTful" is a different story. I.e. you might not be able to cache GraphQL responses easily as all queries target the same endpoint and therefore use the same URI (= cache key). As such this would violate the cacheable constraint REST has. You could, however, use either dedicated media types that specify which properties to return or make use of [resources that support views or the like](https://stackoverflow.com/questions/35495385/rest-services-and-multiple-representations-of-same-object-with-different-fields/35532212#35532212) – Roman Vottner Aug 06 '20 at 08:20
  • Thanks @RomanVottner for your time and response. Wanted to compare what is possible today with GraphQL compared to what we have been doing only with REST. – GSSwain Aug 07 '20 at 04:25

0 Answers0