1.0.2 • Published 2 years ago
@nazaire/resolve-with v1.0.2
@nazaire/resolve-with
Chain related promises together in a fully typed graph-like structure. Works quite nicely with DataLoader
npm install --save @nazaire/resolve-with
Example Usage
import { resolveWith, resolveManyWith } from "@nazaire/resolve-with";
let getBookById: (
id: string
) => Promise<{ id: string; title: string; authorId: string }>;
let getTagsForBook: (
bookId: string
) => Promise<{ bookId: string; name: string }[]>;
let getAuthorById: (
id: string
) => Promise<{ id: string; name: string; favouriteBookId: string }>;
let getRelatedBooks: (
id: string
) => Promise<{ id: string; title: string; authorId: string }[]>;
const bookQuery = (bookId: string) => {
return resolveWith(getBookById(bookId), {
// 1. new promise using result
author: (book) => getAuthorById(book.authorId),
// 2. parallel promises just for convenience
tags: getTagsForBook(bookId),
// 3. complex nested relations
relatedBooks: resolveManyWith(
getRelatedBooks(bookId),
(relatedBook) => ({
author: resolveWith(getAuthorById(relatedBook.authorId), {
favouriteBook: (author) => getBookById(author.favouriteBookId),
}),
})
),
});
};
const book = await bookQuery("123");
book.value.title; // string
book.author.name; // string
book.tags[0].name; // string
book.relatedBooks[0].value.title; // string
book.relatedBooks[0].author.value.name; // string
book.relatedBooks[0].author.favouriteBook.title; // string
1.0.2
2 years ago
1.0.2-alpha.0
2 years ago
1.0.1-alpha.4
2 years ago
1.0.1-alpha.3
2 years ago
1.0.1-alpha.2
2 years ago
1.0.1-alpha.1
2 years ago
1.0.1-alpha.0
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago