Laravel GraphQL之Query

2018年6月11日17:57:02 实习第七周 XXM

Begin

首先我们通过artisan命令 快速创建一个Type文件

php artisan make:graphql:type UserType

UserType.php

<?php

namespace App\GraphQL\Type;

use GraphQL\Type\Definition\Type; use Folklore\GraphQL\Support\Type as BaseType; use GraphQL;

class UserType extends BaseType { protected $attributes = [ 'name' => 'UserType', 'description' => 'A type' ];

<span class="hljs-keyword" style="font-weight: bold;">public</span> <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-title" style="color: rgb(153, 0, 0); font-weight: bold;">fields</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword" style="font-weight: bold;">return</span> [
        <span class="hljs-string" style="color: rgb(221, 17, 68);">'id'</span>=&gt;[
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'type'</span>=&gt;Type::nonNull(Type::int()),
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'description'</span>=&gt;<span class="hljs-string" style="color: rgb(221, 17, 68);">'The id of User'</span>
        ],
        <span class="hljs-string" style="color: rgb(221, 17, 68);">'userAccount'</span>=&gt;[
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'type'</span>=&gt;Type::string(),
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'description'</span>=&gt;<span class="hljs-string" style="color: rgb(221, 17, 68);">'The userAccount of User'</span>
        ],
        <span class="hljs-string" style="color: rgb(221, 17, 68);">'userPass'</span>=&gt;[
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'type'</span>=&gt;Type::string(),
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'description'</span>=&gt;<span class="hljs-string" style="color: rgb(221, 17, 68);">'The userPass of User'</span>
        ],
        <span class="hljs-string" style="color: rgb(221, 17, 68);">'created_at'</span>=&gt;[
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'type'</span>=&gt;Type::string(),
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'description'</span>=&gt;<span class="hljs-string" style="color: rgb(221, 17, 68);">'The created_at of User'</span>
        ],
        <span class="hljs-string" style="color: rgb(221, 17, 68);">'updated_at'</span>=&gt;[
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'type'</span>=&gt;Type::string(),
            <span class="hljs-string" style="color: rgb(221, 17, 68);">'description'</span>=&gt;<span class="hljs-string" style="color: rgb(221, 17, 68);">'The description of User'</span>
        ]
    ];
}

}

然后在 config/graphql.php 中注册该类型:

'types' => [
    'User'=>'\App\GraphQL\Type\UserType',
],

定义好Type后,通过Artisan命令创建一个query

php artisan make:graphql:query UsersQuery

UsersQuery.php

<?php

namespace App\GraphQL\Query;

use Folklore\GraphQL\Support\Query; use GraphQL\Type\Definition\ResolveInfo; use GraphQL\Type\Definition\Type; use GraphQL; use App\User;

class UsersQuery extends Query { protected $attributes = [ 'name' => 'UserQuery', 'description' => 'A query' ];

<span class="hljs-keyword" style="font-weight: bold;">public</span> <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-title" style="color: rgb(153, 0, 0); font-weight: bold;">type</span><span class="hljs-params">()</span> 
</span>{
    <span class="hljs-keyword" style="font-weight: bold;">return</span> Type::listOf(GraphQL::type(<span class="hljs-string" style="color: rgb(221, 17, 68);">'User'</span>));
}

<span class="hljs-keyword" style="font-weight: bold;">public</span> <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-title" style="color: rgb(153, 0, 0); font-weight: bold;">args</span><span class="hljs-params">()</span>
</span>{
    <span class="hljs-keyword" style="font-weight: bold;">return</span> [
        <span class="hljs-string" style="color: rgb(221, 17, 68);">'id'</span>=&gt;[<span class="hljs-string" style="color: rgb(221, 17, 68);">'name'</span>=&gt;<span class="hljs-string" style="color: rgb(221, 17, 68);">'id'</span>,<span class="hljs-string" style="color: rgb(221, 17, 68);">'type'</span>=&gt;Type::string()],
        <span class="hljs-string" style="color: rgb(221, 17, 68);">'userAccount'</span>=&gt;[<span class="hljs-string" style="color: rgb(221, 17, 68);">'name'</span>=&gt;<span class="hljs-string" style="color: rgb(221, 17, 68);">'userAccount'</span>,<span class="hljs-string" style="color: rgb(221, 17, 68);">'type'</span>=&gt;Type::string()],
        <span class="hljs-string" style="color: rgb(221, 17, 68);">'userPass'</span>=&gt;[<span class="hljs-string" style="color: rgb(221, 17, 68);">'name'</span>=&gt;<span class="hljs-string" style="color: rgb(221, 17, 68);">'userPass'</span>,<span class="hljs-string" style="color: rgb(221, 17, 68);">'type'</span>=&gt;Type::string()],
        <span class="hljs-string" style="color: rgb(221, 17, 68);">'limit'</span>=&gt;[<span class="hljs-string" style="color: rgb(221, 17, 68);">'name'</span>=&gt;<span class="hljs-string" style="color: rgb(221, 17, 68);">'limit'</span>,<span class="hljs-string" style="color: rgb(221, 17, 68);">'type'</span>=&gt;Type::int()]
    ];
}

<span class="hljs-keyword" style="font-weight: bold;">public</span> <span class="hljs-function"><span class="hljs-keyword" style="font-weight: bold;">function</span> <span class="hljs-title" style="color: rgb(153, 0, 0); font-weight: bold;">resolve</span><span class="hljs-params">($root, $args, $context, ResolveInfo $info)</span>
</span>{

    $qb = User::orderBy(<span class="hljs-string" style="color: rgb(221, 17, 68);">'id'</span>, <span class="hljs-string" style="color: rgb(221, 17, 68);">'desc'</span>);

    <span class="hljs-keyword" style="font-weight: bold;">if</span>(<span class="hljs-keyword" style="font-weight: bold;">isset</span>($args[<span class="hljs-string" style="color: rgb(221, 17, 68);">'limit'</span>]))
    {
        $qb-&gt;limit($args[<span class="hljs-string" style="color: rgb(221, 17, 68);">'limit'</span>])-&gt;latest(<span class="hljs-string" style="color: rgb(221, 17, 68);">'id'</span>);
    }

    <span class="hljs-keyword" style="font-weight: bold;">if</span>(<span class="hljs-keyword" style="font-weight: bold;">isset</span>($args[<span class="hljs-string" style="color: rgb(221, 17, 68);">'id'</span>]))
    {
        $qb-&gt;where(<span class="hljs-string" style="color: rgb(221, 17, 68);">'id'</span>,$args[<span class="hljs-string" style="color: rgb(221, 17, 68);">'id'</span>]);
    }

    <span class="hljs-keyword" style="font-weight: bold;">if</span>(<span class="hljs-keyword" style="font-weight: bold;">isset</span>($args[<span class="hljs-string" style="color: rgb(221, 17, 68);">'userAccount'</span>]))
    {
        $qb-&gt;where(<span class="hljs-string" style="color: rgb(221, 17, 68);">'userAccount'</span>,$args[<span class="hljs-string" style="color: rgb(221, 17, 68);">'userAccount'</span>]);
    }

    <span class="hljs-keyword" style="font-weight: bold;">return</span> $qb-&gt;get();
}

}

然后配置 config/graphql.php 新增查询结构:

'schemas' => [
        'default' => [
            'query' => [
                'users'=>'\App\GraphQL\Query\UsersQuery',
            ],
            'mutation' => [

        ]
    ]
],</code></pre><p>以上就成功实现了一个简单的Query</p><p><img alt="QQ截图20180611175350.png" src="/storage/image/653685.jpg" width="950" height="466"><br></p><h2>End</h2>

日记本

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!

赞赏支持
被以下专题收入,发现更多相似内容