Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andreas/ocaml-dataloader
Dataloader is a utility for batching and caching when fetching data, in particular for GraphQL.
https://github.com/andreas/ocaml-dataloader
graphql
Last synced: 16 days ago
JSON representation
Dataloader is a utility for batching and caching when fetching data, in particular for GraphQL.
- Host: GitHub
- URL: https://github.com/andreas/ocaml-dataloader
- Owner: andreas
- Created: 2017-12-01T21:34:04.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-12-02T05:53:53.000Z (almost 5 years ago)
- Last Synced: 2024-08-04T01:28:40.344Z (3 months ago)
- Topics: graphql
- Language: OCaml
- Homepage:
- Size: 4.88 KB
- Stars: 44
- Watchers: 4
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-list - ocaml-dataloader
README
Dataloader is a utility to be used for your application's data fetching layer to provide batching and caching, in particular with [`ocaml-graphql-server`](https://github.com/andreas/ocaml-graphql-server). It is a port of [facebook/dataloader](https://github.com/facebook/dataloader) for Node. The library is still under active development.
This repo contains two packages:
- `dataloader`, which is IO-agnostic written in CPS-style.
- `dataloader-lwt`, which is a shim on top of `dataloader` for [`Lwt`](https://github.com/ocsigen/lwt).## Example
The following examples assumes a function `batchLoadUsersFromDatabase` of the type `user_id list -> (user list, exn) result Lwt.t`:
```ocaml
let user_loader = Dataloader_lwt.create ~load:(fun user_ids ->
batchLoadUsersFromDatabase user_ids
)(* triggers only 1 query rather than 5 *)
List.map (fun user_id ->
Dataloader_lwt.load user_loader user_id
) [1; 2; 3; 4; 5]
```The function `~load` provided to `Dataloader.create` must uphold the following constraints:
- The list of values must be the same length as the list of keys.
- Each index in the list of values must correspond to the same index in the list of keys.