GraphQL Query Complexity 项目教程

GraphQL Query Complexity 项目教程

graphql-query-complexityGraphQL query complexity analysis and validation for graphql-js项目地址:https://gitcode.com/gh_mirrors/gr/graphql-query-complexity

1. 项目的目录结构及介绍

GraphQL Query Complexity 项目的目录结构如下:

graphql-query-complexity/
├── src/
│   ├── estimators/
│   │   ├── fieldExtensionsEstimator.ts
│   │   ├── simpleEstimator.ts
│   ├── index.ts
├── examples/
│   ├── apollo-server/
│   │   ├── index.ts
│   ├── express-graphql/
│   │   ├── index.ts
├── package.json
├── README.md

目录结构介绍

  • src/: 包含项目的主要源代码。
    • estimators/: 包含用于计算查询复杂度的估算器。
      • fieldExtensionsEstimator.ts: 用于从字段扩展中获取复杂度。
      • simpleEstimator.ts: 简单的复杂度估算器。
    • index.ts: 项目的主入口文件。
  • examples/: 包含不同框架的示例代码。
    • apollo-server/: 使用 Apollo Server 的示例。
    • express-graphql/: 使用 Express-GraphQL 的示例。
  • package.json: 项目的依赖和脚本配置文件。
  • README.md: 项目的介绍和使用说明。

2. 项目的启动文件介绍

项目的启动文件位于 examples/ 目录下,分别有 apollo-server/index.tsexpress-graphql/index.ts

Apollo Server 示例启动文件

// examples/apollo-server/index.ts
import { ApolloServer } from 'apollo-server';
import { getComplexity, fieldExtensionsEstimator, simpleEstimator } from 'graphql-query-complexity';
import { schema } from '../schema';

async function bootstrap() {
  const server = new ApolloServer({
    schema,
    plugins: [
      {
        requestDidStart: async () => ({
          async 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 Error(`Sorry, too complicated query! ${complexity} is over 20 that is the max allowed complexity.`);
            }
            console.log("Used query complexity points:", complexity);
          },
        }),
      },
    ],
  });

  const { url } = await server.listen({ port: 4000 });
  console.log(`GraphQL server ready at ${url}`);
}

bootstrap();

Express-GraphQL 示例启动文件

// examples/express-graphql/index.ts
import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import { getComplexity, fieldExtensionsEstimator, simpleEstimator } from 'graphql-query-complexity';
import { schema } from '../schema';

const app = express();

app.use('/graphql', graphqlHTTP({
  schema,
  graphiql: true,
  customFormatErrorFn: (error) => ({
    message: error.message,
    locations: error.locations,
    stack: error.stack ? error.stack.split('\n') : [],
    path: error.path,
  }),
  extensions: ({ document, variables, operationName }) => {
    const complexity = getComplexity({
      schema,
      operationName,
      query: document,
      variables,
      estimators: [
        fieldExtensionsEstimator(),
        simpleEstimator({ defaultComplexity: 1 }),
      ],
    });
    if (complexity > 20) {
      throw new Error(`Sorry, too complicated query! ${complexity} is over 20 that is the max allowed complexity.`);
    }
    console.log("Used query complexity points:", complexity);
    return { complexity };
  },
}));

app.listen(4000, () => {
  console.log('GraphQL server ready at http://localhost:4000/graphql');
});

3.

graphql-query-complexityGraphQL query complexity analysis and validation for graphql-js项目地址:https://gitcode.com/gh_mirrors/gr/graphql-query-complexity

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

殷巧或

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值