apollographql / apollo-ios

📱  A strongly-typed, caching GraphQL client for iOS, written in Swift.
https://www.apollographql.com/docs/ios/
MIT License
3.89k stars 730 forks source link

High likelihood of identical hash values for GraphQLOperation types #3467

Closed aim2120 closed 3 weeks ago

aim2120 commented 3 weeks ago

Question

I recently discovered that GraphQLOperation hashing does not differentiate operations as much as I thought it would. In fact, if two operations both have no variables, they have the same hash value, even if they're entirely different operation types.

This is due to the way hash(into:) is implemented for GraphQLOperation

public extension GraphQLOperation {
  var __variables: Variables? {
    return nil
  }

  func hash(into hasher: inout Hasher) {
    hasher.combine(__variables?._jsonEncodableValue?._jsonValue)
  }
}

This seems like an unfortunate design decision. A few questions for the maintainers of the project:

I've already updated my usage of the hash value based on this finding, but wanted to create an issue in case it may affect others.

calvincestari commented 3 weeks ago

Thanks for raising the issue @aim2120. Are you using the hash value from an operation directly that you noticed this?

github-actions[bot] commented 3 weeks ago

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.

calvincestari commented 3 weeks ago

@aim2120, the change is merged and will go out in the next release.

aim2120 commented 2 weeks ago

Hey @calvincestari, thanks for getting that updated! Yes, I was using the hash value in an interceptor implementation when I noticed the behavior.