You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cacheability metadata not bubbled up for computed fields in GraphQL responses #1414
It looks like cacheability metadata for computed fields are not bubbled up in GraphQL responses.
This is a similar issue as https://www.drupal.org/node/3423720 (but then for GraphQL responses).
I had a similar problem, where I had a computed field that had to return a Url to another entity that was referencing the entity.
In the end the solution was making sure that the graphql field producer plugins use addCacheableDependency on the FieldContext passed to the resolve method.
In my case I had to use entity_reference as the field type for the computed field (instead of uri). Because we're using the graphql_compose module for the schema I used the field_entity_reference producer plugin. This adds the referencing entity as a cacheable dependency, thus making sure the cacheability metadata is bubbled up.
Example from the SchemaExtension plugin:
public function registerResolvers(ResolverRegistryInterface $registry): void {
// ...
$registry->addFieldResolver(
$bundle->getTypeSdl(),
'your_custom_field',
$builder->compose(
// Make sure the referenced entities are added to the
// cacheability metadata.
$builder->produce('field_entity_reference')
->map('entity', $builder->fromParent())
->map('field', $builder->fromValue('your_custom_field'))
->map('types', $builder->fromValue(['node'])),
$builder->produce('seek')
->map('input', $builder->fromParent())
->map('position', $builder->fromValue(0)),
$builder->produce('entity_url')
->map('entity', $builder->fromParent()),
$builder->callback(fn (?Url $url) => $url ? $url->toString() : NULL)
)
);
// ...
}
Activity
klaasvw commentedon Oct 3, 2024
I had a similar problem, where I had a computed field that had to return a Url to another entity that was referencing the entity.
In the end the solution was making sure that the graphql field producer plugins use
addCacheableDependencyon theFieldContextpassed to the resolve method.In my case I had to use
entity_referenceas the field type for the computed field (instead ofuri). Because we're using the graphql_compose module for the schema I used thefield_entity_referenceproducer plugin. This adds the referencing entity as a cacheable dependency, thus making sure the cacheability metadata is bubbled up.Example from the SchemaExtension plugin: