Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.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 Laravel中的Graphql查询对象_Php_Graphql_Laravel 5.5_Graphql Php - Fatal编程技术网

Php Laravel中的Graphql查询对象

Php Laravel中的Graphql查询对象,php,graphql,laravel-5.5,graphql-php,Php,Graphql,Laravel 5.5,Graphql Php,因此,我使用GraphQL缩小了基础知识的范围。在我的laravel应用程序中 我的graphql返回ID、tenant_引用和角色,但我似乎无法添加自定义属性。它是一个具有自定义键的对象,它只返回null,或者如果我尝试添加键,它会出错 以下是数据的JSON表示示例: "data": { "vendor_id": "b8faaf26-c8a6-3560-8357-f789f3325b0c", "roles": [ "manager" ], "te

因此,我使用GraphQL缩小了基础知识的范围。在我的laravel应用程序中

我的graphql返回ID、tenant_引用和角色,但我似乎无法添加自定义属性。它是一个具有自定义键的对象,它只返回null,或者如果我尝试添加键,它会出错

以下是数据的JSON表示示例:

"data": {
    "vendor_id": "b8faaf26-c8a6-3560-8357-f789f3325b0c",
    "roles": [
        "manager"
    ],
    "tenant_reference": "lightcoral70",
    "custom_attributes": {
        "fred": "test",
        "one": "test2"
    }
}
下面是类型代码段:

public function fields() {
    return [
        'id' => [
            'type' => GraphQlType::nonNull(GraphQlType::string()),
            'description' => 'The id of the user',
            'alias' => 'user_id', // Use 'alias', if the database column is different from the type name
        ],
        'tenant_reference' => [
            'type' => GraphQlType::nonNull(GraphQlType::string()),
            'description' => 'Tenant reference this user belongs to',
            'alias' => 'tenant'
        ],
        'roles' => [
            'type' => GraphQlType::listOf(GraphQlType::string()),
            'description' => 'User roles'
        ],
        'custom_attributes' => [
            'type' => GraphQlType::listOf(GraphQlType::string()),
        ]
    ];
}
以下是查询片段:

protected $repository;

protected $attributes = [
    'name' => 'Users query'
];

public function __construct(UserRepository $repository) {
    $this->repository = $repository;
}

/**
 * @return \GraphQL\Type\Definition\ListOfType|null
 */
public function type() {
    return GraphQlType::listOf(GraphQL::type('user_record'));
}

/**
 * Arguments we can use to filter the query
 * @return array
 */
public function args() {

    return [
        'sub' => [
            'name' => 'id',
            'type' => GraphQlType::string()
        ],
        'tenantReference' => [
            'name' => 'tenantReference',
            'type' => GraphQlType::string()
        ],
    ];
}

/**
 */
public function resolve($root, $args) {
    return User::where([
        "tenant_reference" => $args['tenantReference'],
        "sub" => $args['id']
    ])->get();
}
这基本上就是我希望使用graphiql仿真器实现的目标:

请记住,自定义属性可以是任何东西。我可以在twitter上动态添加另一个键:urlhere

{
    user_record(
        id:"b8faaf26-c8a6-3560-8357-f789f3325b0c",
        tenantReference:"lightcoral70"
    ) 
    {
       id,
       tenant_reference,
       roles,
       custom_attributes
          fred
    }
}
这就是使用graphiql所得到的结果


在graphql上使用此查询

{
user_record(
    id:"b8faaf26-c8a6-3560-8357-f789f3325b0c",
    tenantReference:"lightcoral70"
) 
{
   id,
   tenant_reference,
   roles,
   custom_attributes{
      fred
    }
}
}