GraphQL best practices for high performance data fetching from database?
See original GitHub issueHello,
How can I improve performance of GraphQL queries? This is a general question, I know. I’m looking for best practices on how to implement GraphQL with graphql-java.
I have a simple schema:
type Account @java(package:"com.example") {
id: ID!
users: [User]!
}
type User @java(package:"com.example"){
id: ID!
email: String!
firstName: String
lastName: String
}
Making the following query:
{
accounts { id users { id } }
}
takes around ~19s -> ~50s on my machine and the staging server to produce: query-results.txt
Data is fetched from a database with a code that looks like this:
@Override
public List<Account> getAccounts() {
List<gr8pi.web.platform.Account> allAccounts = accountRepository.findAll();
return allAccounts.stream().map(a -> {
List<gr8pi.web.candidate.Respondent> respondents = respondentRepository.findAll();
List<Membership> memberships = membershipRepository.findByAccountId(a.getId());
List<User> users = memberships.stream()
.map(membership -> new User.Unresolved.Builder()
.withId(Long.toString(membership.getUser().getId()))
.withEmail(membership.getUser().getEmail())
.withFirstName(membership.getUser().getFirstName())
.withLastName(membership.getUser().getLastName())
.build())
.collect(Collectors.toList());
return new Account.Builder()
.withId(Long.toString(a.getId()))
.withRespondents(Collections.EMPTY_LIST)
.withUsers(users)
.build();
}
).collect(Collectors.toList());
}
I’m using spring data repository and hibernate 5.2 and the database is PostgreSQL. The code is very simple and I do believe some improvements can be made, but the point is, in my opinion : you can’t get away without making a lot of calls to the database.
My question is: What are the best practices on implementing good performing data fetching from database systems.
Thanks,
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (4 by maintainers)
Top Results From Across the Web
GraphQL Best Practices
Following are brief descriptions of some of the more common best practices and opinionated stances held by GraphQL services, however each article in...
Read more >Designing a GraphQL server for optimal performance
To be efficient, we would expect to execute only two queries to retrieve the data from the database: one to fetch the directors'...
Read more >Connecting to data sources | Full-Stack Quickstart
Fetch data from multiple locations. Now that we've constructed our schema, we need to connect data sources to Apollo Server. A data source...
Read more >Architecture of a high performance GraphQL to SQL engine
We can determine the tables that are needed by a GraphQL query and generate a single SQL query using joins to fetch all...
Read more >Building a High Performance Realtime GraphQL API on ...
I will talk about the approach we took at Hasura of building a GraphQL API that can leverage an existing database. You'll learn...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Also have a look at idea behind DataLoader : https://github.com/facebook/dataloader
It allows for combining common look ups into less calls (if it suits your app)
There is a java implementation : https://github.com/engagingspaces/vertx-dataloader
I havent used the above one however
In terms of pagination in graphql, this is your responsibility as a schema designer.
The list fields you give back can contain “pagination” arguments and pagination “info”
The Relay specification has pagination built in : https://facebook.github.io/relay/docs/graphql-connections.html
This also outlines some ways to do pagination : http://graphql.org/learn/pagination/
The point is pagination is not a graphql engine thing - its the schema designers repsonsibility