Use Facebook GraphQL with Laravel 5 or Lumen. It is based on the PHP implementation here. You can find more information about GraphQL in the GraphQL Introduction on the React blog or you can read the GraphQL specifications. This is a work in progress.
This package is compatible with Eloquent model (or any other data source) and Relay. See the example below.
1- Require the package via Composer in your composer.json
.
{
"require": {
"folklore/graphql": "dev-feature/relay"
}
}
2- Run Composer to install or update the new requirement.
$ composer install
or
$ composer update
1- Add the service provider to your app/config/app.php
file
'Folklore\GraphQL\ServiceProvider',
2- Add the facade to your app/config/app.php
file
'GraphQL' => 'Folklore\GraphQL\Support\Facades\GraphQL',
3- Publish the configuration file
$ php artisan vendor:publish --provider="Folklore\GraphQL\ServiceProvider"
4- Review the configuration file
config/graphql.php
1- Load the service provider in bootstrap/app.php
$app->register(Folklore\GraphQL\LumenServiceProvider::class);
2- For using the facade you have to uncomment the line $app->withFacades();
in bootstrap/app.php
$app->withFacades();
After uncommenting this line you can create the GraphQL
alias
class_alias(\Folklore\GraphQL\Support\Facades\GraphQL::class, 'GraphQL');
3- Publish the configuration file
$ php artisan graphql:publish
4- Load configuration file in bootstrap/app.php
Important: this command needs to be executed before the registration of the service provider
$app->configure('graphql');
...
$app->register(Folklore\GraphQL\LumenServiceProvider::class)
5- Review the configuration file
config/graphql.php
1- First create a type:
php artisan make:graphql:type UserType
This command will create a Type Class UserType.php
in the app/GraphQL/Type
folder. You can review the file and add fields.
class UserType extends BaseType
{
//...
protected function fields()
{
return [
'id' => [
'type' => Type::id(),
'description' => 'The user id'
],
'email' => [
'type' => Type::string(),
'description' => 'The user email'
]
]
}
//...
}
Then you need to add the Type to the config in config/graphql.php
, like so:
<?php
return [
//...
'types' => [
\App\GraphQL\Type\UserType::class
]
//...
];
Or use the facade in a service provider, like this:
class AppServiceProvider extends ServiceProvider
{
//...
public function boot()
{
GraphQL::addType(\App\GraphQL\Type\UserType::class);
}
//...
}
2- Then create a query:
php artisan make:graphql:query UserQuery
Review the file and add arguments, the type returned by the query and fill the resolve method
class UserType extends BaseType
{
//...
protected function args()
{
return [
'id' => [
'name' => 'id',
'type' => Type::nonNull(Type::id()),
'description' => 'The user id'
]
]
}
protected function type()
{
//This is the type we've created the step before
return GraphQL::type('User');
}
public function resolve($root, $args, $context, ResolveInfo $info)
{
//Take the arguments and get a user from an eloquent model
$user = User::find($args['id']);
return $user
}
}
The you need to add the query to the default schema in the config config/graphql.php
.
<?php
return [
//...
'schemas' => [
'default' => [
'query' => [
'user' => \App\GraphQL\Query\UserQuery::class
]
//...
]
]
//...
];
or using the facade:
class AppServiceProvider extends ServiceProvider
{
//...
public function boot()
{
GraphQL::addSchema('default', [
'query' => [
'user' => \App\GraphQL\Query\UserQuery::class
]
]);
}
//...
}
3- Query your schema
You can then query your schema by sending a GET request to /graphql
with the following input:
query
query GetUser($id: ID!)
{
user (id: $id)
{
id
email
}
}
variables
{
"id": "1"
}
Full url:
http://homestead.dev/graphql?variables={"id":"1"}&query=query GetUser($id: ID!) { user (id: $id) { id email } }