Recompose: Getting all render arguments in fromRenderProps

Created on 25 Jun 2018  路  2Comments  路  Source: acdlite/recompose

Apollo React's <Mutation /> component passes multiple arguments in its render prop function (https://www.apollographql.com/docs/react/essentials/mutations.html#basic). This means that when using fromRenderProps, only the mutator is passed and not the remaining arguments.

Maybe we can refactor this line (https://github.com/acdlite/recompose/pull/677/files#diff-73d23a5af881a3abeef802a6a9db71f6R15) and spread the arguments when passing to propsMapper?

...
[renderPropName]: (...props) =>
  baseFactory({ ...ownerProps, ...propsMapper(...props) }),
...

Most helpful comment

@itsacezon Thanks for the feedback. :)

I created a PR #694 which supports for multiple arguments in fromRenderProps HOC.
Would you like to share more detail about your use case for using Apollo with fromRenderProps?

All 2 comments

@itsacezon Thanks for the feedback. :)

I created a PR #694 which supports for multiple arguments in fromRenderProps HOC.
Would you like to share more detail about your use case for using Apollo with fromRenderProps?

Thanks for the quick response! 馃帀

I'm making a generic HOC that returns a decorated mutate function that passes the arguments as the variables object to the GraphQL query. Instead of using the graphql HOC provided, I have to use the <Mutation /> component since it also returns an object that has useful info such as loading, data, etc. The fromRenderProps update made my life easier!

Here's an excerpt; my goal is to reduce boilerplate code for GraphQL mutations:

function withMutation (mutation, config = {}, mutateOptions = {}) {
  const handlerName = config.name || 'mutate';
  const _Mutation = withProps({ mutation, ...config })(Mutation); // from `react-apollo`

  return compose(
    fromRenderProps(_Mutation, (mutate, result) => ({ mutate, result }),

    withHandlers({
      [handlerName]: ({ mutate, ...rest }) => params => {
        // So options like `optimisticResponse` and `variables` can use the parameters passed
        // when calling the mutator, as well as the owner props (including the `result` object)
        const options = typeof mutateOptions === 'function'
          ? mutateOptions(params, rest)
          : { ...mutateOptions, variables: params };

        mutate(options);
      },
    })
  );
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

Secretmapper picture Secretmapper  路  3Comments

franklinkim picture franklinkim  路  3Comments

jethrolarson picture jethrolarson  路  4Comments

jeron-diovis picture jeron-diovis  路  4Comments

astanciu picture astanciu  路  3Comments