Skip to content

InlineFragment: read selectionset from QueryBuilderInterface #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/FieldTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,9 @@ protected function constructSelectionSet(): string

return $attributesString;
}
}

public function getSelectionSet()
{
return $this->selectionSet;
}
}
17 changes: 15 additions & 2 deletions src/InlineFragment.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace GraphQL;

use GraphQL\QueryBuilder\QueryBuilderInterface;

/**
* Class InlineFragment
*
Expand All @@ -23,21 +25,32 @@ class InlineFragment extends NestableObject
*/
protected $typeName;

/**
* @var QueryBuilderInterface|null
*/
protected $queryBuilder;

/**
* InlineFragment constructor.
*
* @param string $typeName
* @param QueryBuilderInterface|null $queryBuilder
*/
public function __construct(string $typeName)
public function __construct(string $typeName, ?QueryBuilderInterface $queryBuilder = null)
{
$this->typeName = $typeName;
$this->queryBuilder = $queryBuilder;
}

/**
*
*/
public function __toString()
{
if ($this->queryBuilder !== null) {
$this->setSelectionSet($this->queryBuilder->getQuery()->getSelectionSet());
}

return sprintf(static::FORMAT, $this->typeName, $this->constructSelectionSet());
}

Expand All @@ -50,4 +63,4 @@ protected function setAsNested()
{
// TODO: Remove this method, it's purely tech debt
}
}
}
28 changes: 27 additions & 1 deletion tests/InlineFragmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GraphQL\InlineFragment;
use GraphQL\Query;
use GraphQL\QueryBuilder\QueryBuilder;
use PHPUnit\Framework\TestCase;

/**
Expand Down Expand Up @@ -37,6 +38,7 @@ public function testConvertToString()
(string) $fragment
);
}

/**
* @covers \GraphQL\InlineFragment::__construct
* @covers \GraphQL\InlineFragment::setSelectionSet
Expand Down Expand Up @@ -84,4 +86,28 @@ public function testConvertNestedFragmentToString()
(string) $fragment
);
}
}

/**
* @covers \GraphQL\InlineFragment::__construct
* @covers \GraphQL\InlineFragment::setSelectionSet
* @covers \GraphQL\InlineFragment::getSelectionSet
* @covers \GraphQL\InlineFragment::constructSelectionSet
* @covers \GraphQL\InlineFragment::__toString
*/
public function testConvertQueryBuilderToString()
{
$queryBuilder = new QueryBuilder();

$fragment = new InlineFragment('Test', $queryBuilder);
$queryBuilder->selectField('field1');
$queryBuilder->selectField('field2');

$this->assertEquals(
'... on Test {
field1
field2
}',
(string) $fragment
);
}
}