Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP Graphql创建片段_Php_Graphql - Fatal编程技术网

PHP Graphql创建片段

PHP Graphql创建片段,php,graphql,Php,Graphql,我有这个疑问 query{ paintInputsUser{ username,country, views, rol } } 然而,我需要一种方法将这些值转换成一个唯一的字段,在谷歌搜索时,我发现最好的方法是使用片段。我想要这个: query{ ...paintInputsUser } 但是我找不到如何创建片段以及如何将其添加到模式中 这是我的绘画输入型 <?php namespace App\GraphQL\Type; use App\Gr

我有这个疑问

query{
    paintInputsUser{
        username,country, views, rol
    }
}
然而,我需要一种方法将这些值转换成一个唯一的字段,在谷歌搜索时,我发现最好的方法是使用片段。我想要这个:

query{
    ...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);
    }
}

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

片段允许重复使用常见的重复选择字段,减少文档中的重复文本。。。使用扩展运算符(…)消耗片段。片段选择的所有字段都将添加到与片段调用相同级别的查询字段选择中。这是通过多个级别的碎片扩散实现的

因此,客户端请求可能包括如下查询:

query {
    paintInputsUser{
        ...paintInputUserFields
    }
}

fragment paintInputUserFields on PaintInputUser {
  username
  country
  views
  rol
}

您无需在服务器端执行任何特殊操作即可启用此行为。所有符合规范的GraphQL实现都支持现成的片段。

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

片段允许重复使用常见的重复选择字段,减少文档中的重复文本。。。使用扩展运算符(…)消耗片段。片段选择的所有字段都将添加到与片段调用相同级别的查询字段选择中。这是通过多个级别的碎片扩散实现的

因此,客户端请求可能包括如下查询:

query {
    paintInputsUser{
        ...paintInputUserFields
    }
}

fragment paintInputUserFields on PaintInputUser {
  username
  country
  views
  rol
}

您无需在服务器端执行任何特殊操作即可启用此行为。所有符合规范的GraphQL实现都支持开箱即用的片段。

oooh我明白了,这是一个缓解,很好的解释,谢谢:)oooh我明白了,这是一个缓解,很好的解释,谢谢:)