GraphQL Best Practices Resources and Design Patterns

GraphQL become the hottest trend in API design. We already discussed GraphQL in various sections such as the comparison of API architectural styles, as many considerations for GraphQL and RESTful API are similar, since it all boils down to for machines to communication with each other. However, given how popular GraphQL is, it now deserves its own section for our curated collection of best resources.

What is GraphQL?

Should you use GraphQL?

Understand the advantages of GraphQL is super important, but at same time it is worth a while to see if your project needs is suitable for it. So there are many articles written on this topic. Here are some below:

GraphQL design patterns and best practices

Graphql homepage do cover some highlevel best practices, but it doesn’t go into a lot of depth into each topic. There are others articles that goes deeper into each of these topics.

Transport layer: protocol and JSON

Modeling best Practices

For any application, data model will drive many of the design decisions down the road, and can have huge impact on performance and usability of your app.

API Versioning

How to do GraphQL versioning correctly? RESTful API versioning has multiple strategies with their pros and cons. GraphQL best practices discourages versioning, because in GraphQL the clients determines what fields or values needs to be returned. Therefore, well designed GraphQL APIs should just continue to return the fields that the clients needs.

As the query patterns for GraphQL can vary so much more, using analytics to understand your API usage can be even more important. If you are curious, we here at Moesif decided to support GraphQL from the ground up when we started our API analytics platform.

Mutations

Mutations in GraphQL is surprising tricky. Below are some of the articles on best practices.

Should you use Null?

When client requests a data or an object that doesn’t exist, how should you handle it? Unlike RESTful API, where there is 404, you’ll have to hand the concept of null. Here are some good articles on best practices:

Pagination in GraphQL

Like pagination in REST APIs, there are multiple approaches for pagination in GraphQL, such as cursor based or offset based. There are pros and cons to both.

Caching and Batching

For GraphQL, one of the downside is that you can’t use the HTTP semantics and many standard cache features for browsers and web servers that is battle tested. The team at GraphQL already support a framework called DataLoader for cache.

Authentication and Authorization

What if you want to set permissions by resources? This article talks about some the best strategy for that.

Other topics

Architectural Aspects

Below are the framework to think about how to architecting your backend:

  • Interface Layer: This is how client interfaces with your backend. And in many ways, GraphQL is really this layer. Many applications will let client decide to interface with GraphQL or RESTful. It is important to refactor your application to
  • Cross Cutting Concerns Layer: (authorization, authentication, logging, permissions, etc.)
  • Business Logic Layer: This is your actual business logic.
  • Data Layer (or Persistence Layer)

After understand that framework, then look at some of the below articles on some of specific architectural choices.

GraphQl resources by programming language

Since GraphQL is cross platform, there are different frameworks, libraries, and tools for different languages.

It is not a surprise that Javascript have the most available tools and libraries. I have an article dedicated to this topic on the tech stack for the GraphQL in the Node.js ecosystem. That article gives a particular breakdown of all the popular technologies in the GraphQL Node.js ecosystem such as

On APIGudie.io, each language specific resources page have sections dedicated to GraphQL. I’ll link them here for convenience:

Summary

We do realize the above list isn’t complete. Please let us know if there is any good resources you like by either comment below or send us a message.

Learn More About Moesif Build Better API Products With Moesif 14 day free trial. No credit card required. Learn More
Monitor Your GraphQL APIs With Moesif Monitor Your GraphQL APIs With Moesif

Monitor Your GraphQL APIs With Moesif

Learn More