11# PHP GraphQL Client
2- [ ![ Build Status] ( https://travis-ci.org/mghoneimy/php-graphql-client.svg?branch=master )] ( https://travis-ci.org/mghoneimy/php-graphql-client )
3- [ ![ Codacy Badge] ( https://api.codacy.com/project/badge/Grade/cb5e0708c4244c1a848be668dbcda8b0 )] ( https://app.codacy.com/app/mghoneimy/php-graphql-client?utm_source=github.com&utm_medium=referral&utm_content=mghoneimy/php-graphql-client&utm_campaign=Badge_Grade_Dashboard )
4- [ ![ Codacy Badge] ( https://api.codacy.com/project/badge/Coverage/c2b0ae3a556547c38e1247d63228adde )] ( https://www.codacy.com/app/mghoneimy/php-graphql-client?utm_source=github.com&utm_medium=referral&utm_content=mghoneimy/php-graphql-client&utm_campaign=Badge_Coverage )
5- [ ![ Total Downloads] ( https://poser.pugx.org/gmostafa/php-graphql-client/downloads )] ( https://packagist.org/packages/gmostafa/php-graphql-client )
6- [ ![ Latest Stable Version] ( https://poser.pugx.org/gmostafa/php-graphql-client/v/stable )] ( https://packagist.org/packages/gmostafa/php-graphql-client )
2+ [ ![ Build
3+ Status] ( https://travis-ci.org/mghoneimy/php-graphql-client.svg?branch=master )] ( https://travis-ci.org/mghoneimy/php-graphql-client )
4+ [ ![ Codacy
5+ Badge] ( https://api.codacy.com/project/badge/Grade/cb5e0708c4244c1a848be668dbcda8b0 )] ( https://app.codacy.com/app/mghoneimy/php-graphql-client?utm_source=github.com&utm_medium=referral&utm_content=mghoneimy/php-graphql-client&utm_campaign=Badge_Grade_Dashboard )
6+ [ ![ Codacy
7+ Badge] ( https://api.codacy.com/project/badge/Coverage/c2b0ae3a556547c38e1247d63228adde )] ( https://www.codacy.com/app/mghoneimy/php-graphql-client?utm_source=github.com&utm_medium=referral&utm_content=mghoneimy/php-graphql-client&utm_campaign=Badge_Coverage )
8+ [ ![ Total
9+ Downloads] ( https://poser.pugx.org/gmostafa/php-graphql-client/downloads )] ( https://packagist.org/packages/gmostafa/php-graphql-client )
10+ [ ![ Latest Stable
11+ Version] ( https://poser.pugx.org/gmostafa/php-graphql-client/v/stable )] ( https://packagist.org/packages/gmostafa/php-graphql-client )
712[ ![ License] ( https://poser.pugx.org/gmostafa/php-graphql-client/license )] ( https://packagist.org/packages/gmostafa/php-graphql-client )
813
9- A GraphQL client written in PHP which provides very simple, yet powerful, query generator classes that make the process
10- of interacting with a GraphQL server a very simple one.
14+ A GraphQL client written in PHP which provides very simple, yet powerful, query
15+ generator classes that make the process of interacting with a GraphQL server a
16+ very simple one.
1117
1218# Usage
19+
1320There are 3 primary ways to use this package to generate your GraphQL queries:
14- 1 . Query Class: Simple class that maps to GraphQL queries. It's designed to manipulate queries with ease and speed.
15- 2 . QueryBuilder Class: Builder class that can be used to generate ` Query ` objects dynamically. It's design to be used in
16- cases where a query is being build in a dynamic fashion.
17- 3 . PHP GraphQL-OQM: An extension to this package. It Eliminates the need to write any GraphQL queries or refer to the
18- API documentation or syntax. It generates query objects from the API schema, declaration exposed through GraphQL's
19- introspection, which can then be simply interacted with.
21+
22+ 1 . Query Class: Simple class that maps to GraphQL queries. It's designed to
23+ manipulate queries with ease and speed.
24+ 2 . QueryBuilder Class: Builder class that can be used to generate ` Query `
25+ objects dynamically. It's design to be used in cases where a query is being
26+ build in a dynamic fashion.
27+ 3 . PHP GraphQL-OQM: An extension to this package. It Eliminates the need to
28+ write any GraphQL queries or refer to the API documentation or syntax. It
29+ generates query objects from the API schema, declaration exposed through
30+ GraphQL's introspection, which can then be simply interacted with.
2031
2132# Installation
33+
2234Run the following command to install the package using composer:
35+
2336```
24- composer require gmostafa/php-graphql-client
37+ $ composer require gmostafa/php-graphql-client
2538```
2639
2740# Object-to-Query-Mapper Extension
28- To avoid the hassle of having to write _ any_ queries and just interact with PHP objects generated from your API schema
29- visit PHP GraphQL OQM repository at:
30- https://github.com/mghoneimy/php-graphql-oqm
41+
42+ To avoid the hassle of having to write _ any_ queries and just interact with PHP
43+ objects generated from your API schema visit [ PHP GraphQL OQM repository] (
44+ https://github.com/mghoneimy/php-graphql-oqm )
3145
3246# Query Examples
47+
3348## Simple Query
49+
3450```
3551$gql = (new Query('companies'))
3652 ->setSelectionSet(
@@ -40,7 +56,9 @@ $gql = (new Query('companies'))
4056 ]
4157 );
4258```
43- This simple query will retrieve all companies displaying their names and serial numbers.
59+
60+ This simple query will retrieve all companies displaying their names and serial
61+ numbers.
4462
4563## Nested Queries
4664```
@@ -60,11 +78,15 @@ $gql = (new Query('companies'))
6078 ]
6179 );
6280```
63- This query is a more complex one, retrieving not just scalar fields, but object fields as well. This query returns all
64- companies, displaying their names, serial numbers, and for each company, all its branches, displaying the branch address,
65- and for each address, it retrieves all contracts bound to this address, displaying their dates.
81+
82+ This query is a more complex one, retrieving not just scalar fields, but object
83+ fields as well. This query returns all companies, displaying their names, serial
84+ numbers, and for each company, all its branches, displaying the branch address,
85+ and for each address, it retrieves all contracts bound to this address,
86+ displaying their dates.
6687
6788## Query With Arguments
89+
6890```
6991$gql = (new Query('companies'))
7092 ->setArguments(['name' => 'Tech Co.', 'first' => 3])
@@ -75,10 +97,13 @@ $gql = (new Query('companies'))
7597 ]
7698 );
7799```
78- This query does not retrieve all companies by adding arguments. This query will retrieve the first 3 companies with the
79- name "Tech Co.", displaying their names and serial numbers.
100+
101+ This query does not retrieve all companies by adding arguments. This query will
102+ retrieve the first 3 companies with the name "Tech Co.", displaying their names
103+ and serial numbers.
80104
81105## Query With Array Argument
106+
82107```
83108$gql = (new Query('companies'))
84109 ->setArguments(['serialNumbers' => [159, 260, 371]])
@@ -89,10 +114,13 @@ $gql = (new Query('companies'))
89114 ]
90115 );
91116```
92- This query is a special case of the arguments query. In this example, the query will retrieve only the companies with
93- serial number in one of 159, 260, and 371, displaying the name and serial number.
117+
118+ This query is a special case of the arguments query. In this example, the query
119+ will retrieve only the companies with serial number in one of 159, 260, and 371,
120+ displaying the name and serial number.
94121
95122## Query With Input Object Argument
123+
96124```
97125$gql = (new Query('companies'))
98126 ->setArguments(['filter' => new RawObject('{name_starts_with: "Face"}')])
@@ -103,15 +131,20 @@ $gql = (new Query('companies'))
103131 ]
104132 );
105133```
106- This query is another special case of the arguments query. In this example, we're setting a custom input object "filter"
107- with some values to limit the companies being returned. We're setting the filter "name_starts_with" with value "Face".
108- This query will retrieve only the companies whose names start with the phrase "Face".
109134
110- The RawObject class being constructed is used for injecting the string into the query as it is. Whatever string is input
111- into the RawObject constructor will be put in the query as it is without any custom formatting normally done by the
135+ This query is another special case of the arguments query. In this example,
136+ we're setting a custom input object "filter" with some values to limit the
137+ companies being returned. We're setting the filter "name_starts_with" with
138+ value "Face". This query will retrieve only the companies whose names
139+ start with the phrase "Face".
140+
141+ The RawObject class being constructed is used for injecting the string into the
142+ query as it is. Whatever string is input into the RawObject constructor will be
143+ put in the query as it is without any custom formatting normally done by the
112144query class.
113145
114146## Query With Variables
147+
115148```
116149$gql = (new Query('companies'))
117150 ->setVariables(
@@ -128,23 +161,32 @@ $gql = (new Query('companies'))
128161 ]
129162 );
130163```
131- This query shows how variables can be used in this package to allow for dynamic requests enabled by GraphQL standards.
164+
165+ This query shows how variables can be used in this package to allow for dynamic
166+ requests enabled by GraphQL standards.
132167
133168### The Variable Class
134- The Variable class is an immutable class that represents a variable in GraphQL standards. Its constructor receives 4
135- arguments:
169+
170+ The Variable class is an immutable class that represents a variable in GraphQL
171+ standards. Its constructor receives 4 arguments:
172+
136173- name: Represents the variable name
137174- 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.
175+ - isRequired (Optional): Represents if the variable is required or not, it's
176+ false by default
177+ - defaultValue (Optional): Represents the default value to be assigned to the
178+ variable. The default value will only be considered
179+ if the isRequired argument is set to false.
141180
142181# The Query Builder
143- The QueryBuilder class can be used to construct Query objects dynamically, which can be useful in some cases. It works
144- very similarly to the Query class, but the Query building is divided into steps.
145182
146- That's how the "Query With Input Object Argument" example can be created using the
147- QueryBuilder:
183+ The QueryBuilder class can be used to construct Query objects dynamically, which
184+ can be useful in some cases. It works very similarly to the Query class, but the
185+ Query building is divided into steps.
186+
187+ That's how the "Query With Input Object Argument" example can be created using
188+ the QueryBuilder:
189+
148190```
149191$builder = (new QueryBuilder('companies'))
150192 ->setVariable('namePrefix', 'String', true)
@@ -155,11 +197,14 @@ $gql = $builder->getQuery();
155197```
156198
157199# Constructing The Client
158- A Client object can easily be instantiated by providing the GraphQL endpoint URL. The Client constructor also receives
159- an optional "authorizationHeaders" array, which can be used to add authorization headers to all requests being sent to
160- the GraphQL server.
200+
201+ A Client object can easily be instantiated by providing the GraphQL endpoint
202+ URL. The Client constructor also receives an optional "authorizationHeaders"
203+ array, which can be used to add authorization headers to all requests being sent
204+ to the GraphQL server.
161205
162206Example:
207+
163208```
164209$client = new Client(
165210 'http://api.graphql.com',
@@ -168,21 +213,29 @@ $client = new Client(
168213```
169214
170215# Running Queries
216+
171217## Result Formatting
172- Running query with the GraphQL client and getting the results in object structure:
218+
219+ Running query with the GraphQL client and getting the results in object
220+ structure:
221+
173222```
174223$results = $client->runQuery($gql);
175224$results->getData()->Company[0]->branches;
176225```
177226Or getting results in array structure:
227+
178228```
179229$results = $client->runQuery($gql, true);
180230$results->getData()['Company'][1]['branches']['address']
181231```
182232
183233## 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:
234+
235+ Running queries containing variables requires passing an associative array which
236+ maps variable names (keys) to variable values (values) to the ` runQuery ` method.
237+ Here's an example:
238+
186239```
187240$gql = (new Query('companies'))
188241 ->setVariables(
@@ -203,10 +256,12 @@ $results = $client->runQuery($gql, true, $variablesArray);
203256```
204257
205258# Mutations
206- Mutations follow the same rules of queries in GraphQL, they select fields on returned objects, receive arguments, and
207- can have sub-fields.
259+
260+ Mutations follow the same rules of queries in GraphQL, they select fields on
261+ returned objects, receive arguments, and can have sub-fields.
208262
209263Here's a sample example on how to construct and run mutations:
264+
210265```
211266$mutation = (new Mutation('createCompany'))
212267 ->setArguments(['companyObject' => new RawObject('{name: "Trial Company", employees: 200}')])
@@ -219,11 +274,15 @@ $mutation = (new Mutation('createCompany'))
219274 );
220275$results = $client->runQuery($mutation);
221276```
277+
222278Mutations can be run by the client the same way queries are run.
223279
224280## 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:
281+
282+ Mutations can utilize the variables in the same way Queries can. Here's an
283+ example on how to use the variables to pass input objects to the GraphQL server
284+ dynamically:
285+
227286```
228287$mutation = (new Mutation('createCompany'))
229288 ->setVariables([new Variable('company', 'CompanyInputObject', true)])
@@ -234,22 +293,28 @@ $client->runQuery(
234293 $mutation, true, $variables
235294);
236295```
296+
237297These are the resulting mutation and the variables passed with it:
298+
238299```
239300mutation($company: CompanyInputObject!) {
240301 createCompany(companyObject: $company)
241302}
242303{"company":{"name":"Tech Company","type":"Testing","size":"Medium"}}
243304```
305+
244306# Live API Example
245- GraphQL Pokemon is a very cool public GraphQL API available to retrieve Pokemon data. The API is available publicly on
246- the web, we'll use it to demo the capabilities of this client.
307+
308+ GraphQL Pokemon is a very cool public GraphQL API available to retrieve Pokemon
309+ data. The API is available publicly on the web, we'll use it to demo the
310+ capabilities of this client.
247311
248312Github Repo link: https://github.com/lucasbento/graphql-pokemon
249-
313+
250314API link: https://graphql-pokemon.now.sh/
251315
252316This query retrieves any pokemon's evolutions and their attacks:
317+
253318```
254319query($name: String!) {
255320 pokemon(name: $name) {
@@ -277,7 +342,9 @@ query($name: String!) {
277342
278343```
279344
280- That's how this query can be written using the query class and run using the client:
345+ That's how this query can be written using the query class and run using the
346+ client:
347+
281348```
282349$client = new Client(
283350 'https://graphql-pokemon.now.sh/'
@@ -323,7 +390,10 @@ catch (QueryError $exception) {
323390}
324391print_r($results->getData()['pokemon']);
325392```
326- Or alternatively, That's how this query can be generated using the QueryBuilder class:
393+
394+ Or alternatively, That's how this query can be generated using the QueryBuilder
395+ class:
396+
327397```
328398$client = new Client(
329399 'https://graphql-pokemon.now.sh/'
@@ -361,8 +431,11 @@ print_r($results->getData()['pokemon']);
361431```
362432
363433# Running Raw Queries
364- Although not the primary goal of this package, but it supports running raw string queries, just like any other client
365- using the ` runRawQuery ` method in the ` Client ` class. Here's an example on how to use it:
434+
435+ Although not the primary goal of this package, but it supports running raw
436+ string queries, just like any other client using the ` runRawQuery ` method in the
437+ ` Client ` class. Here's an example on how to use it:
438+
366439```
367440$gql = <<<QUERY
368441query {
0 commit comments