This project is based of Folklore\GraphQL package, adding Helpers that make more easy the integration with Laravel.
For more details check: https://github.com/Folkloreatelier/laravel-graphql
-
Getting Started
-
Custom Types
-
Custom Fields
composer require xpromx/laravel-graphql
in /config/graphql.php check the example inside this repository.
uncomment the follow lines:
$app->withFacades();
$app->withEloquent();
$app->register(App\Providers\AppServiceProvider::class);
$app->register(App\Providers\AuthServiceProvider::class);
then add this lines in the same file
$app->configure('graphql');
$app->register(Folklore\GraphQL\LumenServiceProvider::class);
inside this folder: /app/GraphQL with these other folders inside: /Types and /Query
An in-browser IDE for exploring GraphQL.
http://project-name.test/graphiql
Creating new Types = Your Laravel Models check the examples in /Examples/Type folder inside this repository. check the custom types section in this doc.
Register the Types in your /config/graphql.php.
<?php
// app/GraphQL/Type/UserType.php
namespace App\GraphQL\Type;
use Xpromx\GraphQL\Definition\Type;
use Xpromx\GraphQL\Type as BaseType;
class UserType extends BaseType
{
protected $attributes = [
'name' => 'UserType',
'description' => 'A User',
'model' => \App\User::class // Laravel Model
];
public function fields()
{
return [
'id' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The id of the user'
],
'created_at' => [
'type' => Type::date(),
'description' => 'When the user was created'
],
'updated_at' => [
'type' => Type::date(),
'description' => 'When the user was updated'
],
];
}
}
Creating new Queries = Endpoints of your API. Check the examples in /Examples/Query folder inside this repository. example:
Register the Queries in your /config/graphql.php.
<?php
// app/GraphQL/Query/UserQuery.php
namespace App\GraphQL\Query;
use Xpromx\GraphQL\Query;
use Xpromx\GraphQL\Definition\Type;
class UsersQuery extends Query
{
protected $attributes = [
'name' => 'UsersQuery',
'description' => 'A Users Query'
];
public function type()
{
return Type::connection('user'); // UserType
}
}
these are the filters you can apply automatically for your Queries. In order to use advance filters you have to register the types in your graphql.php config.
'types' => [
'Filter' => 'Xpromx\GraphQL\Filter\FilterType',
'FilterCondition' => 'Xpromx\GraphQL\Filter\FilterConditionEnum',
]
Query Example:
users(
id: 1,
limit: 30,
page: 2,
hasRelation: 'user',
doesntHaveRelation: 'comments',
orderBy: 'id DESC',
filter: [{field: "email", condition:CONTAINS, value:"@gmail.com"}]
)
{
nodes {
...
}
pageInfo {
...
}
}
- GT
- GTE
- LT
- LTE
- EQUAL
- CONTAINS
- NOT_CONTAINS
- STARTS_WITH
- ENDS_WTIH
- IN
- NOT_IN
- NOT_EQUAL
- NULL
- NOT_NULL
Will create the connection for the Type selected, this connection will simplify the queries and adapt the results to http://graphql.org/ standars. format:
{
userQuery(page:1, limit:20){
nodes{
id,
first_name
email
...
},
pageInfo{
current_page
total
}
}
}
Return the dates formated for humans
'updated_at' => [
'type' => Type::date(),
'description' => 'When the user was updated'
]
Return the time formated for humans
'duration' => [
'type' => Type::time(),
'description' => 'Movie duration'
]
This one return a list of the Type selected, for Relations OneToMany
// UserType.php
'comments' => Type::hasMany('comment')
For OneToOne Relations
// CommentType
'user' => Type::hasOne('user')
When you need to return a Json object use the MetaType field
// UserType
'meta' => [
'type' => Type::meta(),
'description' => 'Extra information about this user'
]
Return the pagination fields, this one is automatically applied in the ConnectionType, and the fields are:
{
pageInfo
{
current_page,
next_page,
prev_page,
last_page,
per_page,
total
}
}
You can use a custom date field to have a default format and also be able to change the format from the query.
public function fields()
{
return [
'updated_at' => Type::dateField($field='updated_at', $format='M j, Y'),
]
}
{
userQuery(page:1, limit:20){
nodes{
id,
first_name
created_at(format:"Y-m-d")
...
},
}
}
You can use a custom time field to have a default format and also be able to change the format from the query.
public function fields()
{
return [
'updated_at' => Type::timeField($field='updated_at', $format='H:i'),
]
}
{
userQuery(page:1, limit:20){
nodes{
id,
first_name
created_at(format:"H:i:s")
...
},
}
}