You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
8
8
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:
12
10
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
13
16
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._
15
20
16
21
## Getting started
17
22
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.
19
24
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.
21
26
22
27
### Installation
23
28
24
29
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`:
26
31
27
32
```
28
33
{
@@ -31,9 +36,11 @@ Add the following package to your `composer.json` and run `composer update`
31
36
}
32
37
}
33
38
```
39
+
Run `composer update`.
34
40
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))
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
73
79
```
74
80
75
81
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`.
76
83
77
84
## Usage
78
85
79
-
Let's architect a schema for the simple Blog.
86
+
Now, as an example, let's architect a schema for the Blog.
80
87
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.
83
90
Here's an example of the query that returns title and summary of the latest Post:
84
91
```
85
92
latestPost {
@@ -88,7 +95,7 @@ Here's an example of the query that returns title and summary of the latest Post
88
95
}
89
96
```
90
97
91
-
Supposedly our server should return the following structure:
98
+
Supposedly our server should reply with a response structured like following:
92
99
```
93
100
{
94
101
data: {
@@ -105,19 +112,36 @@ Now, let's create a schema and the backend that can handle this simple request.
105
112
### Creating Post schema
106
113
107
114
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)_
118
117
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.
119
119
120
+
#### Inline approach
120
121
122
+
So let's start by creating post type. For now we'll have only two fields – title and summary:
121
123
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",
0 commit comments