relay: Relay modern Mutation changes in relay store but doesn't updates the UI
I don’t know if it’s an issue, I’m new to Relay… After doing the mutation in relay modern,
const mutation = graphql`
mutation addPostMutation (
$input: addPostData!
) {
addPost(data: $input) {
node {
id
content
}
}
}
`;
commitMutation(
environment,
{
mutation,
variables,
onCompleted: (res) => {
console.log(environment)
console.log(res)
},
onError: err => console.error(err)
}
)
when I console.log environment, I can see the newly added post in the relay store, but it’s not updated in the UI. However, after refreshing the app, the changes are made. This is my schema.graphql
schema { query: Root mutation: Mutation }
type Mutation { addPost(data: addPostData!): addPostMutation }
input addPostData { userID: String, post: String, tags: [String] }
type addPostMutation { node: postNode }
interface Node { id: ID! }
type postEdges { node: postNode }
type postNode implements Node { id: ID! content: String }
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 6
- Comments: 51 (11 by maintainers)
Just giving this a bump as we are having the same problem.
There is no clarity on how to update your UI on a Mutation. The only source of information we have is from GitHub.
We do not use an edge/node schema in the same way that the Facebook Todo example does, thus making it incredibly difficult to find any indication of the way to update our store and UI with a successful mutation.
Most of all, it isn’t clear why the UI doesn’t refresh with the store changes using :
Would be good to have some clearer documentation on the mutations and updaters.
Thanks @cipater! That, plus reading the RelayConnectionHandler source, gave me the last bits I needed. It works now! 😄
Posting my solution here for others. This is my updater function:
The fragment in my ReportList component:
yeah right. What other approach can we use other than manually updating the store?
The response is normalized in the store, but it is not appended in
getPostsQuery
Basically, you want something like thiswe have this helpers to update connections
Usage:
Returning an edge from mutation makes things easier, but you could also create the edge on client
Hi, I am running into the same issue here (relay 1.4.0). I’m using the viewer pattern, but I am NOT using connections and pagination as I don’t require that at the moment.
The mutation I am performing gets correctly updated into the relay store, and is correctly inserting records through my updater function, but Relay never automatically updates the UI.
My work around at the moment is to run a completed callback which runs setState on the component to manually update the UI. I am happy with that for now. But it would be nice to know how to get Relay to update the UI after a mutation is successfully performed?
For anyone interested here is how my updater and onCompleted function looks like. is there anything I am missing here?
Also I am using found-relay for my routing. I haven’t heard this mention by anyone. But wondering if this is an element tat could be affecting this issue as well?
Example graphQL mutation input:
Example graphQL mutation payload:
Updater and onCompleted mutation configs:
Schema: here’s the related schema with relevant sections included:
My query on the component
and the related fragment…
hmm, I don’t think @saudpunjwani101 is asking about pagination container. What I mean by “refetch” is that you could refetch the connections in the mutation payload. However, this involves changes in your current schema. I’m not sure how to modify your schema, but we usually have
Then the query fragment will be
And the mutation will be
Then there is no need to have your
updater
function. The one downside of this is that it will be cumbersome to provide an optimistic response.In your case, if you really want to fetch many edges in the query, then it makes sense to use
@connection
and give a customizedupdater
function because it will be too costly to fetch everything again in the mutation. In that case, you could just fetch the newly created edge and use helper functions inRelayConnectionHandler
to update the client side (and this is exactly what you are doing now).check this post https://medium.com/entria/wrangling-the-client-store-with-the-relay-modern-updater-function-5c32149a71ac by @ssomnoremac
I got struck with this problem where it didn’t update the UI in React-native App but work in Web. Thank you @erikfrisk for saving my time not to move to Apollo.
I had a similar problem: I needed to update specific query when mutation was committed. I had this query in my header component
where = { id: { eq: '7' } }
So in relay store there was record
Person{"where":{"id":{"eq":"7"}}}
and it was null.In my mutations config in
updater
function i wrote this:my mutations response looks like this: { login: { user: { id: …} } }
I know it’s very specific case but now it’s possible to work with it further. Basically the idea is to get whatever comes from the server find the needed record in the relay store and just reset it.
Or if you have
MeQuery
query all you need to do to update it in relay store isstore.getRoot().setLinkedRecord(record, 'MeQuery');
@saudpunjwani101 A common practice is to have a structure such as: