使用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}}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值