Description

When the GraphQL module is not enabled, the Social 13 code (notably in
social_post hooks) crashes because it references GraphQL DataProducers unconditionally.
Classes like Drupal\social_post\Plugin\GraphQL\DataProducer\UserPostsCreated are invoked without first checking whether
the graphql module is enabled.

Since graphql is not declared as a dependency in the module’s .info.yml, Drupal allows Social 13 to run even when GraphQL is missing — causing fatal errors.

Steps to Reproduce

  1. Install Open Social 13.x (ensure social_post is enabled).
  2. Make sure the GraphQL module is disabled.
  3. Trigger a post-related action (create, update, delete).
  4. Observe a fatal error, e.g.:
    Error: Class "Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase" not found in include() (line 31 of profiles/contrib/open_social/modules/social_features/social_post/src/Plugin/GraphQL/DataProducer/UserPostsCreated.php).
    

Expected Behavior

The system should not crash when GraphQL is disabled. Hook logic that relies on GraphQL should gracefully skip if the module is missing.

Actual Behavior

Hooks such as PostsHooks::postCreateDelete() and PostsHooks::postUpdate() directly reference GraphQL classes without verifying module availability, causing hard crashes when graphql is disabled.

Scope

  • PostsHooks
  • CommentsHooks
  • GroupHooks

Proposed Fix

Add a moduleExists('graphql') guard before any GraphQL-specific logic (cache tag invalidations based on GraphQL DataProducers, etc.).

Example implementation (PostsHooks)

<?php

namespace Drupal\social_post\Hooks;

use Drupal\Core\Cache\CacheTagsInvalidatorInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\hux\Attribute\Hook;
use Drupal\social_post\Entity\PostInterface;
use Drupal\social_post\Plugin\GraphQL\DataProducer\UserPostsCreated;

/**
 * Provides hook related to posts.
 */
final class PostsHooks {

  /**
   * Construct for the hux.
   */
  public function __construct(
    protected CacheTagsInvalidatorInterface $cacheInvalidator,
    protected ModuleHandlerInterface $moduleHandler
  ) {}

  /**
   * Implements hook_ENTITY_TYPE_delete() and hook_ENTITY_TYPE_create().
   */
  #[Hook('post_delete')]
  #[Hook('post_insert')]
  public function postCreateDelete(EntityInterface $entity): void {
    if (!$this->moduleHandler->moduleExists('graphql')) {
      return;
    }
    if (!$entity instanceof PostInterface) {
      return;
    }
    // Invalidate cache.
    $this->cacheInvalidator->invalidateTags([UserPostsCreated::CID_BASE . $entity->getOwnerId()]);
  }

  /**
   * Implements hook_ENTITY_TYPE_update().
   */
  #[Hook('post_update')]
  public function postUpdate(EntityInterface $entity): void {
    if (!$this->moduleHandler->moduleExists('graphql')) {
      return;
    }
    // Using $entity->original, $entity->getOriginal() does not exists here.
    if (
      !$entity instanceof PostInterface ||
      empty($entity->original) ||
      !$entity->original instanceof PostInterface
    ) {
      return;
    }
    // Invalidate cache on author change for both.
    if ($entity->original->getOwnerId() !== $entity->getOwnerId()) {
      $this->cacheInvalidator->invalidateTags([UserPostsCreated::CID_BASE . $entity->getOwnerId()]);
      $this->cacheInvalidator->invalidateTags([UserPostsCreated::CID_BASE . $entity->original->getOwnerId()]);
    }
  }

}

Alternative Solution

Declare graphql as a hard dependency in the module’s .info.yml (makes GraphQL mandatory):

dependencies:
  - graphql:graphql

If GraphQL should remain optional, prefer the conditional check above.

CommentFileSizeAuthor
#8 3551785-8.patch33.12 KBrobertragas

Issue fork social-3551785

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

christian.wiedemann created an issue. See original summary.

christian.wiedemann’s picture

Issue summary: View changes

christian.wiedemann’s picture

Status: Active » Needs review

hpcalaf changed the visibility of the branch 3551785- to hidden.

duwid changed the visibility of the branch 3551785- to active.

socialnicheguru’s picture

Status: Needs review » Needs work

the MR does not work

robertragas’s picture

StatusFileSize
new33.12 KB

Thanks for reporting the issue. We also discussed this internally, and instead of adding a module check in there, we are removing this custom cache implementation and add the cache tags to the data producers removing the hard dependency on the graphql.

Off course I will still give the credits for work in this issue to everyone.

robertragas’s picture

Status: Needs work » Needs review
robertragas’s picture

Note that we already added this to the 13.0.0 release. Feel free to re-open the issue if there is anything you are running into.

robertragas’s picture

Status: Needs review » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.