首页 文章

PHP Graphql创建片段

提问于
浏览
0

我有这个问题 .

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

但我需要一种方法将这些值转换为一个独特的字段,在谷歌搜索我发现最好的方法是使用片段 . 我要这个:

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 回答

  • 2

    片段用于客户端,而不是服务器端 . 根据规格:

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

    所以客户端请求可能包含这样的查询:

    query {
        paintInputsUser{
            ...paintInputUserFields
        }
    }
    
    fragment paintInputUserFields on PaintInputUser {
      username
      country
      views
      rol
    }
    

    您需要在服务器端执行此行为 . GraphQL的所有符合规范的实现都支持开箱即用的片段 .

相关问题