【问题标题】:Graphql Query Objects in LaravelLaravel 中的 Graphql 查询对象
【发布时间】:2019-01-26 09:28:53
【问题描述】:

所以我使用 GraphQL 缩小了基础范围。在我的 laravel APP 中

我的 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"
    }
}

这里是sn-p类型:

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()),
        ]
    ];
}

这里是查询 sn-p:

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返回的内容

【问题讨论】:

    标签: php graphql laravel-5.5 graphql-php


    【解决方案1】:

    在 graphql 上使用这个查询

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

    }

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 2016-10-24
      • 2019-07-29
      • 2018-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-09
      • 2019-06-13
      • 2019-07-14
      相关资源
      最近更新 更多