You are here

protected function QueryRouteEnhancer::extractMultipart in GraphQL 8.4

Same name and namespace in other branches
  1. 8.3 src/Routing/QueryRouteEnhancer.php \Drupal\graphql\Routing\QueryRouteEnhancer::extractMultipart()

Handles file uploads from multipart/form-data requests.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object.

array $values: The request body values.

Return value

array The query parameters with added file uploads.

1 call to QueryRouteEnhancer::extractMultipart()
QueryRouteEnhancer::extractBody in src/Routing/QueryRouteEnhancer.php
Extracts the body parameters from a request.

File

src/Routing/QueryRouteEnhancer.php, line 98

Class

QueryRouteEnhancer
Adds GraphQL operation information to the Symfony route being resolved.

Namespace

Drupal\graphql\Routing

Code

protected function extractMultipart(Request $request, array $values) {

  // The request body parameters might contain file upload mutations. We treat
  // them according to the graphql multipart request specification.
  //
  // @see https://github.com/jaydenseric/graphql-multipart-request-spec#server
  if ($body = JsonHelper::decodeParams($request->request
    ->all())) {

    // Flatten the operations array if it exists.
    $operations = isset($body['operations']) && is_array($body['operations']) ? $body['operations'] : [];
    $values = array_merge($values, $body, $operations);
  }

  // According to the graphql multipart request specification, uploaded files
  // are referenced to variable placeholders in a map. Here, we resolve this
  // map by assigning the uploaded files to the corresponding variables.
  if (!empty($values['map']) && is_array($values['map']) && ($files = $request->files
    ->all())) {
    foreach ($files as $key => $file) {
      if (!isset($values['map'][$key])) {
        continue;
      }
      $paths = (array) $values['map'][$key];
      foreach ($paths as $path) {
        $path = explode('.', $path);
        if (NestedArray::keyExists($values, $path)) {
          NestedArray::setValue($values, $path, $file);
        }
      }
    }
  }
  return $values;
}