graphql-js README

The project's README from github.com/graphql/graphql-js. As one markdown file: README.md.

GraphQL.js

The JavaScript reference implementation for GraphQL, a query language for APIs created by Facebook.

npm version Build Status

See more complete documentation at https://graphql.org/ and https://graphql.org/graphql-js/.

Looking for help? Find resources from the community.

Getting Started

A general overview of GraphQL is available in the README for the Specification for GraphQL. That overview describes a simple set of GraphQL examples that exist as tests in this repository. A good way to get started with this repository is to walk through that README and the corresponding tests in parallel.

Using GraphQL.js

Install GraphQL.js from npm

With npm:

npm install --save graphql

With yarn:

yarn add graphql

With bun:

bun add graphql

GraphQL.js provides two important capabilities: building a type schema and serving queries against that type schema.

First, build a GraphQL type schema which maps to your codebase.

import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
} from 'graphql';

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      hello: {
        type: GraphQLString,
        resolve() {
          return 'world';
        },
      },
    },
  }),
});

This defines a simple schema, with one type and one field, that resolves to a fixed value. The resolve function can return a value, a promise, or an array of promises. A more complex example is included in the top-level tests directory.

Then, serve the result of a query against that type schema.

var source = '{ hello }';

graphql({ schema, source }).then((result) => {
  // Prints
  // {
  //   data: { hello: "world" }
  // }
  console.log(result);
});

This runs a query fetching the one field defined. The graphql function will first ensure the query is syntactically and semantically valid before executing it, reporting errors otherwise.

var source = '{ BoyHowdy }';

graphql({ schema, source }).then((result) => {
  // Prints
  // {
  //   errors: [
  //     { message: 'Cannot query field BoyHowdy on RootQueryType',
  //       locations: [ { line: 1, column: 3 } ] }
  //   ]
  // }
  console.log(result);
});

Want to ride the bleeding edge?

The npm branch in this repository is automatically maintained to be the last commit to 17.x.x to pass all tests, in the same form found on npm. It is recommended to use builds deployed to npm for many reasons, but if you want to use the latest not-yet-released version of graphql-js, you can do so by depending directly on this branch:

npm install graphql@git://github.com/graphql/graphql-js.git#npm

Using in a Browser

GraphQL.js is a general-purpose library and can be used both in a Node server and in the browser. As an example, the GraphiQL tool is built with GraphQL.js!

Building a project using GraphQL.js with webpack or rollup should just work and only include the portions of the library you use. This works because GraphQL.js is distributed with both CommonJS (require()) and ESModule (import) files. The exports map within the project package.json should direct runtimes and bundlers to the appropriate files. Tools that do not support exports will find the CommonJS and ESModule builds side by side, with the CommonJS code packaged in files with the .js extension and the ESModule build within .mjs files.

Contributing

We actively welcome pull requests. Learn how to contribute.

This repository is managed by EasyCLA. Project participants must sign the free GraphQL Specification Membership agreement before making a contribution. You only need to do this one time, and it can be signed by individual contributors or their employers.

To initiate the signature process please open a PR against this repo. The EasyCLA bot will block the merge if we still need a membership agreement from you.

You can find detailed information here. If you have issues, please email [email protected].

If your company benefits from GraphQL and you would like to provide essential financial support for the systems and people that power our community, please also consider membership in the GraphQL Foundation.

Changelog

Changes are tracked as GitHub releases.

License

GraphQL.js is MIT-licensed.

Version Support

GraphQL.JS follows Semantic Versioning (SemVer) for its releases. Our version support policy is as follows:

Long-Term Support (LTS)

We do not currently offer a Long-Term Support version of GraphQL.JS. Users are encouraged to upgrade to the latest stable version to receive the most up-to-date features, performance improvements, and security updates.

End-of-Life (EOL) Schedule

We will announce the EOL date for a major version at least 6 months in advance. After a version reaches its EOL, it will no longer receive updates, even for critical security issues.

Upgrade Assistance

To assist users in upgrading to newer versions:

Security Updates

We prioritize the security of GraphQL.JS:

Community Contributions

We welcome community contributions for all versions of GraphQL.JS. However, our maintainers will primarily focus on reviewing and merging contributions for supported versions.

graphql-js (graphql/graphql-js)

Searching Database…

这是什么?GraphQL 的 JavaScript 参考实现。GraphQL 是 Facebook 推出的一种 API 查询语言——这是在 JavaScript 或 TypeScript 中构建和执行 GraphQL API 的核心库。
它是一个开箱即用的 GraphQL 服务器,还是一个构建组件?是构建组件:GraphQL.js 提供两项能力——构建一个映射到你代码库的类型 schema,以及针对该 schema 执行查询。你需要自己把它接入自己的 Node 服务器或应用。
怎么把它加到项目里?基本用法是什么样的?用 npm、yarn 或 bun 安装 graphql 包,定义一个 schema,其中的类型字段带有 resolve 函数,然后用 graphql() 来执行查询。每个查询在执行前都会做语法和语义校验,有问题会报错。
如果我用了某个版本,它能持续获得修复多长时间?它遵循 SemVer:最新的主版本获得完整支持;上一个主版本继续获得 bug 和安全修复,外加 12 个月的非破坏性功能回移;更旧的版本不再支持。没有 LTS 路线,停止维护(EOL)日期会提前至少 6 个月公布。
它只能在 Node 里用,还是浏览器里也能用?都可以——这是一个通用库,既能在 Node 服务器中运行,也能在浏览器中运行;GraphiQL 工具就是用它构建的。它同时提供 CommonJS 和 ESModule 构建,所以 webpack、rollup 这类打包工具只会打包你用到的部分。
在商业项目里可以免费使用吗?可以——GraphQL.js 采用 MIT 许可证,可以免费使用、修改和分发,包括商业用途。
resolver 函数可以异步加载数据吗?可以——resolve 函数可以返回一个普通值、一个 promise,或一个 promise 数组,因此字段可以由数据库或 API 这类异步数据源支撑。
主版本之间升级麻烦吗?主版本可能包含破坏性变更,但项目会为每个版本发布详细的发布说明和迁移指南,社区的 Discord 频道 #graphql-js 也能提供升级帮助。
What are similar projects?crystal, ua-parser-js, mongoose, zod, ajv, joi, fastify, protobuf.js
Creator
graphql
Size
33.8 MB
Stars
20.3K
Age
11y
Commits · 90 Days