@@ -111,6 +111,34 @@ The RawObject class being constructed is used for injecting the string into the
111
111
into the RawObject constructor will be put in the query as it is without any custom formatting normally done by the
112
112
query class.
113
113
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
+
114
142
# The Query Builder
115
143
The QueryBuilder class can be used to construct Query objects dynamically, which can be useful in some cases. It works
116
144
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
119
147
QueryBuilder:
120
148
```
121
149
$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}'))
123
152
->selectField('name')
124
153
->selectField('serialNumber');
125
154
$gql = $builder->getQuery();
@@ -139,7 +168,7 @@ $client = new Client(
139
168
```
140
169
141
170
# Running Queries
142
-
171
+ ## Result Formatting
143
172
Running query with the GraphQL client and getting the results in object structure:
144
173
```
145
174
$results = $client->runQuery($gql);
@@ -151,6 +180,28 @@ $results = $client->runQuery($gql, true);
151
180
$results->getData()['Company'][1]['branches']['address']
152
181
```
153
182
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
+
154
205
# Mutations
155
206
Mutations follow the same rules of queries in GraphQL, they select fields on returned objects, receive arguments, and
156
207
can have sub-fields.
@@ -170,6 +221,26 @@ $results = $client->runQuery($mutation);
170
221
```
171
222
Mutations can be run by the client the same way queries are run.
172
223
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
+ ```
173
244
# Live API Example
174
245
GraphQL Pokemon is a very cool public GraphQL API available to retrieve Pokemon data. The API is available publicly on
175
246
the web, we'll use it to demo the capabilities of this client.
@@ -178,10 +249,10 @@ Github Repo link: https://github.com/lucasbento/graphql-pokemon
178
249
179
250
API link: https://graphql-pokemon.now.sh/
180
251
181
- This query retrieves Pikachu 's evolutions and their attacks:
252
+ This query retrieves any pokemon 's evolutions and their attacks:
182
253
```
183
- {
184
- pokemon(name: "Pikachu" ) {
254
+ query($name: String!) {
255
+ pokemon(name: $name ) {
185
256
id
186
257
number
187
258
name
@@ -212,7 +283,8 @@ $client = new Client(
212
283
'https://graphql-pokemon.now.sh/'
213
284
);
214
285
$gql = (new Query('pokemon'))
215
- ->setArguments(['name' => 'Pikachu'])
286
+ ->setVariables([new Variable('name', 'String', true)])
287
+ ->setArguments(['name' => '$name'])
216
288
->setSelectionSet(
217
289
[
218
290
'id',
@@ -242,7 +314,8 @@ $gql = (new Query('pokemon'))
242
314
]
243
315
);
244
316
try {
245
- $results = $client->runQuery($gql, true);
317
+ $name = readline('Enter pokemon name: ');
318
+ $results = $client->runQuery($gql, true, ['name' => $name]);
246
319
}
247
320
catch (QueryError $exception) {
248
321
print_r($exception->getErrorDetails());
@@ -256,7 +329,8 @@ $client = new Client(
256
329
'https://graphql-pokemon.now.sh/'
257
330
);
258
331
$builder = (new QueryBuilder('pokemon'))
259
- ->setArgument('name', 'Pikachu')
332
+ ->setVariable('name', 'String', true)
333
+ ->setArgument('name', '$name')
260
334
->selectField('id')
261
335
->selectField('number')
262
336
->selectField('name')
@@ -276,7 +350,8 @@ $builder = (new QueryBuilder('pokemon'))
276
350
)
277
351
);
278
352
try {
279
- $results = $client->runQuery($builder, true);
353
+ $name = readline('Enter pokemon name: ');
354
+ $results = $client->runQuery($builder, true, ['name' => $name]);
280
355
}
281
356
catch (QueryError $exception) {
282
357
print_r($exception->getErrorDetails());
0 commit comments