diff --git a/src/GraphQL/Client.php b/src/GraphQL/Client.php index d51a9c6..8816b5d 100644 --- a/src/GraphQL/Client.php +++ b/src/GraphQL/Client.php @@ -3,6 +3,7 @@ namespace Drupal\migrate_source_graphql\GraphQL; use GraphQL\Client as GraphQLClient; +use GraphQL\Exception\QueryError; use GraphQL\Query; use GraphQL\RawObject; use GraphQL\QueryBuilder\QueryBuilderInterface; @@ -67,13 +68,15 @@ class Client { ->setSelectionSet($selectionSet); if ($arguments !== NULL) { - $argumentsKey = array_key_first($arguments); - $argumentsToString = json_encode($arguments[$argumentsKey]); - $argumentsToString = preg_replace("/['\"]/", '', $argumentsToString); - - $arguments = [ - $argumentsKey => new RawObject($argumentsToString), - ]; + // Temporally filled with RawObject (filters from migration.yml to string) + $argumentsTemp = []; + // Iterate over all arguments + foreach ($arguments as $argumentsKey => $argument) { + $argumentToString = json_encode($argument); + $argumentToString = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:', $argumentToString); + $argumentsTemp[$argumentsKey] = new RawObject($argumentToString); + } + $arguments = $argumentsTemp; if ($filters !== NULL) { $arguments['filters'] = $filters; diff --git a/src/Plugin/migrate/source/GraphQL.php b/src/Plugin/migrate/source/GraphQL.php index 3139216..86ec67c 100644 --- a/src/Plugin/migrate/source/GraphQL.php +++ b/src/Plugin/migrate/source/GraphQL.php @@ -7,6 +7,7 @@ use Drupal\migrate\Plugin\migrate\source\SourcePluginBase; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\Component\Utility\NestedArray; use Drupal\migrate_source_graphql\GraphQL\Client; +use GraphQL\Exception\QueryError; /** * Class GraphQL migrate source. @@ -106,13 +107,17 @@ class GraphQL extends SourcePluginBase implements ConfigurableInterface { $query = $this->configuration['query']; $queryName = array_key_first($query); $query = $this->buildQuery($queryName, $query[$queryName]); - $results = $this->client->runQuery($query); - $results = $results->getData(); - $property = $this->configuration['data_key']; - $results = $results->$queryName->$property ?? $results->$queryName ?? []; - foreach ($results as $result) { - yield json_decode(json_encode($result), TRUE); - ; + try { + $results = $this->client->runQuery($query); + $results = $results->getData(); + $property = $this->configuration['data_key']; + $results = $results->$queryName->$property ?? $results->$queryName ?? []; + foreach ($results as $result) { + yield json_decode(json_encode($result), TRUE); + ; + } + } catch (QueryError $exception) { + \Drupal::messenger()->addError($exception->getMessage()); } }