drupal-graphql / graphql

GraphQL integration for Drupal 9/10
288 stars 202 forks source link

Caching does not take operation name into account #1365

Closed dulnan closed 9 months ago

dulnan commented 10 months ago

The cachePrefix() method in Executor.php generates a hash based on the query, variables and extensions:

$hash = hash('sha256', serialize([
  'query' => DocumentSerializer::serializeDocument($this->document),
  'variables' => $variables,
  'extensions' => $extensions,
]));

This only works when the document contains a single operation. If the document contains let's say two queries, it will cache the first result and subsequently resolve the cached result for any of the operations in the document. To fix it, the name of the operation should be taken into account.

Example:

query one {
  currentUser {
    name
  }
}

query two {
  nodeById(id: 12) {
    title
  }
}

Executing the query "one" first and then "two" will return the result from query "one".