@@ -111,6 +111,34 @@ The RawObject class being constructed is used for injecting the string into the
111111into the RawObject constructor will be put in the query as it is without any custom formatting normally done by the
112112query 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
115143The QueryBuilder class can be used to construct Query objects dynamically, which can be useful in some cases. It works
116144very 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
119147QueryBuilder:
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
143172Running 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
155206Mutations follow the same rules of queries in GraphQL, they select fields on returned objects, receive arguments, and
156207can have sub-fields.
0 commit comments