graphql
TypeScript icon, indicating that this package has built-in type declarations

16.10.0 • Public • Published

GraphQLConf 2024 Banner: September 10-12, San Francisco. Hosted by the GraphQL Foundation

GraphQL.js

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

npm version Build Status Coverage 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

or using yarn:

yarn 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);
});

Note: Please don't forget to set NODE_ENV=production if you are running a production server. It will disable some checks that can be useful during development but will significantly improve performance.

Want to ride the bleeding edge?

The npm branch in this repository is automatically maintained to be the last commit to main 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

Experimental features

Each release of GraphQL.js will be accompanied by an experimental release containing support for the @defer and @stream directive proposal. We are hoping to get community feedback on these releases before the proposal is accepted into the GraphQL specification. You can use this experimental release of GraphQL.js by adding the following to your project's package.json file.

"graphql": "experimental-stream-defer"

Community feedback on this experimental release is much appreciated and can be provided on the issue created for this purpose.

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

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 operations@graphql.org.

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.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
16.10.03,953,216latest
15.10.1735,78015.x.x
17.0.0-alpha.8653alpha
16.0.0-rc.726rc
0.8.0-beta316beta

Version History

VersionDownloads (Last 7 Days)Published
17.0.0-alpha.8653
15.10.1735,780
16.10.03,953,216
17.0.0-alpha.71,275
17.0.0-alpha.612
15.9.0441,482
16.9.01,817,493
17.0.0-alpha.599
17.0.0-alpha.49
16.8.2138,199
16.8.12,104,623
17.0.0-alpha.3259
16.8.079,391
16.7.1144,227
16.7.01,449
17.0.0-alpha.27,696
16.6.0501,896
17.0.0-alpha.1124
16.5.0153,651
16.4.032,580
16.3.096,290
16.2.017,017
16.1.03,726
15.8.02,242,792
16.0.111,598
16.0.0-experimental-stream-defer.5408
16.0.07,146
15.7.2136,901
16.0.0-rc.7.experimental-stream-defer.418
15.7.11,901
16.0.0-rc.726
15.7.09,862
16.0.0-rc.616
16.0.0-rc.515
16.0.0-rc.417
16.0.0-rc.318
15.6.129,613
15.6.020,501
15.5.320,618
15.5.22,561
16.0.0-rc.2.experimental-stream-defer.320
16.0.0-rc.214
16.0.0-rc.116
15.5.198,498
16.0.0-alpha.514
16.0.0-alpha.412
16.0.0-alpha.3.experimental-stream-defer.213
16.0.0-alpha.314
16.0.0-alpha.210
16.0.0-alpha.114
15.5.0171,431
15.4.0-experimental-stream-defer.1819
15.4.052,844
14.7.0676,930
15.3.0152,469
15.2.02,125
15.1.04,285
15.0.018,360
15.0.0-rc.2797
14.6.048,579
15.0.0-rc.119
15.0.0-alpha.213
15.0.0-alpha.114
14.5.849,995
14.5.74,705
14.5.6959
14.5.563
14.5.44,209
14.5.3465
14.5.258
14.5.133
14.5.01,807
14.4.25,974
14.4.1347
14.4.01,076
14.3.15,593
14.3.02,929
14.2.118,286
14.2.011,025
14.1.19,171
14.1.0185
14.0.228,262
14.0.1103
14.0.060,297
14.0.0-rc.215
14.0.0-rc.113
0.13.289,973
0.13.1335
0.13.011,989
0.13.0-rc.114
0.12.318,236
0.12.222
0.12.122
0.12.018
0.11.7120,656
0.11.647
0.11.544
0.11.457
0.11.37,185
0.11.217
0.11.126
0.11.016
0.10.5146,858
0.10.420
0.10.3835
0.10.220
0.10.11,365
0.10.01,158
0.9.68,791
0.9.515
0.9.417
0.9.323
0.9.2543
0.9.1350
0.9.022
0.8.22,493
0.8.126
0.8.026
0.8.0-beta316
0.8.0-beta222
0.8.0-beta113
0.7.221,473
0.7.116
0.7.0381
0.6.23,253
0.6.111
0.6.016
0.5.0187
0.5.0-beta.113
0.4.18604
0.4.1719
0.4.1612
0.4.1521
0.4.1418
0.4.1315
0.4.1221
0.4.1114
0.4.1017
0.4.914
0.4.821
0.4.720
0.4.623
0.4.524
0.4.415
0.4.314
0.4.216
0.4.118
0.3.019
0.2.612
0.2.516
0.2.418
0.2.316
0.2.211
0.2.122
0.2.066
0.1.1267
0.1.1116
0.1.1011
0.1.916
0.1.816
0.1.714
0.1.614
0.1.512
0.1.417
0.1.312
0.1.212
0.1.114
0.1.015
0.0.217

Package Sidebar

Install

npm i graphql

Weekly Downloads

14,590,028

Version

16.10.0

License

MIT

Unpacked Size

1.36 MB

Total Files

391

Last publish

Collaborators

  • benjie
  • leebyron
  • yaacovcr
  • jdecroock
  • i1g
  • mjmahone
  • enisdenjo