UNPKG

5.92 kBMarkdownView Raw
1# GraphQL.js
2
3The JavaScript reference implementation for GraphQL, a query language for APIs created by Facebook.
4
5[![npm version](https://badge.fury.io/js/graphql.svg)](https://badge.fury.io/js/graphql)
6[![Build Status](https://github.com/graphql/graphql-js/workflows/CI/badge.svg?branch=main)](https://github.com/graphql/graphql-js/actions?query=branch%3Amain)
7[![Coverage Status](https://codecov.io/gh/graphql/graphql-js/branch/main/graph/badge.svg)](https://codecov.io/gh/graphql/graphql-js)
8
9See more complete documentation at https://graphql.org/ and
10https://graphql.org/graphql-js/.
11
12Looking for help? Find resources [from the community](https://graphql.org/community/).
13
14## Getting Started
15
16A general overview of GraphQL is available in the
17[README](https://github.com/graphql/graphql-spec/blob/main/README.md) for the
18[Specification for GraphQL](https://github.com/graphql/graphql-spec). That overview
19describes a simple set of GraphQL examples that exist as [tests](src/__tests__)
20in this repository. A good way to get started with this repository is to walk
21through that README and the corresponding tests in parallel.
22
23### Using GraphQL.js
24
25Install GraphQL.js from npm
26
27With npm:
28
29```sh
30npm install --save graphql
31```
32
33or using yarn:
34
35```sh
36yarn add graphql
37```
38
39GraphQL.js provides two important capabilities: building a type schema and
40serving queries against that type schema.
41
42First, build a GraphQL type schema which maps to your codebase.
43
44```js
45import {
46 graphql,
47 GraphQLSchema,
48 GraphQLObjectType,
49 GraphQLString,
50} from 'graphql';
51
52var schema = new GraphQLSchema({
53 query: new GraphQLObjectType({
54 name: 'RootQueryType',
55 fields: {
56 hello: {
57 type: GraphQLString,
58 resolve() {
59 return 'world';
60 },
61 },
62 },
63 }),
64});
65```
66
67This defines a simple schema, with one type and one field, that resolves
68to a fixed value. The `resolve` function can return a value, a promise,
69or an array of promises. A more complex example is included in the top-level [tests](src/__tests__) directory.
70
71Then, serve the result of a query against that type schema.
72
73```js
74var source = '{ hello }';
75
76graphql({ schema, source }).then((result) => {
77 // Prints
78 // {
79 // data: { hello: "world" }
80 // }
81 console.log(result);
82});
83```
84
85This runs a query fetching the one field defined. The `graphql` function will
86first ensure the query is syntactically and semantically valid before executing
87it, reporting errors otherwise.
88
89```js
90var source = '{ BoyHowdy }';
91
92graphql({ schema, source }).then((result) => {
93 // Prints
94 // {
95 // errors: [
96 // { message: 'Cannot query field BoyHowdy on RootQueryType',
97 // locations: [ { line: 1, column: 3 } ] }
98 // ]
99 // }
100 console.log(result);
101});
102```
103
104**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.
105
106### Want to ride the bleeding edge?
107
108The `npm` branch in this repository is automatically maintained to be the last
109commit to `main` to pass all tests, in the same form found on npm. It is
110recommended to use builds deployed to npm for many reasons, but if you want to use
111the latest not-yet-released version of graphql-js, you can do so by depending
112directly on this branch:
113
114```
115npm install graphql@git://github.com/graphql/graphql-js.git#npm
116```
117
118### Experimental features
119
120Each 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.
121
122```
123"graphql": "experimental-stream-defer"
124```
125
126Community feedback on this experimental release is much appreciated and can be provided on the [issue created for this purpose](https://github.com/graphql/graphql-js/issues/2848).
127
128### Using in a Browser
129
130GraphQL.js is a general-purpose library and can be used both in a Node server
131and in the browser. As an example, the [GraphiQL](https://github.com/graphql/graphiql/)
132tool is built with GraphQL.js!
133
134Building a project using GraphQL.js with [webpack](https://webpack.js.org) or
135[rollup](https://github.com/rollup/rollup) should just work and only include
136the portions of the library you use. This works because GraphQL.js is distributed
137with both CommonJS (`require()`) and ESModule (`import`) files. Ensure that any
138custom build configurations look for `.mjs` files!
139
140### Contributing
141
142We actively welcome pull requests. Learn how to [contribute](./.github/CONTRIBUTING.md).
143
144This repository is managed by EasyCLA. Project participants must sign the free ([GraphQL Specification Membership agreement](https://preview-spec-membership.graphql.org) before making a contribution. You only need to do this one time, and it can be signed by [individual contributors](http://individual-spec-membership.graphql.org/) or their [employers](http://corporate-spec-membership.graphql.org/).
145
146To 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.
147
148You can find [detailed information here](https://github.com/graphql/graphql-wg/tree/main/membership). If you have issues, please email [operations@graphql.org](mailto:operations@graphql.org).
149
150If 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](https://foundation.graphql.org/join).
151
152### Changelog
153
154Changes are tracked as [GitHub releases](https://github.com/graphql/graphql-js/releases).
155
156### License
157
158GraphQL.js is [MIT-licensed](./LICENSE).