grapher-react icon indicating copy to clipboard operation
grapher-react copied to clipboard

Compose different queries to return different results:

Open theodorDiaconu opened this issue 7 years ago • 6 comments

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 } },

theodorDiaconu avatar Feb 02 '18 15:02 theodorDiaconu

@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.

medihack avatar Jun 29 '18 19:06 medihack

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

theodorDiaconu avatar Jun 29 '18 20:06 theodorDiaconu

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.

medihack avatar Jun 29 '18 20:06 medihack

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.

theodorDiaconu avatar Jun 29 '18 20:06 theodorDiaconu

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.

medihack avatar Jun 30 '18 05:06 medihack

+1

erixtekila avatar Sep 05 '18 22:09 erixtekila