UNPKG

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