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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章