<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 復雜性 > 此章節僅適應于代碼優先模式。 查詢復雜性允許你定義某些字段的復雜程度,并限制**最大復雜性**的查詢。其原理是通過使用一個簡單的數字來定義每個字段的復雜度。通常每個字段的復雜度默認為1。另外,GraphQL 查詢的復雜性計算可以使用所謂的復雜度估算器進行定制。復雜度估算器是一個計算字段復雜度的簡單函數。你可以將任意數量的復雜度估算器添加到規則中,然后一個接一個地執行。第一個返回數字復雜度值的估算器確定該字段的復雜度。 `@nestjs/graphql` 包與 <span style="color:red">graphql-query-complexity</span> 等工具能很好地集成,他們提供了一種基于成本分析的解決方案。有了這個庫,你可以拒絕在你的 GraphQL 服務中執行成本過高的查詢。 ### 安裝 要開始使用它,我們首先要安裝它所需要的依賴包。 ```bash $npm install --save graphql-query-complexity ``` ### 快速開始 一旦安裝完,我們就可以定義 `ComplexityPlugin` 這個類: ```typescript import { GraphQLSchemaHost, Plugin } from '@nestjs/graphql'; import { ApolloServerPlugin, GraphQLRequestListener, } from 'apollo-server-plugin-base'; import { GraphQLError } from 'graphql'; import { fieldExtensionsEstimator, getComplexity, simpleEstimator, } from 'graphql-query-complexity'; @Plugin() export class ComplexityPlugin implements ApolloServerPlugin { constructor(private gqlSchemaHost: GraphQLSchemaHost) {} requestDidStart(): GraphQLRequestListener { const { schema } = this.gqlSchemaHost; return { didResolveOperation({ request, document }) { const complexity = getComplexity({ schema, operationName: request.operationName, query: document, variables: request.variables, estimators: [ fieldExtensionsEstimator(), simpleEstimator({ defaultComplexity: 1 }), ], }); if (complexity >= 20) { throw new GraphQLError( `Query is too complex: ${complexity}. Maximum allowed complexity: 20`, ); } console.log('Query Complexity:', complexity); }, }; } } ``` 為了演示,我們將允許的最大復雜度指定為 20。在上面的示例中,我們使用了 2 個估算器,`simpleEstimator` 和 `fieldExtensionsEstimator`。 - `simpleEstimator`:該簡單估算器為每個字段返回一個固定復雜度 - `fieldExtensionsEstimator`:字段擴展估算器提取 schema 中每個字段的復雜度值 > 別忘了將此類添加到任意模塊的提供者數組中。 ### 字段級復雜性 有了這個插件,我們可以為任意字段定義復雜度了,做法就是在傳遞給 `@Field()` 裝飾器的選項對象中,指定 `complexity` 屬性的值,如下所示: ```typescript @Field({ complexity: 3 }) title: string; ``` 或者,你也可以定義估算器函數: ```typescript @Field({ complexity: (options: ComplexityEstimatorArgs) => ... }) title: string; ``` ### 查詢/變更級復雜性 此外,`@Query` 和 `@Mutation()` 裝飾器也具有復雜性屬性,可以被這樣指定: ```typescript @Query({ complexity: (options: ComplexityEstimatorArgs) => options.args.count * options.childComplexity }) items(@Args('count') count: number) { return this.itemsService.getItems({ count }); } ```
                  <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>

                              哎呀哎呀视频在线观看