<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 聯合類型 聯合類型與接口非常相似,但是它們沒有指定類型之間的任何公共字段(詳情請參閱[這里](https://graphql.org/learn/schema/#union-types))。聯合類型對于單個字段返回不相交的數據類型很有用。 ### 代碼優先 要定義 GraphQL 聯合類型,我們必須先定義組成這個聯合類型的各個類。遵循 Apollo 文檔中的[示例](https://www.apollographql.com/docs/apollo-server/schema/unions-interfaces/#union-type),我們將創建兩個類。首先,`Book`: ```typescript import { Field, ObjectType } from '@nestjs/graphql'; @ObjectType() export class Book { @Field() title: string; } ``` 然后是 `Author`: ```typescript import { Field, ObjectType } from '@nestjs/graphql'; @ObjectType() export class Author { @Field() name: string; } ``` 在這里,我們使用從 `@nestjs/graphql` 包里導出的 `createUnionType` 函數來注冊 `ResultUnion` 這個聯合類型。 ~~~typescript export const ResultUnion = createUnionType({ name: 'ResultUnion', types: () => [Author, Book] as const, }); ~~~ > `createUnionType` 函數的 types 屬性返回的數組應該被賦予一個 `const `斷言。 如果沒有給出 `const` 斷言,編譯時會生成錯誤的聲明文件,從另一個項目中使用時會出錯。 現在,我們就可以在查詢中引用 `ResultUnion` 這個聯合類型來。 ```typescript @Query(returns => [ResultUnion]) search(): Array<typeof ResultUnion> { return [new Author(), new Book()]; } ``` 最終的結果是在 SDL 中生成以下部分的 GraphQL schema: ```graphql type Author { name: String! } type Book { title: String! } union ResultUnion = Author | Book type Query { search: [ResultUnion!]! } ``` 默認的 `resolveType()` 函數是通過庫根據解析器方法返回值提取的類型來生成的。這意味著你必須返回類的實例(你不能返回 JavaScript 對象字面量)。 提供自定義的 `resolveType()` 函數,將 `resolveType` 屬性傳遞給 `@InterfaceType()` 裝飾器里的選項對象,如下所示: ```typescript export const ResultUnion = createUnionType({ name: 'ResultUnion', types: () => [Author, Book], resolveType(value) { if (value.name) { return Author; } if (value.title) { return Book; } return null; }, }); ``` ### 模式優先 在模式優先方式中定義聯合類型,只需使用 SDL 創建一個 GraphQL 聯合類型。 ```graphql type Author { name: String! } type Book { title: String! } union ResultUnion = Author | Book ``` 然后,你可以使用類型生成功能(如[快速開始](/8/graphql?id=快速開始)章節所示)生成相應的 TypeScript 定義。 ```typescript export class Author { name: string; } export class Book { title: string; } export type ResultUnion = Author | Book; ``` 在解析器映射圖中,聯合類型需要一個額外的 `__resolveType` 字段,來確定聯合類型應該解析為哪個類型。另外,請注意, `ResultUnionResolver` 這個類在任何模塊中都必須被注冊為提供者。讓我們創建一個 `ResultUnionResolver` 類并定義 `__resolveType` 方法: ```typescript @Resolver('ResultUnion') export class ResultUnionResolver { @ResolveField() __resolveType(value) { if (value.name) { return 'Author'; } if (value.title) { return 'Book'; } return null; } } ``` > 所有裝飾器都是從 `@nestjs/graphql` 包里導出。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看