Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compose different queries to return different results: #15

Open
theodorDiaconu opened this issue Feb 2, 2018 · 6 comments
Open

Compose different queries to return different results: #15

theodorDiaconu opened this issue Feb 2, 2018 · 6 comments

Comments

@theodorDiaconu
Copy link
Contributor

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
Copy link

medihack commented Jun 29, 2018

@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
Copy link
Contributor Author

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

@medihack
Copy link

medihack commented Jun 29, 2018

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
Copy link
Contributor Author

theodorDiaconu commented Jun 29, 2018

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
Copy link

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
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants