Skip to content

Commit 2f30ac8

Browse files
committed
working on the documentation
1 parent 1601b8e commit 2f30ac8

File tree

10 files changed

+50
-34
lines changed

10 files changed

+50
-34
lines changed

README.md

+50-26
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@
44
[![Code Coverage](https://scrutinizer-ci.com/g/Youshido/GraphQL/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/Youshido/GraphQL/?branch=master)
55
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/8b8ab2a2-32fb-4298-a986-b75ca523c7c9/mini.png)](https://insight.sensiolabs.com/projects/8b8ab2a2-32fb-4298-a986-b75ca523c7c9)
66

7-
This is a pure PHP realization of the GraphQL protocol based on the working draft of the specification located on https://github.com/facebook/graphql.
7+
This is a clean PHP realization of the GraphQL protocol based on the working draft of the specification located on https://github.com/facebook/graphql.
88

9-
GraphQL is a modern replacement of the REST API approach. It advanced in a lof ot ways and has fundamental advantages:
10-
- reusable API for different versions and devices
11-
- a complete new level of distinguishing the backend and the frontend logic
9+
GraphQL is a modern replacement of the REST API approach. It advanced in many ways and has fundamental improvements over the old not-so-good REST:
1210

11+
- self-checks embedded on the ground level of your app
12+
- reusable API for different client versions and devices – no need in "/v1" and "/v2" anymore
13+
- a complete new level of distinguishing the backend and the frontend logic
14+
- easily generated documentation and incredibly easy way to explore API for other developers
15+
- once your architecture is complete – simple changes on the client does not require you to change API
1316

14-
Current package is and will be trying to keep up with the latest revision of the GraphQL specification which is of April 2016 currently.
17+
It could be hard to believe, but give it a try and you'll be rewarded with much better architecture and so much easier to support code.
18+
19+
_Current package is and will be trying to keep up with the latest revision of the GraphQL specification which is of April 2016 currently._
1520

1621
## Getting started
1722

18-
You should be better off starting with some examples, and everybody's using Star Wars trilogy as a domain of knowledge in their examples.
23+
You should be better off starting with some examples, and "Star Wars" become a somewhat "Hello world" example for the GraphQL frameworks.
1924
We have that too and if you looking for just that – go directly by this link – [Star Wars example](https://github.com/Youshido/GraphQL/Tests/StarWars).
20-
On the other hand we believe it's easier to start with something more common so let's get started.
25+
On the other hand based on the feedback we prepared a step-by-step for those who want to get up to speed fast.
2126

2227
### Installation
2328

2429
You should simply install this package using the composer. If you're not familiar with it you should check out the [manual](https://getcomposer.org/doc/00-intro.md).
25-
Add the following package to your `composer.json` and run `composer update`
30+
Add the following package to your `composer.json`:
2631

2732
```
2833
{
@@ -31,9 +36,11 @@ Add the following package to your `composer.json` and run `composer update`
3136
}
3237
}
3338
```
39+
Run `composer update`.
3440

35-
Let's check if everything is good by setting up a simple schema that will return current time.
36-
You can find this example in the examples directory – [01_sandbox](https://github.com/Youshido/GraphQL/examples/01_sandbox).
41+
After the successful message, Let's check if everything is good by setting up a simple schema that will return current time.
42+
(you can find this example in the examples directory – [01_sandbox](https://github.com/Youshido/GraphQL/examples/01_sandbox))
43+
3744
```php
3845
<?php
3946
namespace Sandbox;
@@ -62,7 +69,6 @@ $processor->setSchema(new Schema([
6269

6370
$res = $processor->processRequest('{ currentTime }')->getResponseData();
6471
print_r($res);
65-
6672
```
6773

6874
If everything was set up correctly – you should see response with your current time:
@@ -73,13 +79,14 @@ If everything was set up correctly – you should see response with your curren
7379
```
7480

7581
If not – check that you have the latest composer version and that you've created your test file in the same directory you have `vendor` folder in.
82+
You can always use a script from `examples` folder. Simply run `php vendor/youshido/GraphQL/examples/01_sandbox/index.php`.
7683

7784
## Usage
7885

79-
Let's architect a schema for the simple Blog.
86+
Now, as an example, let's architect a schema for the Blog.
8087

81-
We'll keep our Blog simple but it will surely have Users who write Posts and leave Comments.
82-
If you never seen a GraphQL queries before – it's a simple text query very much similar to the json/yaml format.
88+
We'll keep it simple but our Blog will surely have Users who write Posts and leave Comments.
89+
If you never seen a GraphQL queries before – it's a simple text query structured very much similar to the json/yaml format.
8390
Here's an example of the query that returns title and summary of the latest Post:
8491
```
8592
latestPost {
@@ -88,7 +95,7 @@ Here's an example of the query that returns title and summary of the latest Post
8895
}
8996
```
9097

91-
Supposedly our server should return the following structure:
98+
Supposedly our server should reply with a response structured like following:
9299
```
93100
{
94101
data: {
@@ -105,19 +112,36 @@ Now, let's create a schema and the backend that can handle this simple request.
105112
### Creating Post schema
106113

107114
We believe you'll be using our package along with your favorite framework (we have a Symfony version [here](http://github.com/Youshido/GraphqlBundle)).
108-
But for the purpose of the current demonstration we'll keep it as plain php code.
109-
110-
*schema.php*
111-
```php
112-
113-
114-
115-
```
116-
117-
115+
But for the purpose of the current example we'll keep it as plain php code.
116+
_(you can check out the complete example by the following link – (https://github.com/Youshido/GraphQL/examples/02_Blog)_
118117

118+
We'll take a quick look on different approaches you can use to define your schema. Even though inline approach might seem to be easier and faster we strongly recommend to use an object based because it will give you more flexibility and freedom as your project grows.
119119

120+
#### Inline approach
120121

122+
So let's start by creating post type. For now we'll have only two fields – title and summary:
121123

124+
*index.php*
125+
```php
126+
//...
127+
$rootQueryType = new ObjectType([
128+
'name' => 'RootQueryType',
129+
'fields' => [
130+
'latestPost' => new ObjectType([
131+
'name' => 'Post',
132+
'fields' => [
133+
'title' => new StringType(),
134+
'summary' => new StringType(),
135+
],
136+
'resolve' => function () {
137+
return [
138+
"title" => "New approach in API has been revealed",
139+
"summary" => "This post will describe a new approach to create and maintain APIs",
140+
];
141+
}
142+
])
143+
]
144+
]);
145+
//...
146+
```
122147

123-
File renamed without changes.
File renamed without changes.

examples/Blog/index.php examples/02_blog/index.php

-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,6 @@
1010
'name' => 'RootQueryType',
1111
]);
1212

13-
function resolvePost()
14-
{
15-
return [
16-
"title" => "Interesting approach",
17-
"summary" => "This new GraphQL library for PHP works really well",
18-
];
19-
}
20-
2113
require_once __DIR__ . '/structures/object-inline.php';
2214

2315
$processor = new Processor();
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)