Skip to content
This repository was archived by the owner on Feb 10, 2019. It is now read-only.

Commit f3f7b12

Browse files
committed
First v1 commit
1 parent 5d6be2a commit f3f7b12

18 files changed

+690
-179
lines changed

Readme.md

Lines changed: 132 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This package is compatible with Eloquent model (or any other data source). See t
2222
```json
2323
{
2424
"require": {
25-
"folklore/graphql": "0.4.*"
25+
"folklore/graphql": "~1.0"
2626
}
2727
}
2828
```
@@ -61,8 +61,47 @@ $ php artisan vendor:publish --provider="Folklore\GraphQL\GraphQLServiceProvider
6161
config/graphql.php
6262
```
6363

64+
## Upgrade to 1.0
65+
66+
The main difference between versions prior 1.0 and 1.0 is the use of multiple schemas. You will need to update your config to have the following structure:
67+
68+
```php
69+
70+
'schema' => 'default',
71+
72+
'schemas' => [
73+
'default' => [
74+
'query' => [
75+
// Your queries
76+
],
77+
'mutation' => [
78+
// Your muttations
79+
]
80+
]
81+
]
82+
83+
```
84+
85+
If you want to use routes that can accept schema name, you need to change `routes` to the following:
86+
87+
```php
88+
89+
'routes' => '{graphql_schema?}',
90+
91+
// or if you use different routes for query and mutation
92+
93+
'routes' => [
94+
'query' => 'query/{graphql_schema?}',
95+
'mutation' => 'mutation/{graphql_schema?}'
96+
],
97+
98+
```
99+
100+
The method `GraphQL::addQuery` and `GraphQL::addMutation` has been removed since it doesn't make sense with multiple schemas. You can use the new `GraphQL::addSchema` method to add new schemas.
101+
64102
## Usage
65103

104+
- [Schemas](#schemas)
66105
- [Creating a query](#creating-a-query)
67106
- [Creating a mutation](#creating-a-mutation)
68107
- [Adding validation to mutation](#adding-validation-to-mutation)
@@ -72,6 +111,83 @@ config/graphql.php
72111
- [Custom field](#custom-field)
73112
- [Eager loading relationships](#eager-loading-relationships)
74113

114+
### Schemas
115+
Starting from version 1.0, you can define multiple schemas. Having multiple schemas can be useful if, for example, you want an endpoint that is public and another one that needs authentication.
116+
117+
You can define multiple schemas in the config:
118+
119+
```php
120+
121+
'schema' => 'default',
122+
123+
'schemas' => [
124+
'default' => [
125+
'query' => [
126+
//'users' => 'App\GraphQL\Query\UsersQuery'
127+
],
128+
'mutation' => [
129+
//'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation'
130+
]
131+
],
132+
'secret' => [
133+
'query' => [
134+
//'users' => 'App\GraphQL\Query\UsersQuery'
135+
],
136+
'mutation' => [
137+
//'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation'
138+
]
139+
]
140+
]
141+
142+
```
143+
144+
Or you can add schema using the facade:
145+
146+
```php
147+
148+
GraphQL::addSchema('secret', [
149+
'query' => [
150+
//'users' => 'App\GraphQL\Query\UsersQuery'
151+
],
152+
'mutation' => [
153+
//'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation'
154+
]
155+
]);
156+
157+
```
158+
159+
Afterwards, you can build the schema using the facade:
160+
161+
```php
162+
163+
// Will return the default schema defined by 'schema' in the config
164+
$schema = GraphQL::schema();
165+
166+
// Will return the 'secret' schema
167+
$schema = GraphQL::schema('secret');
168+
169+
// Will build a new schema
170+
$schema = GraphQL::schema([
171+
'query' => [
172+
//'users' => 'App\GraphQL\Query\UsersQuery'
173+
],
174+
'mutation' => [
175+
//'updateUserEmail' => 'App\GraphQL\Query\UpdateUserEmailMutation'
176+
]
177+
]);
178+
179+
```
180+
181+
Or you can request the endpoint of a specific schema
182+
183+
```
184+
// Default schema
185+
http://homestead.app/graphql?query=query+FetchUsers{users{id,email}}
186+
187+
// Secret schema
188+
http://homestead.app/graphql/secret?query=query+FetchUsers{users{id,email}}
189+
```
190+
75191
### Creating a query
76192

77193
First you need to create a type.
@@ -147,7 +263,7 @@ Then you need to define a query that returns this type (or a list). You can also
147263
class UsersQuery extends Query {
148264

149265
protected $attributes = [
150-
'name' => 'Users query'
266+
'name' => 'users'
151267
];
152268

153269
public function type()
@@ -187,23 +303,17 @@ Add the query to the `config/graphql.php` configuration file
187303

188304
```php
189305

190-
'schema' => [
191-
'query' => [
192-
'users' => 'App\GraphQL\Query\UsersQuery'
193-
],
194-
// ...
306+
'schemas' => [
307+
'default' => [
308+
'query' => [
309+
'users' => 'App\GraphQL\Query\UsersQuery'
310+
],
311+
// ...
312+
]
195313
]
196314

197315
```
198316

199-
Or using the `GraphQL` facade
200-
201-
```php
202-
203-
GraphQL::addQuery('App\GraphQL\Query\UsersQuery', 'users');
204-
205-
```
206-
207317
And that's it. You should be able to query GraphQL with a request to the url `/graphql` (or anything you choose in your config). Try a GET request with the following `query` input
208318

209319
```
@@ -238,7 +348,7 @@ For example a mutation to update the password of a user. First you need to defin
238348
class UpdateUserPasswordMutation extends Mutation {
239349

240350
protected $attributes = [
241-
'name' => 'UpdateUserPassword'
351+
'name' => 'updateUserPassword'
242352
];
243353

244354
public function type()
@@ -279,22 +389,16 @@ You then add the muation to the `config/graphql.php` configuration file
279389
```php
280390

281391
'schema' => [
282-
'mutation' => [
283-
'updateUserPassword' => 'App\GraphQL\Mutation\UpdateUserPasswordMutation'
284-
],
285-
// ...
392+
'default' => [
393+
'mutation' => [
394+
'updateUserPassword' => 'App\GraphQL\Mutation\UpdateUserPasswordMutation'
395+
],
396+
// ...
397+
]
286398
]
287399

288400
```
289401

290-
Or using the `GraphQL` facade
291-
292-
```php
293-
294-
GraphQL::addMutation('App\GraphQL\Mutation\UpdateUserPasswordMutation', 'updateUserPassword');
295-
296-
```
297-
298402
You should then be able to use the following query on your endpoint to do the mutation.
299403

300404
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require": {
2121
"php": ">=5.5.9",
2222
"illuminate/support": "~5.1",
23-
"webonyx/graphql-php": "0.6.*"
23+
"webonyx/graphql-php": "~0.7.0"
2424
},
2525
"require-dev": {
2626
"orchestra/testbench": "~3.1"

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<testsuites>
1414
<testsuite name="Folklore/GraphQL Test Suite">
1515
<file>./tests/GraphQLTest.php</file>
16+
<file>./tests/TypeTest.php</file>
1617
</testsuite>
1718
</testsuites>
1819
</phpunit>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php namespace Folklore\GraphQL\Events;
2+
3+
class SchemaAdded
4+
{
5+
public $schema;
6+
public $name;
7+
8+
public function __construct($schema, $name)
9+
{
10+
$this->schema = $schema;
11+
$this->name = $name;
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php namespace Folklore\GraphQL\Events;
2+
3+
class TypeAdded
4+
{
5+
public $type;
6+
public $name;
7+
8+
public function __construct($type, $name)
9+
{
10+
$this->type = $type;
11+
$this->name = $name;
12+
}
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php namespace Folklore\GraphQL\Exception;
2+
3+
use Exception;
4+
5+
class SchemaNotFound extends Exception {}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php namespace Folklore\GraphQL\Exception;
2+
3+
use Exception;
4+
5+
class TypeNotFound extends Exception {}

0 commit comments

Comments
 (0)