diff --git a/composer.json b/composer.json index d372cec..53a67e3 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "require": { "php": "^7.1 || ^8.0", "ext-json": "*", - "psr/http-message": "^1.0", + "psr/http-message": "^1.0 || ^2.0", "psr/http-client": "^1.0", "guzzlehttp/guzzle": "^6.3|^7.0.1" }, diff --git a/examples/query_example.php b/examples/query_example.php index 7abc472..c038fa7 100644 --- a/examples/query_example.php +++ b/examples/query_example.php @@ -2,6 +2,7 @@ require_once __DIR__ . '/../vendor/autoload.php'; +use GraphQL\ArgumentObject; use GraphQL\Client; use GraphQL\Exception\QueryError; use GraphQL\Query; @@ -15,7 +16,13 @@ // Create the GraphQL query $gql = (new Query('pokemon')) - ->setArguments(['name' => 'Pikachu']) + ->setArguments([ + 'name' => 'Pikachu', + 'age' => new ArgumentObject([ + 'min' => 7, + 'max' => 20 + ]), + ]) ->setSelectionSet( [ 'id', diff --git a/src/ArgumentObject.php b/src/ArgumentObject.php new file mode 100644 index 0000000..7202f6c --- /dev/null +++ b/src/ArgumentObject.php @@ -0,0 +1,39 @@ +input = $input; + } + + + public function __toString(): string + { + $result = []; + foreach ($this->input as $key => $value) { + if (is_scalar($value) || $value === null) { + $value = StringLiteralFormatter::formatValueForRHS($value); + } elseif (is_array($value)) { + $value = StringLiteralFormatter::formatArrayForGQLQuery($value); + } + + $result[] = "${key}: ${value}"; + } + + return '{' . implode(', ', $result) . '}'; + } +}