4 ms·
I never understood peoples positions that GraphQL all of the sudden the frontend can make any Query it wants and control is out of the hands of the backend. Tha
by hansonkd 2y ago
I never understood peoples positions that GraphQL all of the sudden the frontend can make any Query it wants and control is out of the hands of the backend. That seems orthogonal to my experiences with GraphQL.
GraphQL is a protocol and you define the implementation. Some GraphQL implementations like REST implementations try to generate everything for you. That is not "GraphQL" but one type.
GraphQL is way to query nested APIS. The dataloading N+1 Problems are substantially easier to solve in GraphQL and many frameworks have solutions auto-optimize ORM queries for example.
The backend defines the query, the frontend requests it. Simple. Never understood the hate against GQL that now the frontend has all this power. They have exactly as much power as you would have exposed in a Rest Interface just now the backend can actually see all the data it needs at once and optimize.
If you are worried about a cartesian product from a GraphQL call I have news for you about production REST systems. Ever seen an engineer do a loop and make n+1 REST calls for resources? It happens more often then you think.
REST encourages resource waste. You are getting fields you don't need. Hard to query related objects, etc.
Benefits I have reaped with GraphQL:
1. API analysis - I can statically analyze a typescript program and verify their API calls are valid against backend schema.
2. Usage analysis - Statically analyzing the schema I can see which fields are still in use and safely refactor.
3. Frontend Velocity - The frontend can cange its data needs on the frontend query whatever related objects it needs efficiently without backend changes. You can get all data for a page with 1 call and not have to have page specific endpoints.
4. Less backend engineers are needed as API changes less.
5. N+1 Optimization much easier to solve in GraphQL then REST
There are so many advantages to using GraphQL. The only advantage of REST I can think of is for static external APIs.
- tangkikodo 2y agoI would like to recommend pydantic-resolve, it can enjoy benefits from botn rest/rpc and gql. the triky part of gql is we need to build a Query system for all business requirement in single entry, and we can not predict what query will be issued by client. this is anti-pattern because presenter layer should not care about how data is composed. It would be much easy if we treat each rest/rpc endpoint as a GQL entry, use resolver and dataloader to quickly & easily build complicated view data, everything is still under the control of backend. here is a demo. https://github.com/allmonday/pydantic-resolve/blob/master/examples/0_demo.py https://github.com/allmonday/pydantic-resolve/blob/master/ex...