如何在不编写长查询的情况下查询所有 GraphQL 类型字段?

2022-01-04 00:00:00 graphql php laravel graphql-php

假设您有一个 GraphQL 类型并且它包含许多字段.如何在不写下包含所有字段名称的长查询的情况下查询所有字段?

Assume you have a GraphQL type and it includes many fields. How to query all the fields without writing down a long query that includes the names of all the fields?

例如,如果我有这些字段:

For example, If I have these fields :

 public function fields()
    {
        return [
            'id' => [
                'type' => Type::nonNull(Type::string()),
                'description' => 'The id of the user'
            ],
            'username' => [
                'type' => Type::string(),
                'description' => 'The email of user'
            ], 
             'count' => [
                'type' => Type::int(),
                'description' => 'login count for the user'
            ]

        ];
    }

要查询所有字段,通常查询是这样的:

To query all the fields usually the query is something like this:

FetchUsers{users(id:"2"){id,username,count}}

但我想要一种无需编写所有字段即可获得相同结果的方法,如下所示:

But I want a way to have the same results without writing all the fields, something like this:

FetchUsers{users(id:"2"){*}}
//or
FetchUsers{users(id:"2")}

有没有办法在 GraphQL 中做到这一点??

Is there a way to do this in GraphQL ??

我正在使用 Folkloreatelier/laravel-graphql 库.

推荐答案

不幸的是,您想要做的事情是不可能的.GraphQL 要求您明确指定您希望从查询中返回哪些字段.

Unfortunately what you'd like to do is not possible. GraphQL requires you to be explicit about specifying which fields you would like returned from your query.

相关文章