@ardatan/graphql-tools

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

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

中文使用文档
文档目录5 节
  1. 文档
  2. 装入 HTTP
  3. 示例
  4. 捐款
  5. 维护者

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

npm version Build Status Coverage Status Get on Slack

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

  1. 使用 GraphQL 语法 生成一个方案 并全力支持解决者、界面、结合和定制的分级。 制作的图案完全兼容 图QL.js。 。 。 .
  2. 锁定您的图形QL API 与精细的每类嘲笑
  3. 自动 将多个计划缝合在一起 变成一个更大的 API

文档

读取文件.

装入 HTTP

如果您想要将您的 JavaScript GraphQL 计划绑定在一个 HTTP 服务器上, 我们建议使用 阿波罗服务器,它支持每个受欢迎的节点HTTP服务器库,包括Express,Koa,Hapi等.

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

示例

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


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;

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

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数字 :

import { makeExecutableSchema } from 'graphql-tools';

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

此示例包含一个字符串中的整个类型定义, 以及一个文件中的所有解析器, 但是您可以将多个文件和对象中的类型和解析器组合起来, 如文件 模块化计划a 部分.

捐款

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

维护者

版本与发布记录

@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 是什么?

用于创建和操纵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 和提交记录判断。