简体   繁体   中英

Wrap APIs from multiple data sources in Node.js

I'm now developing a middleware layer for new project based on Node.js. It needs to retrieve data from both the "pure backend APIs(RESTful APIs)" and some databases(MongoDB, MySQL, etc.), then mix those data with some conditional filters and expose as new APIs for all clients.

At this moment, I just wrap those data getters as Promises and filter the data which I need. But I wonder if there's any graceful solution for that requirements.

Here're some approach candidates:

  • LoopBack : LoopBack framework can define models beyond multiple data source. However, it seems like LoopBack only support "model level" multiple datasource, and doesn't support "fields level" data source, which means that we couldn't define a specific model with some fields from databases and the others from RESTful APIs.

  • GraphQL : I'm new for GraphQL, however it seems like GraphQL could have some connectors for both RESTful APIs and databases.

Any other packages or libraries are appreciated! Thanks all!

This is a great use case for GraphQL.

Before going into details, note, though, that GraphQL is best used with some sort of a GraphQL client, Relay in particular if you're using React. If you don't have a GraphQL client already, you can still use GraphQL (it's simple HTTP GET/POST after all) but the queries you construct will need to be a bit more bulky compared to pure REST -- however, you'll get much better flexibility in return!

GraphQL is quite complex, here some pointers:

GraphQL is definitely a significant investment, be sure you have the time to study it through. It's also still pretty new, so you won't find too many recommended practices yet.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM