Skip to content

Caching GraphQL POST requests #713

@rafatwork

Description

@rafatwork

#487 allowed page caching GraphQL requests.

As far as I can see this only applies to GET requests since Drupal's \Drupal\Core\PageCache\DefaultRequestPolicy uses the \Drupal\Core\PageCache\RequestPolicy\CommandLineOrUnsafeMethod() rule which eventually decides in \Symfony\Component\HttpFoundation\Request::isMethodCacheable() that only GET and HEAD requests are cacheable.

I would like to cache GraphQL POST requests in one of my projects. I was able to achieve this by swapping out \Drupal\Core\PageCache\DefaultRequestPolicy with a custom implementation that extends \Drupal\Core\PageCache\RequestPolicy\CommandLineOrUnsafeMethod(), explicitely allowing caching GraphQL POST requests, as such:

  /**
   * {@inheritdoc}
   */
  public function check(Request $request) {
    if (!$this->isCli() && $request->getMethod() === 'POST' && $request->attributes->has('_graphql')) {
      return static::ALLOW;
    }

    return parent::check($request);
  }

Has anyone already tried doing the same? Perhaps in a different way? Any potential issues I should be wary of?

Is this a feature that might be worth looking into adding to the GraphQL module, especially since POST is more or less the default behaviour in GraphQL?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions