Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confused about cache behavior #3383

Closed
guendev opened this issue May 18, 2024 · 2 comments
Closed

Confused about cache behavior #3383

guendev opened this issue May 18, 2024 · 2 comments
Labels
question Issues that have a question which should be addressed

Comments

@guendev
Copy link

guendev commented May 18, 2024

Question

Why doesn't the object update automatically when calling mutation?
My workflow: query setting -> mutation updateSetting. After that, if I query settings, It will return an updated object from cache (updated by updateSetting) if they have the same cache-id. It always works on javascript.

But when I did the same with apollo-ios. Cache doest update, it always returns the value from the first time.

Btw, are there any way to print all existing object?

My swift code

        Network.shared.apollo.perform(mutation: UpdateMotherTongueMutation(input: input)) { result in
            guard (try? result.get().errors) == nil else {
                return
            }
            // call function to query:  `SettingQuery`
        }
        
        
        Network.shared.apollo.fetch(query: SettingQuery()) { [weak self] result in
            guard let self = self else { return }

            let data = try? result.get().data
            // save data...
        }

My Operations

query Setting {
  setting {
	id
	activeCourse {
	  id
	}
	motherTongue {
	  id
	  name
	  code
	}
	...more fields
  }
}

mutation UpdateMotherTongue($input: UpdateSettingInput!) {
  updateSetting(input: $input) {
	id
	motherTongue {
	  id
	  name
	  code
	}
  }
}
@guendev guendev added the question Issues that have a question which should be addressed label May 18, 2024
@calvincestari
Copy link
Member

Hi @guendev, it sounds like you haven't configured a cache key that Apollo iOS can use to normalize the object from your query and mutation. The web client has a more advanced cache and may be doing some of that automatically for you whereas Apollo iOS requires you to be explicit about identifying similar objects.

I suggest reading the Normalizing responses section of the documentation which explains how responses are normalized without a custom cache key. Then move on to reading the Normalizing objects by cache key section which will guide you how to return a custom cache key for your object. Once you've got the custom cache key configured correctly you should get the behaviour you're looking for.

I'm going to close this issue as there is no action for us to take here but I'm happy to continue the conversation with you here if you can't figure out the cache key.

@calvincestari calvincestari closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
Copy link
Contributor

Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo iOS usage and allow us to serve you better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issues that have a question which should be addressed
Projects
None yet
Development

No branches or pull requests

2 participants