-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Recommend GraphQL for APIs with multiple data sources #714
Open
MalcolmTomisin
wants to merge
1
commit into
main
Choose a base branch
from
react/guide-graphql
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is an API that pulls from different sources? Do you mean situations where you might want to read data from multiple database tables on the backend? Or do you mean hitting multiple different servers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean both scenarios:
Aggregating data from multiple database tables: For example, an API might fetch user information from a users table and their purchase history from an orders table. This is typically done using database joins or similar techniques, allowing the backend to combine the data and return it via a single endpoint.
Fetching data from multiple servers or APIs: In more complex systems, data might come from entirely separate services or third-party APIs. For instance, an API might pull weather data from one service and location details from another, combining the responses before sending them to the client.
GraphQL is particularly useful in both scenarios because it abstracts the complexities of aggregation—whether it's combining data from tables or integrating multiple APIs. This allows the client to request exactly what it needs in one query, avoiding under-fetching or over-fetching data.
I hope that provided clarity on what I meant by "different sources".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarifying! I'm not sure either of these are properties of GraphQL itself?
The first is an API design issue. Ideally a backend doesn't expose it's database tables directly via an API. Even in a REST API, endpoints shouldn't be 1-1 with your database tables. You should be able to rename a column or split a table without affecting your API. With a properly normalized database, it's common for even REST APIs to join multiple tables in a single endpoint. In this sense, I think every API is "pulling from multiple data sources"?
That said, it's possible that a REST API won't give you everything you want in a single endpoint and that traversing the graph allows you to fetch data in a single request that might otherwise be two. I think that's the original selling point of GraphQL as a technology.
The second scenario (pulling from multiple different APIs) is interesting because I think that might be a feature of local tooling (Apollo maybe?) rather than the protocol itself? AFAICT, GraphQL as a protocol doesn't support pulling from multiple different APIs. Maybe the argument here is that the JS world has better GraphQL tooling than REST tooling so that's why we prefer GraphQL APIs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious about this idea of GraphQL as aggregation tool 🤔. I think there are two forms of aggregation happening here: aggregating data, and seamlessly issuing multiple HTTP requests.
I imagine you still need to aggregate the data once you get it back, regardless of how you get it? You almost certainly don't want data in the shape the server gives it to you, especially when combining multiple sources. I don't know that either GraphQL or REST help you here.
It sounds like like GraphQL tooling (Apollo?) can allow you to abstract over multiple requests, avoiding the need to write code like: