question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

updateQueries will only update cache if keys in mutation variables exactly match mutation signature

See original GitHub issue

Hi there! I am new to graphql and apollo and building a small issue tracker to try them out. I have come across this problem while trying to use updateQueries, to update a list after a mutation and I am not sure if this is intended behaviour or a bug: if the payload of the mutation does not contain exactly the same keys as the parameters in the mutation signature, the return value of updateQueries will be ignored.

I have created a simple representation of the problem as follows:

query

const query = gql`
    query issues{
        issues {
            id
            title
            description
            project{
                id
                title
            }
        }
    }
`;

mutation

const mutation = gql`
    mutation createIssue($title: String!, $project: String!, $description: String){
        createIssue(title: $title, project: $project, description: $description){
            id
            title
            description
            project{
                id
                title
            }
        }
    }
`;

mutation HOC

const withMutation = graphql(mutation, {
    props: ({ownProps, mutate}) => {
        return {
            createIssue: data => mutate({
                variables: data,
                updateQueries: {
                    issues: (prev, action) => {
                        console.log(action);
                        const {mutationResult} = action;
                        const newIssue = mutationResult.data.createIssue;
                        return {
                            issues: [newIssue, ...prev.issues]
                        }
                    }
                }
            })
        }
    }
});

code calling mutation

const issue = {
    title: faker.hacker.phrase(),
    project: projectsIds[Math.floor(Math.random()*projectsIds.length)],
    description: 'test',
};

this.props.createIssue(issue).then(() => {
    console.log('created issue');
});

If I leave out the optional description key from the issue object, updateQueries will still run, but its return value will not be used to update the cache.

Is this intended behaviour, and if so, what is the reason it should work this way?

Thank you.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
bknifflercommented, May 3, 2017

@mkozhukharenko I’m having the same exact issue. After a lot of digging and trying, I found out that the query mutation fields need to match the query that you want to update via updateQueries, or else something weird happens and the value will be undefined.

The linked issue seems to go into more detail.

1reaction
FreddieRidellcommented, Feb 5, 2017

I believe this is the right issue: I’m also unable to add an item to a collection using updateQueries, if that collection is empty to begin with.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutations in Apollo Client - Apollo GraphQL Docs
If your mutation returns all of the objects and fields that it modified, you can update your cache directly without making any followup...
Read more >
Building a GraphQL-powered mobile application With React
Binding Variables To GraphQL Queries; Merging Queries; Adding Client-Side Cache; Calling Mutations; Reacting To Mutations; Updating The UI In An ...
Read more >
React with Apollo and GraphQL Tutorial - Robin Wieruch
Apollo Client was able to match the mutation result with the repository identifier to the repository entity in Apollo Client's cache, the props ......
Read more >
How to update the Apollo Client's cache after a mutation
The Apollo Client is used to fetch data from any GraphQL server. ... true variable to match the exact query we need to...
Read more >
Apex Developer Guide - Salesforce Implementation guides
data types, variables, and if-else statements. You can ... are not deleted while required by active Apex code. ... If no value matches,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found