You are here

public function WSConnectorGraphQL::call in Web Service Data 2.0.x

Same name and namespace in other branches
  1. 8 src/Plugin/WSConnector/WSConnectorGraphQL.php \Drupal\wsdata\Plugin\WSConnector\WSConnectorGraphQL::call()

Make the connector call.

Overrides WSConnectorSimpleHTTP::call

File

src/Plugin/WSConnector/WSConnectorGraphQL.php, line 110

Class

WSConnectorGraphQL
HTTP Connector.

Namespace

Drupal\wsdata\Plugin\WSConnector

Code

public function call($options, $method, $replacements = [], $data = NULL, array $tokens = []) {
  $langcode = $options['langcode'] ?? $this->language_manager
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId();
  $replacements['LANGUAGE'] = strtoupper($langcode);
  $contenttype = FALSE;
  if (!isset($options['headers'])) {
    $options['headers'] = [];
  }
  foreach ($options['headers'] as $key => $header) {
    if (isset($header['key_' . $key]) and strtolower($header['key_' . $key]) == strtolower('Content-Type')) {
      $contenttype = TRUE;
    }
  }
  if (!$contenttype) {
    $i = count($options['headers']);
    $options['headers'][$i] = [
      'key_' . $i => 'Content-Type',
      'value_' . $i => 'application/json',
    ];
  }
  $graphql = [
    'query' => $this
      ->applyReplacements($options['query'], $replacements, $tokens),
    'operationName' => !empty($options['operationName']) ? $options['operationName'] : NULL,
    'variables' => json_decode($options['variables']),
  ];
  unset($options['query']);
  unset($options['operationName']);
  unset($options['variables']);
  $data = json_encode($graphql);
  return parent::call($options, $method, $replacements, $data, $tokens);
}