# \[GRAPHQL] Unions and interfaces (ok)

Unions and interfaces are abstract GraphQL types that enable a schema field to return one of multiple object types.

Các liên hợp và giao diện là các kiểu GraphQL trừu tượng cho phép một trường lược đồ trả về một trong nhiều kiểu đối tượng.

### [Union type](https://www.apollographql.com/docs/apollo-server/schema/unions-interfaces/#union-type) <a href="#union-type" id="union-type"></a>

Khi bạn xác định một kiểu liên hợp, bạn khai báo loại đối tượng nào được bao gồm trong liên hợp:

```
union Media = Book | Movie
```

A field can have a union as its return type. In this case, it can return any object type that's included in the union:

Một trường có thể có một union làm kiểu trả về của nó. Trong trường hợp này, nó có thể trả về bất kỳ kiểu đối tượng nào được bao gồm trong union:

```
type Query {
  allMedia: [Media] # This list can include both Books and Movies
}
```

All of a union's included types must be object types (not scalars, input types, etc.). Included types do not need to share any fields.

Tất cả các kiểu bao gồm của liên hợp phải là kiểu đối tượng (không phải kiểu vô hướng, kiểu đầu vào, v.v.). Các loại bao gồm không cần phải chia sẻ bất kỳ trường nào.

#### [Example](https://www.apollographql.com/docs/apollo-server/schema/unions-interfaces/#example) <a href="#example" id="example"></a>

&#x20;The following schema defines a `Result` union type that can return either a `Book` or an `Author`:

Lược đồ sau xác định kiểu kết hợp Kết quả có thể trả về Sách hoặc Tác giả:

```
union Result = Book | Author
type Book {
  title: String
}
type Author {
  name: String
}
type Query {
  search(contains: String): [Result]
}
```

&#x20;The `Result` union enables `Query.search` to return a list that includes both `Book`s and `Author`s.

Liên kết Kết quả cho phép Query.search trả về một danh sách bao gồm cả Sách và Tác giả.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://javascriptuse.gitbook.io/javascript/advanced/graphql-unions-and-interfaces-ok.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
