Skip to content

Instantly share code, notes, and snippets.

@jamesplease
Last active April 20, 2018 06:53
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/58a89321b62297824ce5327c53138adf to your computer and use it in GitHub Desktop.
Save jamesplease/58a89321b62297824ce5327c53138adf to your computer and use it in GitHub Desktop.

Resource Context API

Context Wrappers

This section is about createContext, Context.Provider and Context.Consumer usage

  • createContext – there will likely be a wrapper around this. createResourceContext?
  • Context.Provider is created for you, as users are not going to be in control of the value in Context
  • Users will likely use a wrapper of Context.Provider that sets up all of the individual resource providers (or maybe just one Provider, if I go the unstated route)
  • Users could use Context.Consumers + Composer, but what is the point? They could just as easily use a single wrapper Consumer that knows about all of the resources

API

  • Subscription-based (that is not necessarily the final language)
  • Subscribe to resources from the cache using a component
  • Also supply fetchers. GET fetches are dispatched immediately, others are not
  • The data returned by the fetchers are merged into your subscription
  • It is similar to Apollo, except the query into the store does not map 1:1 to a request
<Subscribe
resources={{
// Subscribe to some specific books. This will also pull in their
// relationships
// Todo: what is the syntax for multiple subscriptions per resource type?
books: [1, 10, 45],
// Supply a filter function to get some specific authors.
// Todo: verify that this will always get updates when the
// author's metadata changes
authors(resource) {
return resource.meta.isSelected;
},
// access a list.
movies: 'favorites'
}}
fetchers={{
// Todo: how do you pass arguments to the fetcher?
readBook,
deleteBook
}}
>
{({ data, fetchers }) => {
// data is:
//
// {
// books: [],
// authors: [],
// movies: []
// }
//
// Todo: what if you want by-ID lookups here?
//
//
// Fetchers:
// fetchers.readBook(bookId);
// fetchers.deleteBook(bookId);
//
}}
</Subscribe>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment