Skip to content

Commit 1c37e3e

Browse files
committed
GQL-47: Completed implementation and documentation of the query variables feature:
- Modified the behavior of QueryBuilder::setVariable to not receive a Variable object - Added documentation and examples to the readme.md
1 parent 14e42c7 commit 1c37e3e

File tree

4 files changed

+69
-12
lines changed

4 files changed

+69
-12
lines changed

README.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,34 @@ The RawObject class being constructed is used for injecting the string into the
111111
into the RawObject constructor will be put in the query as it is without any custom formatting normally done by the
112112
query class.
113113

114+
## Query With Variables
115+
```
116+
$gql = (new Query('companies'))
117+
->setVariables(
118+
[
119+
new Variable('name', 'String', true),
120+
new Variable('limit', 'Int', false, 5)
121+
]
122+
)
123+
->setArguments(['name' => '$name', 'first' => '$limit'])
124+
->setSelectionSet(
125+
[
126+
'name',
127+
'serialNumber'
128+
]
129+
);
130+
```
131+
This query shows how variables can be used in this package to allow for dynamic requests enabled by GraphQL standards.
132+
133+
### The Variable Class
134+
The Variable class is an immutable class that represents a variable in GraphQL standards. Its constructor receives 4
135+
arguments:
136+
- name: Represents the variable name
137+
- type: Represents the variable type according to the GraphQL server schema
138+
- isRequired (Optional): Represents if the variable is required or not, it's false by default
139+
- defaultValue (Optional): Represents the default value to be assigned to the variable. The default value will only be
140+
considered if the isRequired argument is set to false.
141+
114142
# The Query Builder
115143
The QueryBuilder class can be used to construct Query objects dynamically, which can be useful in some cases. It works
116144
very similarly to the Query class, but the Query building is divided into steps.
@@ -119,7 +147,8 @@ That's how the "Query With Input Object Argument" example can be created using t
119147
QueryBuilder:
120148
```
121149
$builder = (new QueryBuilder('companies'))
122-
->setArgument('filter', new RawObject('{name_starts_with: "Face"}'))
150+
->setVariable('namePrefix', 'String', true)
151+
->setArgument('filter', new RawObject('{name_starts_with: $namePrefix}'))
123152
->selectField('name')
124153
->selectField('serialNumber');
125154
$gql = $builder->getQuery();
@@ -139,7 +168,7 @@ $client = new Client(
139168
```
140169

141170
# Running Queries
142-
171+
## Result Formatting
143172
Running query with the GraphQL client and getting the results in object structure:
144173
```
145174
$results = $client->runQuery($gql);
@@ -151,6 +180,28 @@ $results = $client->runQuery($gql, true);
151180
$results->getData()['Company'][1]['branches']['address']
152181
```
153182

183+
## Passing Variables to Queries
184+
Running queries containing variables requires passing an associative array which maps variable names (keys) to variable
185+
values (values) to the `runQuery` method. Here's an example:
186+
```
187+
$gql = (new Query('companies'))
188+
->setVariables(
189+
[
190+
new Variable('name', 'String', true),
191+
new Variable('limit', 'Int', false, 5)
192+
]
193+
)
194+
->setArguments(['name' => '$name', 'first' => '$limit'])
195+
->setSelectionSet(
196+
[
197+
'name',
198+
'serialNumber'
199+
]
200+
);
201+
$variablesArray = ['name' => 'Tech Co.', 'first' => 5];
202+
$results = $client->runQuery($gql, true, $variablesArray);
203+
```
204+
154205
# Mutations
155206
Mutations follow the same rules of queries in GraphQL, they select fields on returned objects, receive arguments, and
156207
can have sub-fields.

src/QueryBuilder/AbstractQueryBuilder.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,16 @@ protected function setArgument(string $argumentName, $argumentValue)
100100
}
101101

102102
/**
103-
* @param Variable $variable
103+
* @param string $name
104+
* @param string $type
105+
* @param bool $isRequired
106+
* @param null $defaultValue
104107
*
105108
* @return $this
106109
*/
107-
protected function setVariable(Variable $variable)
110+
protected function setVariable(string $name, string $type, bool $isRequired = false, $defaultValue = null)
108111
{
109-
$this->variables[] = $variable;
112+
$this->variables[] = new Variable($name, $type, $isRequired, $defaultValue);
110113

111114
return $this;
112115
}

src/QueryBuilder/QueryBuilder.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,15 @@ public function setArgument(string $argumentName, $argumentValue)
4040
/**
4141
* Changing method visibility to public
4242
*
43-
* @param Variable $variable
43+
* @param string $name
44+
* @param string $type
45+
* @param bool $isRequired
46+
* @param null $defaultValue
4447
*
4548
* @return AbstractQueryBuilder|QueryBuilder
4649
*/
47-
public function setVariable(Variable $variable)
50+
public function setVariable(string $name, string $type, bool $isRequired = false, $defaultValue = null)
4851
{
49-
return parent::setVariable($variable);
52+
return parent::setVariable($name, $type, $isRequired, $defaultValue);
5053
}
5154
}

tests/QueryBuilderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public function testEmptySelectionSet()
6868
public function testAddVariables()
6969
{
7070
$this->queryBuilder
71-
->setVariable(new Variable('var', 'String'))
72-
->setVariable(new Variable('intVar', 'Int', false, 4))
71+
->setVariable('var', 'String')
72+
->setVariable('intVar', 'Int', false, 4)
7373
->selectField('fieldOne');
7474
$this->assertEquals(
7575
'query($var: String $intVar: Int=4) {
@@ -87,11 +87,11 @@ public function testAddVariables()
8787
public function testAddVariablesToSecondLevelQueryDoesNothing()
8888
{
8989
$this->queryBuilder
90-
->setVariable(new Variable('var', 'String'))
90+
->setVariable('var', 'String')
9191
->selectField('fieldOne')
9292
->selectField(
9393
(new QueryBuilder('Nested'))
94-
->setVariable(new Variable('var', 'String'))
94+
->setVariable('var', 'String')
9595
->selectField('fieldTwo')
9696
);
9797
$this->assertEquals(

0 commit comments

Comments
 (0)