PHP Graphql创建片段

时间:2018-10-10 21:00:44

标签: php graphql

我有这个查询。

query{
    paintInputsUser{
        username,country, views, rol
    }
}

但是我需要一种将这些值转换为唯一字段的方法,在google中搜索时,我发现最好的方法是使用片段。我想要这个:

query{
    ...paintInputsUser
}

但是我找不到如何创建片段以及如何将其添加到架构中。

这是我的paintInputsUser类型

<?php

namespace App\GraphQL\Type;
use App\GraphQL\Support\Type;
use GraphQL;
use DB;
use GraphQL\Type\Definition\ObjectType;
use App\Model\Win\Users;

class PaintInputUser extends ObjectType
{

    public function __construct(){
        $config = [
            'name' => 'PaintInputUser',
            'description' => 'Help to know which fields the creation and edition of user are available, and the respective data',
            'fields' => function() {
                return [
                    'username' => [
                        'type' => Type::nonNull(Type::string()),
                    ],
                    'country' => [
                        'type' => Type::listOf(Type::string()),
                        'resolve' => function($person) {
                            $countries = DB::select('select c.country from win_country c inner join win_user_country uc on uc.id_country = c.id_country inner join win_users u on u.id_user = uc.id_user where u.username = ?',[$person->username]);
                            $array = array();
                            foreach($countries as $country){
                                array_push($array,$country->country);
                            }
                            return $array;
                        }
                    ],
                    'cards' => [
                        'type' => Type::listOf(Type::string()),
                        'resolve' => function($person) {
                            $cards = DB::select('select c.card from win_cards c
                            inner join win_user_card_granted ucg on ucg.id_card = c.id_card
                            inner join win_users u on u.id_user = ucg.id_user
                            where u.username = ?',[$person->username]);

                            $array = array();
                            foreach($cards as $card){
                                array_push($array,$card->card);
                            }
                            return $array;

                        }
                    ],
                    'views' => [
                        'type' => Type::listOf(Type::string()),
                        'resolve' => function($person) {
                            $views = DB::select('select vp.view_name from win_views_principal vp
                            inner join win_user_view_granted uvg on uvg.id_view = vp.id_view_principal
                            inner join win_users u on u.id_user = uvg.id_user
                            where u.username = ?',[$person->username]);

                            $array = array();
                            foreach($views as $view){
                                array_push($array,$view->view_name);
                            }
                            return $array;
                        }
                    ],
                    'rol' => [
                        'type' => Type::listOf(Type::string()),
                        'resolve' => function($person) {
                            $roles = DB::select('select r.rolename from win_roles r
                            inner join win_user_role ur on ur.id_role = r.id_role
                            inner join win_users u on u.id_user = ur.id_user
                            where u.username = ?',[$person->username]);

                            $array = array();
                            foreach($roles as $rol){
                                array_push($array,$rol->rolename);
                            }
                            return $array;
                        }
                    ],
                ];
            }
        ];
        parent::__construct($config);
    }
}

请有人向我解释如何实现包含paintInputsUser的片段。

1 个答案:

答案 0 :(得分:2)

片段在客户端而非服务器端使用。根据规格:

  

片段允许重复使用常见的字段选择,从而减少文档中的重复文本...片段通过使用传播运算符(...)消耗。片段选择的所有字段将被添加到与片段调用相同级别的查询字段选择中。这是通过片段传播的多个级别发生的。

因此,客户端请求可能包含这样的查询:

query {
    paintInputsUser{
        ...paintInputUserFields
    }
}

fragment paintInputUserFields on PaintInputUser {
  username
  country
  views
  rol
}

在服务器端启用此行为不需要做任何特别的事情。 GraphQL的所有符合规范的实现均支持现成的片段。