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 728 forks source link

Persisted query operation manifest generation occasionally fails to produce unique hashes #3270

Closed aaronroggow closed 1 year ago

aaronroggow commented 1 year ago

Summary

When generating persisted query operation manifests, we are seeing that the generation occasionally fails to produce unique hashes for every operation.

Normally, our manifest would be generated like so:

{
  "format": "apollo-persisted-query-manifest",
  "version": 1,
  "operations": [
    {
      "id": "OPERATION_BODY_1_HASH",
      "body": "OPERATION_BODY_1_BODY",
      "name": "OPERATION_BODY_1_NAME",
      "type": "mutation"
    },
    {
      "id": "OPERATION_BODY_2_HASH",
      "body": "OPERATION_BODY_2_BODY",
      "name": "OPERATION_BODY_2_NAME",
      "type": "mutation"
    },
    {
      "id": "OPERATION_BODY_3_HASH",
      "body": "OPERATION_BODY_3_BODY",
      "name": "OPERATION_BODY_3_NAME",
      "type": "mutation"
    }
  ]
}

But occasionally it gets generated like this instead:

{
  "format": "apollo-persisted-query-manifest",
  "version": 1,
  "operations": [
    {
      "id": "OPERATION_BODY_1_HASH",
      "body": "OPERATION_BODY_1_BODY",
      "name": "OPERATION_BODY_1_NAME",
      "type": "mutation"
    },
    {
      "id": "OPERATION_BODY_1_HASH",  <--- DUPLICATED HASH ID
      "body": "OPERATION_BODY_2_BODY",
      "name": "OPERATION_BODY_2_NAME",
      "type": "mutation"
    },
    {
      "id": "OPERATION_BODY_3_HASH",
      "body": "OPERATION_BODY_3_BODY",
      "name": "OPERATION_BODY_3_NAME",
      "type": "mutation"
    }
  ]
}

In every instance that I've observed, the incorrect hash is always the duplicated hash of the operation preceding it. Seems that something in the generation isn't updating the hash value in some race condition before it actually gets written to the file.

Version

1.6.1

Steps to reproduce the behavior

Here are the relevant bits of our configuration:

{
  ...
  "options": {
  ...
    "operationDocumentFormat" : [
      "operationId"
    ],
    ...
  },
  "operationManifest" : {
    "generateManifestOnCodeGeneration" : true,
    "path" : "path/to/persisted_queries.json",
    "version" : "persistedQueries"
  }
}

To reproduce, I'm simply generating our code from our schema repeatedly, which also generates the manifest. We are using the apollo-ios-cli executable provided with the release packages to perform our generation.

To detect the error, I'm attempting to sync it with our operations store, which performs a uniqueness validation.

Locally, I had to do this 18 times before I observed a failure case. On our CI, it happens as often as every 3rd or 4th run. Locally I'm running this on an M1 Pro, in CI, we're running this on a much slower Intel machine.

Logs

No response

Anything else?

No response

scottasoutherland commented 1 year ago

We also had this issue. I'm fairly sure this is fixed here: https://github.com/apollographql/apollo-ios-codegen/commit/724674751239f30176ebdd0581f2f70bc6e250f2#diff-c054ba7c82703b1509c6b5be659111a24c57a86cdaf784893a6471f8ef9e8c86R10

However this introduces a new, different bug: https://github.com/apollographql/apollo-ios/issues/3269

bignimbus commented 1 year ago

Thanks all! As @scottasoutherland noted, this has been resolved but we are still tracking #3269 🙏🏻