apollographql / apollo-feature-requests

🧑‍🚀 Apollo Client Feature Requests | (no 🐛 please).
Other
130 stars 7 forks source link

react-apollo mutations composition with non react function #86

Open davidalekna opened 5 years ago

davidalekna commented 5 years ago

Hi! So the problem I'm trying to solve is not actually a problem, more like a cleanup solution to components that have mutations and queries and other compositions all together. React-apollo package has a { graphql } export that composes really well with react components, but I couldn't find a solution to simply compose mutations on a simple function that could be called from react component. Something like this:

const mutation1 = graphql(query, props)
const mutation2 = graphql(query, props)

const someFunction = compose(
  mutation1,
  mutation2
)(async ({ mutation1, mutation2, ...rest }) => {
  await mutation1(rest)
  await mutation2(rest)
  ...
})

function ReactComponent() {
  async function onClick() {
    await someFunction(props)
  }

  return <button onClick={onClick}>click</button>
}

I was wondering is there a way to compose those with something like ramda, or a custom composition function? Maybe something worth adding to the package...

walt-er commented 5 years ago

You're in luck! It looks like the graphql() function does accept a mutation instead of a query: https://www.apollographql.com/docs/react/api/react-apollo/#graphql-options-for-mutations

As far as queries and mutations together, I think you'll ways need to fire them as separate requests. GraphQL doesn't accept a Query and Mutation at the same time, but note that Mutations will return the new value once they're done mutating. Not sure if that's exactly what you meant but something to note.