diff --git a/src/FieldTrait.php b/src/FieldTrait.php index 08ba21b..30cafbe 100644 --- a/src/FieldTrait.php +++ b/src/FieldTrait.php @@ -22,14 +22,35 @@ trait FieldTrait public function setSelectionSet(array $selectionSet) { $nonStringsFields = array_filter($selectionSet, function($element) { - return !is_string($element) && !$element instanceof Query && !$element instanceof InlineFragment; + return !is_string($element) && !is_array($element) && !$element instanceof Query && !$element instanceof InlineFragment; }); + if (!empty($nonStringsFields)) { throw new InvalidSelectionException( - 'One or more of the selection fields provided is not of type string or Query' + 'One or more of the selection fields provided is not of type string, array or Query' ); } + // SelectionSet with arguments + foreach ($selectionSet as $element => $arguments) { + + if (is_array($arguments) && count($arguments)) { + + $params = ''; + foreach ($arguments as $param => $val) { + if (is_string($val)) { + $params .= " $param : \"$val\" "; + } + } + + if (!empty($params)) { + $selectedField = ' ' . $element . " ($params)"; + $selectionSet[] = $selectedField; + unset($selectionSet[$element]); + } + } + } + $this->selectionSet = $selectionSet; return $this; @@ -40,7 +61,7 @@ public function setSelectionSet(array $selectionSet) */ protected function constructSelectionSet(): string { - $attributesString = " {\n"; + $attributesString = " {" . PHP_EOL; $first = true; foreach ($this->selectionSet as $attribute) { @@ -48,7 +69,7 @@ protected function constructSelectionSet(): string if ($first) { $first = false; } else { - $attributesString .= "\n"; + $attributesString .= PHP_EOL; } // If query is included in attributes set as a nested query @@ -59,7 +80,7 @@ protected function constructSelectionSet(): string // Append attribute to returned attributes list $attributesString .= $attribute; } - $attributesString .= "\n}"; + $attributesString .= PHP_EOL . "}"; return $attributesString; } diff --git a/src/Query.php b/src/Query.php index 6f8cab8..a6a0b85 100644 --- a/src/Query.php +++ b/src/Query.php @@ -211,7 +211,7 @@ public function __toString() { $queryFormat = static::QUERY_FORMAT; if (!$this->isNested && $this->fieldName !== static::OPERATION_TYPE) { - $queryFormat = $this->generateSignature() . " {\n" . static::QUERY_FORMAT . "\n}"; + $queryFormat = $this->generateSignature() . " {" . PHP_EOL . static::QUERY_FORMAT . PHP_EOL. "}"; } $argumentsString = $this->constructArguments(); $selectionSetString = $this->constructSelectionSet(); diff --git a/src/QueryBuilder/AbstractQueryBuilder.php b/src/QueryBuilder/AbstractQueryBuilder.php index 1b88568..7fb436a 100644 --- a/src/QueryBuilder/AbstractQueryBuilder.php +++ b/src/QueryBuilder/AbstractQueryBuilder.php @@ -74,9 +74,11 @@ public function getQuery(): Query /** * @param string|QueryBuilder|Query $selectedField * + * @param string|null $alias + * @param array $arguments * @return $this */ - protected function selectField($selectedField) + protected function selectField($selectedField, string $alias = null, array $arguments = []) { if ( is_string($selectedField) @@ -84,6 +86,21 @@ protected function selectField($selectedField) || $selectedField instanceof Query || $selectedField instanceof InlineFragment ) { + + // Set an alias for selectedField + $selectedField = ($alias) ? $alias .':'. $selectedField : $selectedField; + + // Add arguments for selectedField + if (is_array($arguments) && count($arguments)) { + + $params = ''; + foreach ($arguments as $k => $v) { + $params.= " $k : \"$v\" "; + } + $selectedField .= " ($params)"; + } + + $this->selectionSet[] = $selectedField; } diff --git a/src/QueryBuilder/QueryBuilder.php b/src/QueryBuilder/QueryBuilder.php index 70735da..409c698 100644 --- a/src/QueryBuilder/QueryBuilder.php +++ b/src/QueryBuilder/QueryBuilder.php @@ -16,11 +16,13 @@ class QueryBuilder extends AbstractQueryBuilder * * @param Query|QueryBuilder|string $selectedField * + * @param string|null $alias + * @param array $arguments * @return AbstractQueryBuilder|QueryBuilder */ - public function selectField($selectedField) + public function selectField($selectedField, string $alias = null, array $arguments = []) { - return parent::selectField($selectedField); + return parent::selectField($selectedField, $alias, $arguments); } /** diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 4964775..f186c42 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -61,6 +61,75 @@ public function testEmptySelectionSet() $this->queryBuilder->getQuery(); } + + /** + * @covers \GraphQL\QueryBuilder\QueryBuilder::__construct + * @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::__construct + */ + public function testSelectFieldAlias() + { + $builder = new QueryBuilder('Object'); + $builder->selectField('field_one', 'fieldAlias'); + $this->assertEquals( + 'query { +Object { +fieldAlias:field_one +} +}', + (string) $builder->getQuery() + ); + } + + /** + * @covers \GraphQL\QueryBuilder\QueryBuilder::__construct + * @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::__construct + */ + public function testSelectFieldAliasWithArguments() + { + $builder = new QueryBuilder('Object'); + $builder->selectField('field_one', 'fieldAlias', ['param1' => 'val1', 'param2' => 'val2' ]); + $this->assertEquals( + 'query { +Object { +fieldAlias:field_one ( param1 : "val1" param2 : "val2" ) +} +}', + (string) $builder->getQuery() + ); + + } + + /** + * @covers \GraphQL\QueryBuilder\QueryBuilder::__construct + * @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::__construct + */ + public function testSelectionSetAliasWithArguments() + { + $builder = new QueryBuilder('Object'); + + $builder->selectField( + (new Query('data'))->setArguments(['some_field' => 'params']) + ->setSelectionSet( + [ + 'aliasId:first_field' => ['param1' => 'val1', 'param2' => 'val2' ], + 'second_field' => ['param1' => 'val1', 'param2' => 'val2' ] + ]) + ); + + $this->assertEquals( + 'query { +Object { +data(some_field: "params") { + aliasId:first_field ( param1 : "val1" param2 : "val2" ) + second_field ( param1 : "val1" param2 : "val2" ) +} +} +}', + (string) $builder->getQuery() + ); + + + } /** * @covers \GraphQL\QueryBuilder\QueryBuilder::setVariable * @covers \GraphQL\QueryBuilder\AbstractQueryBuilder::setVariable