cult-of-coders / grapher-react

Provides easy to use React Components that are suitable for grapher package.
https://atmospherejs.com/cultofcoders/grapher-react
38 stars 19 forks source link

Compose different queries to return different results: #15

Open theodorDiaconu opened 6 years ago

theodorDiaconu commented 6 years ago

One component may need two different data sources to compose it's output.

compose({
   users: withQuery(() => {}, {}),
   properties: withQuery(() => {}, {})
}, {
   anyIsLoadingTemplate: => ?
})(Component)

Component props =>

{ users : { isLoading, error, data } },
{ properties : { isLoading, error, data } },
medihack commented 6 years ago

@theodorDiaconu Is there already a solution for using multiple queries with grapher-react? I really like its API, but I can't find an example how to do it :-(

How about something like

withQuery(props => {
  return {
    posts: getPostLists.clone(),
    tags: getTagList.clone()
  }
})(Component)

and then access it in the component with:

props.posts.isLoading
props.tags.isLoading

and so on.

But I am not sure if this is doable as I just started to use grapher and grapher-react.

theodorDiaconu commented 6 years ago

You can now use compose: https://github.com/acdlite/recompose and do this

medihack commented 6 years ago

Ah, I get it. Your initial post here is a hint how to do it, and not something that is planned :-) Would be nice to have an example in the Readme. Thanks for grapher! I just came back to Meteor after some time using other stuff and grapher looks really cool.

theodorDiaconu commented 6 years ago

This is a nice to have in the library too, but currently can be done in so many ways since withQuery respects HOC pattern. Glad you enjoy Grapher. A lot of work has been put behind the scenes.

medihack commented 6 years ago

For others interested, here is a solution I came up with ...

export default compose(
  namespace(
    'posts',
    withQuery(() => {
      return postListQuery.clone()
    })
  ),
  namespace(
    'tags',
    withQuery(() => {
      return tagListQuery.clone()
    })
  )
)(Component)

with using the following namespace helper function

import { compose, withProps, mapProps } from 'recompose'

const namespace = (ns, ...hocs) =>
  compose(
    withProps(props => ({ $parentProps: props })),
    ...hocs,
    mapProps(props => ({ [ns]: props, ...props.$parentProps }))
  )

export default namespace

And yes, a built in API would be nice to have. Maybe one that even has a common loadingComponent and errorComponent.

erixtekila commented 6 years ago

+1