# @ardatan/graphql-tools 中文文档

规范地址：https://npmcn.com/package/%40ardatan/graphql-tools/

版本：4\.1\.0
npm 元数据更新时间：2022-04-04T15:22:25.736Z
中文译文更新时间：2026-11-06T18:05:03.299Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40ardatan/graphql-tools
原文对照：https://npmcn.com/package/%40ardatan/graphql-tools/?lang=original
项目主页：https://github.com/apollostack/graphql-tools#readme
源码仓库：https://github.com/apollographql/graphql-tools

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：427 kB，182 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- 运行依赖：5 个
- peer 依赖：graphql ^0\.13\.0 \|\| ^14\.0\.0
- 首次发布：2020-01-11T14:59:20.748Z
- 最新版本发布：2020-01-12T02:53:16.024Z
- 近 30 天下载量：65067（2026-10-07 至 2026-11-05，npm 官方下载统计）
- 已发布版本数：4（近 12 个月 0 个）

## 中文简介

用于创建和操纵GraphQL计划的工具.

## 安装

```sh
npm install @ardatan/graphql-tools
```

也可以使用：`pnpm add @ardatan/graphql-tools`、`yarn add @ardatan/graphql-tools`、`bun add @ardatan/graphql-tools`

## README

# 图QL-工具:生成并模拟图QL.js计划

[![npm version](https://badge.fury.io/js/graphql-tools.svg)](https://badge.fury.io/js/graphql-tools) [![Build Status](https://travis-ci.org/apollographql/graphql-tools.svg?branch=master)](https://travis-ci.org/apollographql/graphql-tools) [![Coverage Status](https://coveralls.io/repos/github/apollographql/graphql-tools/badge.svg?branch=master)](https://coveralls.io/github/apollographql/graphql-tools?branch=master) [![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](http://www.apollostack.com/#slack)

此软件包提供了创建 GraphQL 方案的若干有用方法:

1.  使用 GraphQL 语法 [生成一个方案](https://www.apollographql.com/docs/graphql-tools/generate-schema.html) 并全力支持解决者、界面、结合和定制的分级。 制作的图案完全兼容 [图QL.js](https://github.com/graphql/graphql-js)。 。 。 .
2.  [锁定您的图形QL API](https://www.apollographql.com/docs/graphql-tools/mocking.html) 与精细的每类嘲笑
3.  自动 [将多个计划缝合在一起](https://www.apollographql.com/docs/graphql-tools/schema-stitching.html) 变成一个更大的 API

## 文档

[读取文件.](https://www.apollographql.com/docs/graphql-tools/)

## 装入 HTTP

如果您想要将您的 JavaScript GraphQL 计划绑定在一个 HTTP 服务器上, 我们建议使用 [阿波罗服务器](https://github.com/apollographql/apollo-server/),它支持每个受欢迎的节点HTTP服务器库,包括Express,Koa,Hapi等.

JavaScript GraphQL 服务器通常与 `graphql-tools` 财务报告和审定财务报表 `apollo-server-express` 一起:一个是写出chema和解析器代码,另一个是把它连接到网络服务器上.

## 示例

使用时 `graphql-tools`,您将方案描述为 GraphQL 类型语言字符串 :

```js

const typeDefs = `
type Author {
  id: ID! # the ! means that every author object _must_ have an id
  firstName: String
  lastName: String
  """
  the list of Posts by this author
  """
  posts: [Post]
}

type Post {
  id: ID!
  title: String
  author: Author
  votes: Int
}

# the schema allows the following query:
type Query {
  posts: [Post]
}

# this schema allows the following mutation:
type Mutation {
  upvotePost (
    postId: ID!
  ): Post
}

# we need to tell the server which types represent the root query
# and root mutation types. We call them RootQuery and RootMutation by convention.
schema {
  query: Query
  mutation: Mutation
}
`;

export default typeDefs;
```

然后定义解析器为嵌入对象,将类型和字段名称映射为解析器函数:

```js
const resolvers = {
  Query: {
    posts() {
      return posts;
    },
  },
  Mutation: {
    upvotePost(_, { postId }) {
      const post = find(posts, { id: postId });
      if (!post) {
        throw new Error(`Couldn't find post with id ${postId}`);
      }
      post.votes += 1;
      return post;
    },
  },
  Author: {
    posts(author) {
      return filter(posts, { authorId: author.id });
    },
  },
  Post: {
    author(post) {
      return find(authors, { id: post.authorId });
    },
  },
};

export default resolvers;
```

最后,将计划与解决者合并使用 `makeExecutableSchema`数字 :

```js
import { makeExecutableSchema } from 'graphql-tools';

const executableSchema = makeExecutableSchema({
  typeDefs,
  resolvers,
});
```

此示例包含一个字符串中的整个类型定义, 以及一个文件中的所有解析器, 但是您可以将多个文件和对象中的类型和解析器组合起来, 如文件 [模块化计划a](https://www.apollographql.com/docs/graphql-tools/generate-schema.html#modularizing) 部分.

## 捐款

发言、问题和专题请求非常受欢迎。 如果您正在使用此软件包并为自己修复一个错误, 请考虑提交 PR !

## 维护者

-   [@hwillson (英语)](https://github.com/hwillson) (阿波罗
-   [@ 本贾姆](https://github.com/benjamn) (阿波罗

## 常见问题

### @ardatan/graphql-tools 是什么？

用于创建和操纵GraphQL计划的工具\.

### 如何安装 @ardatan/graphql-tools？

在项目目录运行 npm install @ardatan/graphql-tools，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：graphql（^0\.13\.0 \|\| ^14\.0\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @ardatan/graphql-tools 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @ardatan/graphql-tools 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @ardatan/graphql-tools 可以商用吗？

@ardatan/graphql-tools 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @ardatan/graphql-tools 的下载量有多少？

根据 npm 官方下载统计，@ardatan/graphql-tools 在 2026/10/07 – 2026/11/05 这 30 天内被下载 65,067 次，日均约 2,169 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @ardatan/graphql-tools 还在维护吗？

最新版本 4\.1\.0 发布于 2020/01/12（6 年前）；至今共发布 4 个版本，其中过去 12 个月发布了 0 个；首次发布于 2020/01/11。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

