Skip to content

Instantly share code, notes, and snippets.

@jamesplease
Last active April 3, 2018 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesplease/29647b4ff7c6d21377ee93583acf4af7 to your computer and use it in GitHub Desktop.
Save jamesplease/29647b4ff7c6d21377ee93583acf4af7 to your computer and use it in GitHub Desktop.

Utilities for bad APIs

Goals:

  • Write resolvers that allow you to do "complex" queries in one pop, such as fetching the entire definition of an object
  • "Push" updates so that the slowest endpoint doesn't hold up the entire request
// Create a fetcher. Fetches some data 
const fetchBook = createFetcher(
  // The fetcher
  (movieId) => {
    return fetch(`/books/${movieId}`);
  },
  // The transformer
  (result) => result.book)
);

// Actually do the fetch
fetchBook('1042');

// okay, how about when you need to fetch the book for an author?
// Fetch an author
// I think this would require a model definition which is stupid and I hate it
fetchAuthor(authorId, {
attributes: [
'name',
'books',
'comments'
]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment