graphql-js Documentation

A reference implementation of GraphQL for JavaScript

20,348 stars TypeScript TypeScript 1 file ยท ~3,398 tokens #graphql#graphql-js
RAW Doc

1. Project Overview & Quickstart (graphql/graphql-js)

File: README.md

GraphQL.js

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


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:

sh
npm install --save graphql

With yarn:

sh
yarn add graphql

With bun:

sh
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.

js
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.

js
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.

js
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:

text
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:

  • Latest Major Version: We provide full support, including bug fixes and security updates, for the latest major version of GraphQL.JS.
  • Previous Major Version: We offer feature support for the previous major version for 12 months after the release of the newest major version.
    This means that for 12 months we can backport features for specification changes if they don't cause any breaking changes. We'll continue
    supporting the previous major version with bug and security fixes.
  • Older Versions: Versions older than the previous major release are considered unsupported. While the code remains available,
    we do not actively maintain or provide updates for these versions.
    One exception to this rule is when the older version has been released < 1 year ago, in that case we
    will treat it like the "Previous Major Version".

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:

  • We maintain detailed release notes for each version, highlighting new features, breaking changes, and deprecations.
  • Our documentation includes migration guides for moving between major versions.
  • The community forum (Discord channel #graphql-js) is available for users who need additional assistance with upgrades.

Security Updates

We prioritize the security of GraphQL.JS:

  • Critical security updates will be applied to both the current and previous major version.
  • For versions that have reached EOL, we strongly recommend upgrading to a supported version to receive security updates.

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.


File: integrationTests/README.md

Integration Tests

This directory contains integration tests for GraphQL.js across different environments and bundlers, testing basic GraphQL.JS functionality, as well as development mode and production mode behavior.

Tests are run via the main integration test suite in resources/integration-test.ts.

Test Structure

Basic GraphQL.JS Functionality Tests

Each subdirectory represents a different environment/bundler:

  • node - tests for supported Node.js versions
  • ts - tests for supported Typescript versions
  • webpack - tests for Webpack

Verifying Conditional Exports

The conditions subdirectory contains tests that verify the conditional exports of GraphQL.js. These tests ensure that the correct files are imported based on the environment being used.

Verifying Development Mode Tests

Each subdirectory represents a different platform/bundler demonstrating enabling development mode by enabling the development condition or by calling enableDevMode().

Verifying Production Mode Tests

Each subdirectory represents a different environment/bundler demonstrating production mode when development mode is not enabled.


File: src/error/README.md

GraphQL Errors

The graphql/error module is responsible for creating and formatting
GraphQL errors.

js
import { ... } from 'graphql/error'; // ES6
var GraphQLError = require('graphql/error'); // CommonJS

File: src/execution/README.md

GraphQL Execution

The graphql/execution module is responsible for the execution phase of
fulfilling a GraphQL request.

js
import { execute } from 'graphql/execution'; // ES6
var GraphQLExecution = require('graphql/execution'); // CommonJS

File: src/jsutils/README.md

JavaScript Utils

This directory contains dependency-free JavaScript utility functions used
throughout the codebase.

Each utility should belong in its own file and be the default export.

These functions are not part of the module interface and are subject to change.


File: src/language/README.md

GraphQL Language

The graphql/language module is responsible for parsing and operating on the
GraphQL language.

js
import { ... } from 'graphql/language'; // ES6
var GraphQLLanguage = require('graphql/language'); // CommonJS

File: src/type/README.md

GraphQL Type System

The graphql/type module is responsible for defining GraphQL types and schema.

js
import { ... } from 'graphql/type'; // ES6
var GraphQLType = require('graphql/type'); // CommonJS

File: src/validation/README.md

GraphQL Validation

The graphql/validation module fulfills the Validation phase of fulfilling a
GraphQL result.

js
import { validate } from 'graphql/validation'; // ES6
var GraphQLValidator = require('graphql/validation'); // CommonJS

2. Official Technical Reference & Guides (graphql/graphql.github.io)

Source Repository for GraphQL.org

This repository contains the source code for the GraphQL website.

You can find more discussions on the #website channel on the GraphQL Discord.

Table of Contents

Overview

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. It provides:

  • a complete and understandable description of the data in your API,
  • support for powerful developer tooling, and
  • precise querying, which offers several benefits:
    • clients request only the data they need, improving efficiency;
    • new fields and features can be added without impacting existing clients; and
    • field-level usage can be tracked and monitored for insights and optimization.

The GraphQL Specification is open source and governed by the GraphQL Foundation.

Documentation

Deployment

The website is deployed via Vercel on merges to the source branch. To preview changes locally, follow these steps:

  1. Clone the repository:
    git clone https://github.com/graphql/graphql.github.io.git
    cd graphql.github.io
  2. Install dependencies:
    pnpm i
  3. Run the site locally:
    pnpm dev
  4. Open http://localhost:3000 to view it in the browser.

How to Contribute

We welcome contributions! ๐ŸŽ‰ Please refer to our contributing guide for detailed instructions on how to make changes to the GraphQL website.

CLA Process

Before contributing, all participants must sign the free GraphQL Specification Membership Agreement. You only need to do this once, and it can be signed by:

To initiate the signature process, please open a PR against this repository. The EasyCLA bot will block the merge if the membership agreement has not been signed.

For more information on the CLA, check out the detailed instructions here. If you encounter any issues, please contact us at [email protected].

Join the Foundation!

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 consider becoming a member of the GraphQL Foundation.