0

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

我的 graphql 返回 ID、tenant_reference 和角色,但我似乎无法添加自定义属性,它是一个带有自定义键的对象,它只是返回 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返回的内容

在此处输入图像描述

4

1 回答 1

0

在 graphql 上使用此查询

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

}

于 2019-01-25T11:59:10.080 回答