I would like to develop a backend for frontend service with Node.js that acts as a simple interface between the frontend and backend microservices.
I wanna try out GraphQL but could not decide whether the frontend calls BFF through GraphQL or should BFF calls backend services through GraphQL. Which is the better approach for BFF/API Gateway or what are the best practices?
Is it better to use REST or GraphQL for React in this scenario?
CodePudding user response:
The BFF layer can serve any API style you see fit, REST, GraphQL, gRPC, even SOAP. However, given that one of the goals is to allow front end developers to build delightful user experiences, GraphQL is a common choice.
The BFF allows your backend developers to build the API that makes sense. It should still absorb the complexity as a good API will, but they do not need to build it with one specific UI in mind.
A rest API for a simple application will often be comprised of a listing query, that includes the metadata about each object, a title, and a synopsis.
Graphs, on the other hand, allow you to get fetch the listing and all the details for each object.
CodePudding user response:
To be honest, you'll get opinionated answers for this type of question.
There are huge discussions about REST versus GraphQL. The actual meaning of a RESTful API is also misunderstood by many people.
That being said, the tooling for GraphQL is great in my opinion. The ability to generate types for the client and server implementation is where GraphQL shines. It will be difficult to make mistakes. GraphQL has its disadvantages. The implementation is more difficult on the server side (caching, performance, security, ...). One thing I find lacking in GraphQL is an input union (they are working on it). If you need to deal with complex objects in mutations, it's better to look for something else.
Whereas setting up a few endpoints with OpenAPI spec allows for flexibility and freedom. Tooling is also catching up.
On the other hand, I don't like too much freedom. This is what makes the tooling of GraphQL outstanding.
Try out both approaches and see what you like. If you're not working alone, might be useful to use the same tech as other teams?