Skip to content

Commit 8e9e532

Browse files
committed
v1.2: Version 1.2 finalized and ready for release
- Modified examples from Pokemon live API to contain variables and make queries generic - Added an exmaple showing how to use variables with mutations and input objects - Finalized the changelog
1 parent ac61b3b commit 8e9e532

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

Changelog.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22

33
The change log describes what is "Added", "Removed", "Changed" or "Fixed" between each release.
44

5-
## In Progress
5+
## 2019-07-24
66

7-
- Add support for variables attachment to query
8-
- Add support for variables passing query
9-
- Support for input objects variables
7+
### Added
8+
9+
- Variables support for queries and mutations
10+
- Operation name during in queries
11+
12+
### Fixed
13+
14+
- Issue in mutation string generation when no selection set is provided
1015

1116
## 1.1: 2019-04-26
1217

README.md

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,26 @@ $results = $client->runQuery($mutation);
221221
```
222222
Mutations can be run by the client the same way queries are run.
223223

224+
## Mutations With Variables Example
225+
Mutations can utilize the variables in the same way Queries can. Here's an example on how to use the variables to pass
226+
input objects to the GraphQL server dynamically:
227+
```
228+
$mutation = (new Mutation('createCompany'))
229+
->setVariables([new Variable('company', 'CompanyInputObject', true)])
230+
->setArguments(['companyObject' => '$company']);
231+
232+
$variables = ['company' => ['name' => 'Tech Company', 'type' => 'Testing', 'size' => 'Medium']];
233+
$client->runQuery(
234+
$mutation, true, $variables
235+
);
236+
```
237+
These are the resulting mutation and the variables passed with it:
238+
```
239+
mutation($company: CompanyInputObject!) {
240+
createCompany(companyObject: $company)
241+
}
242+
{"company":{"name":"Tech Company","type":"Testing","size":"Medium"}}
243+
```
224244
# Live API Example
225245
GraphQL Pokemon is a very cool public GraphQL API available to retrieve Pokemon data. The API is available publicly on
226246
the web, we'll use it to demo the capabilities of this client.
@@ -229,10 +249,10 @@ Github Repo link: https://github.com/lucasbento/graphql-pokemon
229249

230250
API link: https://graphql-pokemon.now.sh/
231251

232-
This query retrieves Pikachu's evolutions and their attacks:
252+
This query retrieves any pokemon's evolutions and their attacks:
233253
```
234-
{
235-
pokemon(name: "Pikachu") {
254+
query($name: String!) {
255+
pokemon(name: $name) {
236256
id
237257
number
238258
name
@@ -263,7 +283,8 @@ $client = new Client(
263283
'https://graphql-pokemon.now.sh/'
264284
);
265285
$gql = (new Query('pokemon'))
266-
->setArguments(['name' => 'Pikachu'])
286+
->setVariables([new Variable('name', 'String', true)])
287+
->setArguments(['name' => '$name'])
267288
->setSelectionSet(
268289
[
269290
'id',
@@ -293,7 +314,8 @@ $gql = (new Query('pokemon'))
293314
]
294315
);
295316
try {
296-
$results = $client->runQuery($gql, true);
317+
$name = readline('Enter pokemon name: ');
318+
$results = $client->runQuery($gql, true, ['name' => $name]);
297319
}
298320
catch (QueryError $exception) {
299321
print_r($exception->getErrorDetails());
@@ -307,7 +329,8 @@ $client = new Client(
307329
'https://graphql-pokemon.now.sh/'
308330
);
309331
$builder = (new QueryBuilder('pokemon'))
310-
->setArgument('name', 'Pikachu')
332+
->setVariable('name', 'String', true)
333+
->setArgument('name', '$name')
311334
->selectField('id')
312335
->selectField('number')
313336
->selectField('name')
@@ -327,7 +350,8 @@ $builder = (new QueryBuilder('pokemon'))
327350
)
328351
);
329352
try {
330-
$results = $client->runQuery($builder, true);
353+
$name = readline('Enter pokemon name: ');
354+
$results = $client->runQuery($builder, true, ['name' => $name]);
331355
}
332356
catch (QueryError $exception) {
333357
print_r($exception->getErrorDetails());

0 commit comments

Comments
 (0)