Tips for creating a component library

If you work in an organization with several applications, you’ll most likely find yourself recreating the same pieces of UI again and again. They’ll look almost the same, of course, and have slightly…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Encapsulating data on GraphQL using Loaders

If you have already used GraphQL, you probably saw that it can consume data coming from the most varied sources, be it a local database or a external API, while centralizing them in a single, self-contained, API.

Here at Entria, we built a structure using files that we call Loaders, which are kinda like a database abstraction layer, but for our GraphQL data sources. Here is a introduction to this simple concept, heavily used everywhere else.

Imagine that your GraphQL server is that guy in the middle

You can use them to wrap your data fetching logic, this way it doesn’t matter where this data is coming from, be it, for example, your MongoDB instance:

or an external API:

Your GraphQL resolvers are not going to know about how this data is fetched, all they expect is a single, well-defined interface, that you have already set:

This allows you to create powerful models for those data sources, permitting you to not have just the data fetching logic isolated, but even more complex logic, like access control rules, which I’ve written about on another post:

The examples above uses just simple functions, but imagine a single Loader file per object type that your GraphQL server exposes, with multiple simple functions for retrieving data related to this object.

Check our GraphQL dataloader boilerplate for a more complete example:

Add a comment

Related posts:

How I build Capital Cities with thier Latitude

As we enter the website, we are greeted by a user-friendly interface. The navigation bar at the top allows us to explore different charts. The website is made with HTML, CSS , Javascript, Bootstrap…

The Books I Read in 2017

In terms of book-reading especially, 2017 was the most joyful and edifying year of my life. By following my gut and my interests, I have absorbed some absolute gems that have deepened and expanded my…

On fulfilling your dreams

When you dream alone, it stay’s a dream. Only when you tell others, it becomes reality. Since my Childhood, I have one dream, that I finally pursue. My mother is a school teacher, this definitely…