@apollo/graphql

中文文档
14.5.0发布于 2019/08/21MITTypeScriptESM + CJS近 30 天下载 2065

A Query Language and Runtime which can target any service.

原始使用文档
文档目录7 节
  1. Getting Started
  2. Using GraphQL.js
  3. Using in a Browser
  4. Contributing
  5. Changelog
  6. License
  7. Credits

GraphQL.js

A JavaScript implementation of GraphQL, a query language for APIs created by Facebook.

Build Status npm version

The purpose of this fork is to increase the velocity with which we at Apollo are able to improve the graphql implementation, while also gradually converting it from Flow to TypeScript.

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

Looking for help? Find resources from the community.

Getting Started

An overview of GraphQL in general 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

npm install --save @apollo/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 code base.

import {
  graphql,
  GraphQLSchema,
  GraphQLObjectType,
  GraphQLString,
} from '@apollo/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 query = '{ hello }';

graphql(schema, query).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 query = '{ boyhowdy }';

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

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. Ensure that any custom build configurations look for .mjs files!

Contributing

Read the Apollo Contributor Guidelines.

TypeScript

Part of this fork's purpose is to begin a gradual migration of graphql-js from Flow to TypeScript. Some parts have already been converted, and some remain to be converted. The steps for converting a file from Flow to TypeScript are as follows:

  1. Rename the file, and convert syntax to TypeScript: Foo.js becomes Foo.ts, and any Flow specific syntax should be transalted.
  2. Create a Foo.js.flow type declaration file for the file's exports (Foo.d.ts, if present, can be helpful for guiding this process). Make sure you prefix the file with // @flow, or else strange things will happen!
  3. Delete the Foo.d.ts file, if it exists.
  4. Run npm run check, and ensure both flow and tsc typecheck the project without issue.

Check out 45da517 for an example of this process.

Changelog

Changes are tracked as GitHub releases.

License

GraphQL.js is MIT-licensed.

Credits

The .d.ts files in this project are, in part, from @types/graphql, written by:

And licensed under the MIT License.

Thanks to all the above contributors!

版本与发布记录

@apollo/graphql 共发布 5 个版本,首次发布于 2019/08/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
14.5.0latest2019/08/217 年前
14.4.52019/08/217 年前
14.4.42019/08/207 年前
14.4.32019/08/177 年前
14.4.22019/08/167 年前

关于 @apollo/graphql 的常见问题

@apollo/graphql 是什么?

A Query Language and Runtime which can target any service.

如何安装 @apollo/graphql?

在项目目录运行 npm install @apollo/graphql,或使用 pnpm add / yarn add / bun add。

@apollo/graphql 支持 TypeScript 吗?

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

@apollo/graphql 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@apollo/graphql 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 6.x。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@apollo/graphql 可以商用吗?

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

@apollo/graphql 的下载量有多少?

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

@apollo/graphql 还在维护吗?

最新版本 14.5.0 发布于 2019/08/21(7 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/08/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。