Im using the withApollo HOC when i submit a form t...
# prisma-whats-new
s
Im using the withApollo HOC when i submit a form to run some mutation, the problem is is that the data that is returned is not passed into my props, does anyone know how to access this data?
n
withApollo
injects a client instance that allows you to create a promise that sends a query.
then
-ing the promise gives access to the data
if you want to have access to the data via
props
, you need to use the
graphql
HOC instead
c
@samjbmason shoot me a sample of the code you're trying to get working and I'll be happy to help 🙂
s
@cj This the handle submit of the form
Copy code
handleSubmit: (payload, { props }) => {
    props.client
      .query({
        query: findUserQuery,
        variables: { email: payload.email }
      })
      .then(({ data }) => {
        if (data.User != null) {
          return props.client.mutate({
            mutation: createRideWithExistingUserMutation,
            variables: { userId: data.User.id, ...rideObject(payload) }
          })
        }
        return props.client.mutate({
          mutation: createUserAndRideMutation,
          variables: { email: payload.email, ...rideObject(payload) }
        })
      })
      .catch(e => console.log(e))
  }
problem is the data being returned is not then passed into props from the cache
@nilan Becuase Im doing some logic on deciding what mutate to run is ther a way to do this with the graphql HOC
c
@samjbmason sorry, kids got home... why wouldn't you just use
graphql
as @nilan suggested and then do something like:
Copy code
handleSubmit: async (payload, { props }) => {
  const defaultData = { ...rideObject(payload) }
  const { User } = await props.findUser({ email: payload.email })

  if (User) {
    return props.createRideWithExistingUser({ userId: User.id, ...defaultData })
  } else {
    return props.createUserAndRide({ email: payload.email, ...defaultData })
  }
}
I'll be back a bit later, need to get them to bed
s
this line looks good but I didnt think it was possible to calla query from a prop
const { User } = await props.findUser({ email: payload.email })
c
@samjbmason that's what
import { graphql } from 'react-apollo'
is for http://dev.apollodata.com/react/higher-order-components.html
s
@cj But I thought queries passed into graphql, run immediately on load and that it was only mutations that you can call inside a component through props.
Where are the docs that say I can run a query through props
@cj Did you maybe mean use the
withApollo
HOC for the query and then use
graphql
for the mutations therefore being able to access the mutation results in the props
d
@sambmason - if you use withApollo then you are doing the graphql query imperatively inside your component, so it would not make sense for the data to go into props (which are for things passed into your component from outside). You should use setState in the then() part of the promise returned by props.client.query (or dispatch an action if you are using redux for state) and then have your component render based on state rather than props
s
@dankent I ended up with a mix of withApollo and graphql, I set up the mutations with
graphql
so I could use them in the props with
props.mutate
and then used the withApollo to setup a
props.client.query
to fetch the the user in the submit handler and the pass that value to the mutations. It seemed to work pretty well