다음을 통해 공유


데이터 API 작성기에서 엔터티 관계

엔터티 관계를 사용하면 GraphQL 쿼리가 관련 엔터티를 트래버스하여 단일 쿼리로 복잡한 데이터 셰이프를 사용할 수 있습니다. 다음은 그 예입니다.

{
  books {
    items {
      id
      title
      authors {
        items {
          first_name
          last_name
        }
      }
    }
  }
}

이 동작을 달성하려면 구성 파일의 섹션을 통해 DAB에게 엔터티가 어떻게 관련되어 있는지 알려야 합니다.

Configuration

엔터티 간의 관계를 정의하려면 다음을 수행합니다.

  • relationships 엔터티 구성 내에서 객체를 사용합니다.
  • 이름을 입력합니다 target.entity .
  • cardinality"one" 또는 "many"로 설정하십시오.
  • 필요에 따라 지정 source.fieldstarget.fields.
  • linking.object 조인 테이블을 노출하지 않고 다 대 다 관계를 모델링할 때 사용하세요.

CLI 예제

dab update Book \
  --relationship authors \
  --target.entity Author \
  --cardinality many \
  --relationship.fields "id:id" \
  --linking.object "dbo.books_authors" \
  --linking.source.fields "book_id" \
  --linking.target.fields "author_id"

구성 예제

"Book": {
  "source": "dbo.books",
  "relationships": {
    "authors": {
      "cardinality": "many",
      "target.entity": "Author",
      "source.fields": [ "id" ],
      "target.fields": [ "id" ],
      "linking.object": "dbo.books_authors",
      "linking.source.fields": [ "book_id" ],
      "linking.target.fields": [ "author_id" ]
    }
  }
}

일대다

  • 카디널리티를 사용합니다."many"
  • 예: Series에는 여러 가지의 Books가 있습니다.
  • 외래 키가 있는 경우 DAB에서 필드를 유추할 수 있습니다.
dab update Series \
  --relationship books \
  --target.entity Book \
  --cardinality many

다대일

  • 카디널리티를 사용합니다."one"
  • 예: A Book 는 1 Series에 속합니다.
dab update Book \
  --relationship series \
  --target.entity Series \
  --cardinality one

다대다(링킹 개체)

  • GraphQL에 노출되지 않는 조인 테이블을 사용합니다.
  • 조인 테이블을 통해 원본에서 대상으로 연결 필드를 정의합니다.
dab update Author \
  --relationship books \
  --target.entity Book \
  --cardinality many \
  --relationship.fields "id:id" \
  --linking.object "dbo.books_authors" \
  --linking.source.fields "author_id" \
  --linking.target.fields "book_id"

Many-to-Many(다대다 명시적 조인 엔티티)

  • 조인 테이블을 GraphQL 개체로 노출합니다.
  • 세 엔터티 모두에 대한 관계를 정의합니다.
dab add BookAuthor \
  --source dbo.books_authors \
  --permissions "anonymous:*"

dab update BookAuthor \
  --relationship book \
  --target.entity Book \
  --cardinality one \
  --relationship.fields "book_id:id"

dab update BookAuthor \
  --relationship author \
  --target.entity Author \
  --cardinality one \
  --relationship.fields "author_id:id"

상호 관계

양방향 탐색을 허용하려면(예: Book에서 Author로, 또는 Author에서 Book로), 대상 엔터티에서 원본 및 대상 필드를 반대로 하는 두 번째 관계를 정의합니다.

예시

dab update Author \
  --relationship books \
  --target.entity Book \
  --cardinality many \
  --relationship.fields "id:id" \
  --linking.object "dbo.books_authors" \
  --linking.source.fields "author_id" \
  --linking.target.fields "book_id"

이 구성은 BookAuthor 관계와 짝을 이루며, GraphQL에서 대칭 순회를 가능하게 합니다.

{
  authors {
    items {
      first_name
      books {
        items {
          title
        }
      }
    }
  }
}

GraphQL 지원

  • 관련 필드는 중첩된 개체로 표시됩니다.
  • 카디널리티는 목록 또는 단일 개체가 반환되는지 여부를 결정합니다.
  • GraphQL 형식 이름 및 필드는 구성 이름과 일치합니다.

제한점

  • 관계를 정의하려면 엔터티가 동일한 구성 파일에 존재해야 합니다.
  • 단일 홉 네비게이션만 지원됩니다.
  • 순환 및 심층 중첩은 최적화되지 않습니다.
  • REST는 관계를 지원하지 않습니다(GraphQL만 해당).