Reputation: 33946
We are starting a new full stack project. It has been decided we are going to use React for the front end which should consume a GraphQL API.
We are contemplating two scenarios for the development of this API:
The first one is to build a GraphQL API that uses a REST API as data source using Apollo.
The second one is to build a GraphQL API that uses a database as data source skipping the REST API.
What are the advantages and disadvantages of each scenario? Is one better than the other?
Upvotes: 4
Views: 1954
Reputation: 2741
The second scenario is best
The second one is to build a GraphQL API that uses a database as data source skipping the REST API.
REST and GraphQL can both be operated over HTTP, though GraphQL is protocol agnostic. And GraphQL is the new kid in the block with lot of cool features and it solves most of the problem faced with REST like :
1.) Multiple end points to fetch data
In rest we have create multiple API endpoints
2.) Over/Under Fetching
Some time API return extra data that we don't need or less data in response
3.) Network Requests
multiple endpoint leads to more network requests
4.) Error Handling
Error handling in REST is pretty straightforward, we simply check the HTTP headers to get the status of a response. Depending on the HTTP status code ( 404, 503, 500 etc) we get, we can easily tell what the error is and how to go about resolving it. GraphQL on the other hand, when operated over HTTP, we will always get a 200 OK response status. When an error occurs while processing GraphQL queries, the complete error message is sent to the client with the response
5.) Versioning
Often when consuming third-party REST APIs, we see stuff like v1, v2, v3 etc. which simply indicate the version of the REST API we are using. This leads to code redundancy and less maintainable code. With GraphQL, there is no need for versioning as we can easily add new fields and types to our GraphQL API without impacting existing queries. Also, we can easily mark fields as deprecated and the fields will be excluded from the response gotten from the server
For detailed comparison please visit GraphQl vs Rest
Upvotes: 2
Reputation: 1593
Based on our discussion on gitter,
Since you are building your platform from scratch, you don't need to build your GraphQL layer over REST APIs. That will be double work and is not needed. I recommend that you set up Apollo-Server which will directly interact with your database(s).
You said that you might need to use some legacy APIs for which I recommended Microservices which can easily work with graphql.
Regarding PRISMA, it doesn't give you as much flexibility. It also does not allow ACID compliant updates for fields such as increment or decrement a value. This issue on github can be used to track updates on this feature.
PRISMA is good for getting you set up quickly but I(personal opinion) feel that it is not as flexible.
Your final question was how do you query database/databases without REST API, for which I suggested querying directly from your server.
GraphQL is client side and Apollo will help you to extend it and provide much more features. You don't need to do things much different than what you'd do when creating a REST API other than the fact that you don't need to define separate endpoints for each request. The database interactions remain almost the same other than the fact that you only return/query for the data that you need and this can be done dynamically. From graphql website
Send a GraphQL query to your API and get exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results. Apps using GraphQL are fast and stable because they control the data they get, not the server.
Other mentions: AWS Appsync
Upvotes: 4
Reputation: 11
Using REST to power your GraphQL is like using a 🏇 to power your Tesla. Simply dumb!! Use a database but only select the fields and records asked by GraphQL query, NOT "select *". Use Redis cache if you need the speed.
Upvotes: 0