@ardatan/graphql-tools

中文文档
4.1.0发布于 2020/01/12MITTypeScriptCommonJS近 30 天下载 6.5万

Useful tools to create and manipulate GraphQL schemas.

原始使用文档
文档目录5 节
  1. Documentation
  2. Binding to HTTP
  3. Example
  4. Contributions
  5. Maintainers

GraphQL-tools: generate and mock GraphQL.js schemas

npm version Build Status Coverage Status Get on Slack

This package provides a few useful ways to create a GraphQL schema:

  1. Use the GraphQL schema language to generate a schema with full support for resolvers, interfaces, unions, and custom scalars. The schema produced is completely compatible with GraphQL.js.
  2. Mock your GraphQL API with fine-grained per-type mocking
  3. Automatically stitch multiple schemas together into one larger API

Documentation

Read the docs.

Binding to HTTP

If you want to bind your JavaScript GraphQL schema to an HTTP server, we recommend using Apollo Server, which supports every popular Node HTTP server library including Express, Koa, Hapi, and more.

JavaScript GraphQL servers are often developed with graphql-tools and apollo-server-express together: One to write the schema and resolver code, and the other to connect it to a web server.

Example

When using graphql-tools, you describe the schema as a GraphQL type language string:


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;

Then you define resolvers as a nested object that maps type and field names to resolver functions:

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;

At the end, the schema and resolvers are combined using makeExecutableSchema:

import { makeExecutableSchema } from 'graphql-tools';

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

This example has the entire type definition in one string and all resolvers in one file, but you can combine types and resolvers from multiple files and objects, as documented in the modularizing the schema section of the docs.

Contributions

Contributions, issues and feature requests are very welcome. If you are using this package and fixed a bug for yourself, please consider submitting a PR!

Maintainers

版本与发布记录

@ardatan/graphql-tools 共发布 4 个版本,首次发布于 2020/01/11;过去 12 个月发布了 0 个版本。

版本发布日期距今
4.1.0latest2020/01/126 年前
4.0.92020/01/126 年前
4.0.82020/01/126 年前
4.0.72020/01/116 年前

关于 @ardatan/graphql-tools 的常见问题

@ardatan/graphql-tools 是什么?

Useful tools to create and manipulate GraphQL schemas.

如何安装 @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 和提交记录判断。