graphql federation - external directive

아엘·2024년 3월 8일
0

graphql

목록 보기
1/1

3개의 subgraph가 있다고 가정합니다.

type Query {
  me: User
}

type User @key(fields: "id") {
  id: ID!
  username: String! @shareable
}

# (Subgraph schemas include
# this to opt in to
# Federation 2 features.)
extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.3",
        import: ["@key", "@shareable"])
type Query {
  topProducts(first: Int = 5): [Product]
}

type Product @key(fields: "upc") {
  upc: String!
  name: String!
  price: Int
}

extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.3",
        import: ["@key", "@shareable"])
type Review {
  body: String
  author: User @provides(fields: "username")
  product: Product
}

type User @key(fields: "id") {
  id: ID!
  username: String! @external
  reviews: [Review]
}

type Product @key(fields: "upc") {
  upc: String!
  reviews: [Review]
}

# (This subgraph uses additional
# federated directives)
extend schema
  @link(url: "https://specs.apollo.dev/federation/v2.3",
        import: ["@key", "@shareable", "@provides", "@external"])

external에 대한 이런 질문이 있을 수 있습니다.
Review Subgrapqh에서 username field을 제거하면 external안써도 되는거 아니에요?

Review subgraph에서 author를 필드로 조회할 수 없게됩니다. external을 사용해야, client에서는 여러 subgraph에 존재하는 스키마들이 composed된 스키마를 사용할 수 있게 되며, federation을 통해 Review.author를 조회할 수 있게 됩니다.
external 디렉티브는 federation이 쿼리 플랜을 구성할때 나말고 다른 subgraph가 이 필드 resolve 할거야 라고 설정해주는 역할을 합니다.

추가 내용)
key를 공유하면 subgraph의 모든 필드가 같을 필요가 없습니다. resolve할수 있는 subgraph에서만 해당 필드는 선언해도될 것 같습니다. 그러면 external을 사용하지 않아도 됩니다.

참고:
Subgraph schemas: https://www.apollographql.com/docs/federation/federated-types/overview#subgraph-schemas

profile
하루 하나씩

0개의 댓글