Skip to content

Commit

Permalink
fix printing "mutation" keywork when no operation name
Browse files Browse the repository at this point in the history
support multi line arguments
  • Loading branch information
fnash committed Dec 4, 2020
1 parent e0b4498 commit e45870e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
10 changes: 6 additions & 4 deletions GraphQL/QueryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private static function printQuery($operationName, $variables): string
if (\count($variables)) {
$operationName = static::$operationNamePlaceholder;
} else {
return '';
return static::class === Mutation::class ? sprintf('%s', static::KEYWORD) : '';
}
}

Expand Down Expand Up @@ -158,7 +158,9 @@ private static function printArgs(array $value): string
$args = [];
foreach ($value as $argName => $argValue) {
if (\is_string($argValue) && '$' !== $argValue[0]) {
$argValue = sprintf('"%s"', $argValue);
if (preg_match_all('/"""/', $argValue) !== 2) { // if not a multi line argument value
$argValue = sprintf('"%s"', $argValue);
}
}

if (\is_bool($argValue) || \is_float($argValue)) {
Expand Down Expand Up @@ -187,7 +189,7 @@ public function fields(array $fields = [])
}
}

if ($field instanceof self) {
if ($field instanceof Query) {
$field->isRootQuery = false;
$this->fields[$fieldAlias] = $field;
}
Expand Down Expand Up @@ -246,7 +248,7 @@ private static function printFields(array $value, array $skipIf = [], array $inc
}
}

if ($field instanceof self) {
if ($field instanceof Query) {
$field->isRootQuery = false;

if (array_key_exists($fieldAlias, $skipIf)) {
Expand Down
57 changes: 57 additions & 0 deletions Tests/MutationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Fnash\GraphQL;

use Fnash\GraphQL\Mutation;
use Fnash\GraphQL\Query;
use PHPUnit\Framework\TestCase;

class MutationTest extends TestCase
Expand Down Expand Up @@ -34,4 +35,60 @@ public function testMutation()
';
$this->assertEquals($expected, (string) $mutation);
}


/**
* Tests operation name generation and printing.
*/
public function testOperationName()
{
// simple mutation without variables nor operation name
$mutation = Mutation::create('createReview')
->arguments([
'episode' => 123,
'review' => 'great review as string',
])
->fields([
'stars',
'commentary',
]);

$expected =
'mutation {
createReview(episode: 123, review: "great review as string") {
commentary
stars
}
}
';

$this->assertEquals($expected, (string) $mutation);


// mutation with variables but no operation name
$mutation = Mutation::create('createReview')
->variables([
'$ep' => 'Episode!',
'$review' => 'ReviewInput!',
])
->arguments([
'episode' => '$ep',
'review' => '$review',
])
->fields([
'stars',
'commentary',
]);

$expected =
'mutation mutation_4db26a2e56d1146f7c7715edf0d1d5d55eeb261c($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
commentary
stars
}
}
';

$this->assertEquals($expected, (string) $mutation);
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
}
],
"require": {
"webonyx/graphql-php": "^0.11|^0.12|^0.13"
"php": ">7",
"webonyx/graphql-php": "^0.11|^0.12|^0.13|^14.0"
},
"require-dev": {
"phpunit/phpunit": "^6.4"
"phpunit/phpunit": "^6.4",
"symfony/var-dumper": "*"
}
}

0 comments on commit e45870e

Please sign in to comment.