百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
G

graphql-tools

> 后端框架
开源

:wrench: 用于 GraphQL 的实用工具库,以 SDL 优先(SDL-first)的方式构建、拼接和模拟 GraphQL schema

5.4K stars0 点赞0 次浏览
访问官网GitHub

工具介绍

:wrench: 用于 GraphQL 的实用工具库,以 SDL 优先(SDL-first)的方式构建、拼接和模拟 GraphQL schema

renovate-app badge

本软件包提供了几种创建 GraphQL schema 的实用方法:

  1. 使用 GraphQL schema 语言来 解析器、接口、联合类型和自定义标量。生成的 schema 与 GraphQL.js 完全兼容。
  2. 模拟你的 GraphQL API,支持针对每种类型的细粒度 模拟
  3. 自动 合并为一个更大的 API

文档

绑定到 HTTP

如果你想将你的 JavaScript GraphQL schema 绑定到 HTTP 服务器,可以使用

你可以结合使用 graphql-tools 和 GraphQL Yoga 来开发基于 JavaScript 的 GraphQL API: 一个用于编写 schema 和 resolver 代码,另一个用于将其连接到 Web 服务器。

示例

使用 graphql-tools 时,你需要用 GraphQL 类型语言字符串来描述 schema:

…

然后,你将 resolver 定义为一个嵌套对象,将类型和字段名称映射到 resolver 函数:

javascript
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 将 schema 和 resolver 组合起来:

javascript
import { makeExecutableSchema } from '@graphql-tools/schema'

const executableSchema = makeExecutableSchema({
  typeDefs,
  resolvers
})

GraphQL-Tools 的 schema 可以被使用

generate a schema with full support for stitch multiple schemas together Read the docs. GraphQL Yoga .

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

TypeScriptgraphqlgraphql-apigraphql-jsgraphql-schema

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类后端框架
定价开源

> 相关工具

N
Node.js
基于 V8 的 JavaScript 运行时
D
Django
Python 高级 Web 框架
S
Spring Boot
Java 生态主流微服务框架