@@ -221,6 +221,26 @@ $results = $client->runQuery($mutation);
221221```
222222Mutations 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
225245GraphQL Pokemon is a very cool public GraphQL API available to retrieve Pokemon data. The API is available publicly on
226246the 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
230250API 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 );
295316try {
296- $results = $client->runQuery($gql, true);
317+ $name = readline('Enter pokemon name: ');
318+ $results = $client->runQuery($gql, true, ['name' => $name]);
297319}
298320catch (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 );
329352try {
330- $results = $client->runQuery($builder, true);
353+ $name = readline('Enter pokemon name: ');
354+ $results = $client->runQuery($builder, true, ['name' => $name]);
331355}
332356catch (QueryError $exception) {
333357 print_r($exception->getErrorDetails());
0 commit comments