쿼리 라이브러리
쿼리 라이브러리외부 API의 데이터 보강하기

외부 API의 데이터 보강하기

외부 API에서 데이터를 가져와야 하지만, 결과를 어떤 식으로든 수정해야 하는 경우(예: 특정 필드가 비어 있을 때 기본값 제공), Gato GraphQL을 사용하여 API 게이트웨이를 구현하고 항목을 필요에 따라 변환할 수 있습니다.

예를 들어, 어떤 WordPress 사이트의 REST API 엔드포인트 /users를 호출할 때, url 필드가 비어 있으면 기본값을 추가하고, HTML 코드가 포함된 추가 link 속성을 붙일 수 있습니다:

query FilterDataFromWordPressAPI(
  # eg: https://somesite.com/wp-json/wp/v2/users/?_fields=id,name,url
  $endpointURL: URL!
) {
  usersWithLinkAndDefaultURL: _sendJSONObjectCollectionHTTPRequest(
    input: {
      url: $endpointURL
    }
  )
    # Set a default URL for users without any
    @underEachArrayItem
      @underJSONObjectProperty(
        by: {
          key: "url"
        }
      )
        @default(
          value: "https://mysite.com"
          condition: IS_EMPTY
        )
 
    # Add a new "link" entry on the JSON object
    @underEachArrayItem(
      affectDirectivesUnderPos: [1, 2, 3, 4],
      passValueOnwardsAs: "userListItem"
    )
      @applyField(
        name: "_objectProperty",
        arguments: {
          object: $userListItem,
          by: {
            key: "name"
          }
        },
        passOnwardsAs: "userName"
      )
      @applyField(
        name: "_objectProperty",
        arguments: {
          object: $userListItem,
          by: {
            key: "url"
          }
        },
        passOnwardsAs: "userURL"
      )
      @applyField(
        name: "_sprintf",
        arguments: {
          string: "<a href=\"%s\">%s</a>",
          values: [$userURL, $userName]
        },
        passOnwardsAs: "userLink"
      )
      @applyField(
        name: "_objectAddEntry",
        arguments: {
          object: $userListItem,
          key: "link",
          value: $userLink
        },
        setResultInResponse: true
      )
}