Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React-apollo update not reloading after mutation if I transform data before the render()

I have wrapped for both Query and Mutations so I can globally handle the repeat actions that need to happen with each Query, Mutation. In the query I transform the data so I don't need to worry about all the nodes, edges, etc

I am using react-adopt to wrap all my query and mutations components into one render prop back on the view layer.

Works - Page will re-render once a mutation has taken place

<ApolloQuery>

export const ApolloQuery = ({
  query: query,
  render,
}) => {
  return (
    <Query query={query}>
      {({ data }) => {
        return (
          <Fragment>
               render(data)
          </Fragment>
        )
      }}
    </Query>
  )
}

A Component

export default externalProps => {
  return (
    <QueryContainer {...externalProps}>
      {({ someQueryData, aMutation }) => { //react-adopt render props
        const { nestedData } = new apolloClass(someQueryData).start()
        return (
          <Grid container spacing={16}>
            {nestedData.map((ticket, index) => (
               {...Mutation button in here}
            ))}
         </Grid>
        )
      }}
    </QueryContainer>
  )
}

Does not work - Page does not re-render but cache is updated with correct records

<ApolloQuery>

    <Query query={query}>
      {({ data }) => {
        const transformedData = new apolloClass(data).start() //move transform into render
        return (
          <Fragment>
               render(transformedData)
          </Fragment>
        )
      }}
    </Query>

A Component

export default externalProps => {
  return (
    <QueryContainer {...externalProps}>
      {({ someQueryData: { nestedData }, aMutation }) => {
        return (
          <Grid container spacing={16}>
            {nestedData.map((ticket, index) => (
               {...Mutation button in here}
            ))}
         </Grid>
        )
      }}
    </QueryContainer>
  )
}

So now, the page will not update after a mutation if I move the apolloClass to transform before the render of the query

like image 874
Jamie Hutber Avatar asked Nov 06 '22 15:11

Jamie Hutber


1 Answers

Most likely you need to set refetchQueries or awaitRefetchQueries in the mutation options to force Apollo updating those queries and hence triggering a re-render.

like image 125
SirPeople Avatar answered Nov 14 '22 22:11

SirPeople