Programing

긴 쿼리를 작성하지 않고 모든 GraphQL 유형 필드를 쿼리하는 방법은 무엇입니까?

lottogame 2020. 9. 9. 18:55
반응형

긴 쿼리를 작성하지 않고 모든 GraphQL 유형 필드를 쿼리하는 방법은 무엇입니까?


GraphQL 유형이 있고 많은 필드를 포함한다고 가정합니다. 모든 필드의 이름을 포함하는 긴 쿼리를 작성하지 않고 모든 필드를 쿼리하는 방법은 무엇입니까?

예를 들어, 다음 필드가있는 경우 :

 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'
            ]

        ];
    }

모든 필드를 쿼리하려면 일반적으로 쿼리는 다음과 같습니다.

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

그러나 다음과 같이 모든 필드를 작성하지 않고도 동일한 결과를 얻을 수있는 방법을 원합니다.

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

GraphQL에서 이것을 수행하는 방법이 있습니까?

내가 사용하고 Folkloreatelier / laravel-graphql 라이브러리를.


불행히도 당신이 원하는 것은 불가능합니다. GraphQL에서는 쿼리에서 반환 할 필드를 명시 적으로 지정해야합니다.


예, introspection을 사용하여이를 수행 수 있습니다 . GraphQL 쿼리를 ( UserType 유형에 대해 )

{
   __type(name:"UserType") {
      fields {
         name
         description
      }  
   }
}

다음과 같은 응답을 받게됩니다 (실제 필드 이름은 실제 스키마 / 유형 정의에 따라 다름).

{
  "data": {
    "__type": {
      "fields": [
        {
          "name": "id",
          "description": ""
        },
        {
          "name": "username",
          "description": "Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only."
        },
        {
          "name": "firstName",
          "description": ""
        },
        {
          "name": "lastName",
          "description": ""
        },
        {
         "name": "email",
          "description": ""
        },
        ( etc. etc. ...)
      ]
    }
  }
}

그런 다음 클라이언트에서이 필드 목록을 읽고 두 번째 GraphQL 쿼리를 동적으로 빌드하여 이러한 모든 필드를 가져올 수 있습니다.

이는 필드를 가져 오려는 유형의 이름을 아는 것에 달려 있습니다. 유형을 모르는 경우 다음과 같은 인트로 스펙 션을 사용하여 모든 유형과 필드를 함께 가져올 수 있습니다.

{
  __schema {
    types {
      name
      fields {
        name
        description
      }
    }
  }
}

NOTE: this is the over-the-wire GraphQL data -- you're on your own to figure out how to read and write with your actual client. Your graphQL javascript library may already employ introspection in some capacity, for example the apollo codegen command uses introspection to generate types.


I guess the only way to do this is by utilizing reusable fragments:

fragment UserFragment on Users {
    id
    username
    count
} 

FetchUsers {
    users(id: "2") {
        ...UserFragment
    }
}

I faced this same issue when I needed to load location data that I had serialized into the database from the google places API. Generally I would want the whole thing so it works with maps but I didn't want to have to specify all of the fields every time.

I was working in Ruby so I can't give you the PHP implementation but the principle should be the same.

I defined a custom scalar type called JSON which just returns a literal JSON object.

The ruby implementation was like so (using graphql-ruby)

module Graph
  module Types
    JsonType = GraphQL::ScalarType.define do
      name "JSON"
      coerce_input -> (x) { x }
      coerce_result -> (x) { x }
    end
  end
end

Then I used it for our objects like so

field :location, Types::JsonType

I would use this very sparingly though, using it only where you know you always need the whole JSON object (as I did in my case). Otherwise it is defeating the object of GraphQL more generally speaking.

참고URL : https://stackoverflow.com/questions/34199982/how-to-query-all-the-graphql-type-fields-without-writing-a-long-query

반응형