Laravel中的Graphql查询对象

时间:2018-08-20 09:15:18

标签: php graphql laravel-5.5 graphql-php

因此,我使用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"
    }
}

在此处输入代码段:

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的结果

enter image description here

1 个答案:

答案 0 :(得分:0)

在graphql上使用此查询

@RunWith(SpringRunner.class)
@SpringBootTest
@EmbeddedKafka(partitions = 3, controlledShutdown = false, count = 1, topics = {"zc.deviceposition"}, brokerProperties = {"listeners=PLAINTEXT://localhost:9092", "port=9092", "log.dir=/home/name/logs"})
@EmbeddedCassandra(timeout = 60000)
@CassandraDataSet(value = {"bootstrap_test.cql"}, keyspace = "statistics")
@ActiveProfiles("test")
@DirtiesContext
public class CassandraTripsAggregatorProcessorSupplierIntegrationTest {
  @Test
  public void someTest() {System.out.println("hello world");}
}

}