22
33namespace GraphQL ;
44
5+ use Exception ;
56use GraphQL \Exception \ArgumentException ;
67use GraphQL \Exception \InvalidSelectionException ;
8+ use GraphQL \Exception \InvalidVariableException ;
79use GraphQL \Util \StringLiteralFormatter ;
810
911/**
@@ -27,12 +29,26 @@ class Query
2729 */
2830 protected const OPERATION_TYPE = 'query ' ;
2931
32+ /**
33+ * Stores the name of the operation to be run on the server
34+ *
35+ * @var string
36+ */
37+ private $ operationName ;
38+
3039 /**
3140 * Stores the object being queried for
3241 *
3342 * @var string
3443 */
35- private $ object ;
44+ private $ fieldName ;
45+
46+ /**
47+ * Stores the list of variables to be used in the query
48+ *
49+ * @var array|Variable[]
50+ */
51+ private $ variables ;
3652
3753 /**
3854 * Stores the list of arguments used when querying data
@@ -58,14 +74,49 @@ class Query
5874 /**
5975 * GQLQueryBuilder constructor.
6076 *
61- * @param string $objectName
77+ * @param string $fieldName
78+ */
79+ public function __construct (string $ fieldName )
80+ {
81+ $ this ->fieldName = $ fieldName ;
82+ $ this ->operationName = '' ;
83+ $ this ->variables = [];
84+ $ this ->arguments = [];
85+ $ this ->selectionSet = [];
86+ $ this ->isNested = false ;
87+ }
88+
89+ /**
90+ * @param string $operationName
91+ *
92+ * @return Query
93+ */
94+ public function setOperationName (string $ operationName )
95+ {
96+ if (!empty ($ operationName )) {
97+ $ this ->operationName = " $ operationName " ;
98+ }
99+
100+ return $ this ;
101+ }
102+
103+ /**
104+ * @param array $variables
105+ *
106+ * @return Query
62107 */
63- public function __construct ( string $ objectName )
108+ public function setVariables ( array $ variables )
64109 {
65- $ this ->object = $ objectName ;
66- $ this ->arguments = [];
67- $ this ->selectionSet = [];
68- $ this ->isNested = false ;
110+ $ nonVarElements = array_filter ($ variables , function ($ e ) {
111+ return !$ e instanceof Variable;
112+ });
113+ if (count ($ nonVarElements ) > 0 ) {
114+ throw new InvalidVariableException ('At least one of the elements of the variables array provided is not an instance of GraphQL \\Variable ' );
115+ }
116+
117+ $ this ->variables = $ variables ;
118+
119+ return $ this ;
69120 }
70121
71122 /**
@@ -116,6 +167,34 @@ public function setSelectionSet(array $selectionSet): Query
116167 return $ this ;
117168 }
118169
170+ /**
171+ * @return string
172+ */
173+ protected function constructVariables (): string
174+ {
175+ if (empty ($ this ->variables )) {
176+ return '' ;
177+ }
178+
179+ $ varsString = '( ' ;
180+ $ first = true ;
181+ foreach ($ this ->variables as $ variable ) {
182+
183+ // Append space at the beginning if it's not the first item on the list
184+ if ($ first ) {
185+ $ first = false ;
186+ } else {
187+ $ varsString .= ' ' ;
188+ }
189+
190+ // Append variable string value to the variables string
191+ $ varsString .= (string ) $ variable ;
192+ }
193+ $ varsString .= ') ' ;
194+
195+ return $ varsString ;
196+ }
197+
119198 /**
120199 * @return string
121200 */
@@ -188,13 +267,23 @@ protected function constructSelectionSet(): string
188267 public function __toString ()
189268 {
190269 $ queryFormat = static ::QUERY_FORMAT ;
191- if (!$ this ->isNested && $ this ->object !== static ::OPERATION_TYPE ) {
192- $ queryFormat = static :: OPERATION_TYPE . " { \n" . static ::QUERY_FORMAT . "\n} " ;
270+ if (!$ this ->isNested && $ this ->fieldName !== static ::OPERATION_TYPE ) {
271+ $ queryFormat = $ this -> generateSignature () . " { \n" . static ::QUERY_FORMAT . "\n} " ;
193272 }
194273 $ argumentsString = $ this ->constructArguments ();
195274 $ selectionSetString = $ this ->constructSelectionSet ();
196275
197- return sprintf ($ queryFormat , $ this ->object , $ argumentsString , $ selectionSetString );
276+ return sprintf ($ queryFormat , $ this ->fieldName , $ argumentsString , $ selectionSetString );
277+ }
278+
279+ /**
280+ * @return string
281+ */
282+ protected function generateSignature (): string
283+ {
284+ $ signatureFormat = '%s%s%s ' ;
285+
286+ return sprintf ($ signatureFormat , static ::OPERATION_TYPE , $ this ->operationName , $ this ->constructVariables ());
198287 }
199288
200289 /**
0 commit comments