使用Laravel创建一个GraphQl接口
创建一个Laravel项目
这里以Laravel 5.7举例
创建一个Laravel项目
//5.7是版本号,可以更换成别的版本,project_name是你创建项目的名称
composer create-project laravel/laravel=5.7.* --prefer-dist laravel-graphql
安装Graphql
composer require folklore/graphql
创建几个表用来测试
发布
去config里app.php中添加
Folklore\GraphQL\ServiceProvider::class,
'GraphQL' => Folklore\GraphQL\Support\Facades\GraphQL::class,
php artisan vendor:publish --provider="Folklore\GraphQL\ServiceProvider"
修改数据库连接
修改,env的数据库连接
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=tomorrownews
DB_USERNAME=root
DB_PASSWORD=
创建一个Model
php artisan make:model Models/News
修改model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class News extends Model
{
/**
* 关联到模型的数据表
* @var string
*/
protected $table = 'news';
/**
* Laravel有默认时间字段,如果不需要则去除
* 表明模型是否应该被打上时间戳
* @var bool
*/
public $timestamps = false;
}
创建一个Type
php artisan make:graphql:type NewsType
修改
<?php
namespace App\GraphQL\Type;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Type as BaseType;
class NewsType extends BaseType
{
protected $attributes = [
'name' => 'NewsType',
'description' => 'A type'
];
public function fields()
{
return [
'id' => [
'type' => Type::nonNull(Type::int()),
'description' => 'The id of the new'
],
'title' => [
'type' => Type::string(),
'description' => 'The title of the new'
],
'content' => [
'type' => Type::string(),
'description' => 'The content of the new'
],
'author' => [
'type' => Type::string(),
'description' => 'The author of the new'
]
];
}
}
去config/graphql.php修改
'types' => [
'News' => \App\GraphQL\Type\NewsType::class,
],
创建用于查询的Query
php artisan make:graphql:query NewsQuery
修改
<?php
namespace App\GraphQL\Query;
use App\Models\News;
use Folklore\GraphQL\Support\Query;
use GraphQL;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
class NewsQuery extends Query
{
protected $attributes = [
'name' => 'NewsQuery',
'description' => 'A query'
];
public function type()
{
return Type::listOf(GraphQL::type('News'));
}
public function args()
{
return [
'id' => ['name' => 'id', 'type' => Type::int()],
'first' => ['name' => 'first', 'type' => Type::int()],
'title' => ['name' => 'title', 'type' => Type::string()],
'content' => ['name' => 'content', 'type' => Type::string()],
'author' => ['name' => 'author', 'type' => Type::string()],
];
}
public function resolve($root, $args, $context, ResolveInfo $info)
{
if (isset($args['id'])) {
return News::where('id', $args['id'])->get();
} elseif (isset($args['title'])) {
return News::where('title', $args['title'])->get();
}elseif (isset($args['content'])) {
return News::where('content', $args['content'])->get();
}elseif (isset($args['author'])) {
return News::where('author', $args['author'])->get();
}elseif (isset($args['first'])) {
return News::limit($args['first'])->latest('id')->get();
} else {
return News::all();
}
}
去config/graphql.php修改
'schemas' => [
'default' => [
'query' => [
'news' => \App\GraphQL\Query\NewsQuery::class
],
'mutation' => [
]
]
],
简单查询
1.查询所有的数据
http://localhost/laravel-graphql/public/graphql?query=query+FecthNews{news{id,content}}
2.根据id为5的数据
http://localhost/laravel-graphql/public/graphql?query=query+FecthNews{news(id:5){id,content}}
3.根据按照id排序的前五条的数据
http://localhost/laravel-graphql/public/graphql?query=query+FecthNews{news(first:5){id,content}}
4.从第二条开始查询几条数据
http://localhost/laravel-graphql/public/graphql?query=query+FecthNews{news(first:5,offset:2){id}}
创建用于数据库操作的
php artisan make:graphql:mutation UpdateDeviceMutation
修改
<?php
namespace App\GraphQL\Mutation;
use App\Models\News;
use Folklore\GraphQL\Support\Mutation;
use GraphQL;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
class UpdateDeviceMutation extends Mutation
{
protected $attributes = [
'name' => 'UpdateDeviceMutation',
'description' => 'A mutation'
];
public function type()
{
return GraphQL::type('News');
}
public function args()
{
return [
'id' => ['name' => 'id', 'type' => Type::int()],
'title' => ['name' => 'title', 'type' => Type::string()],
'content' => ['name' => 'content', 'type' => Type::string()],
'author' => ['name' => 'author', 'type' => Type::string()],
];
}
public function resolve($root, $args, $context, ResolveInfo $info)
{
$device = News::find($args['id']);
if (!$device) {
return null;
}
if (!empty($args['content'])) {
$device['content'] = $args['content'];
}
$device->save();
return $device;
}
}
在graphql.php中添加
'mutation' => [
'updatenews' => \App\GraphQL\Mutation\UpdateDeviceMutation::class,
]
请求
或者直接请求
http://localhost/laravel-graphql/public/graphql?query=mutation+updatenew{updatenews(id:20,content:"全都是空白"){id,content}}