1 | # GraphQL.js
|
2 |
|
3 | The 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=master)](https://github.com/graphql/graphql-js/actions?query=branch%3Amaster)
|
7 | [![Coverage Status](https://codecov.io/gh/graphql/graphql-js/branch/master/graph/badge.svg)](https://codecov.io/gh/graphql/graphql-js)
|
8 |
|
9 | See more complete documentation at https://graphql.org/ and
|
10 | https://graphql.org/graphql-js/.
|
11 |
|
12 | Looking for help? Find resources [from the community](https://graphql.org/community/).
|
13 |
|
14 | ## Getting Started
|
15 |
|
16 | An overview of GraphQL in general is available in the
|
17 | [README](https://github.com/graphql/graphql-spec/blob/master/README.md) for the
|
18 | [Specification for GraphQL](https://github.com/graphql/graphql-spec). That overview
|
19 | describes a simple set of GraphQL examples that exist as [tests](src/__tests__)
|
20 | in this repository. A good way to get started with this repository is to walk
|
21 | through that README and the corresponding tests in parallel.
|
22 |
|
23 | ### Using GraphQL.js
|
24 |
|
25 | Install GraphQL.js from npm
|
26 |
|
27 | With npm:
|
28 |
|
29 | ```sh
|
30 | npm install --save graphql
|
31 | ```
|
32 |
|
33 | or alternatively using yarn:
|
34 |
|
35 | ```sh
|
36 | yarn add graphql
|
37 | ```
|
38 |
|
39 | GraphQL.js provides two important capabilities: building a type schema, and
|
40 | serving queries against that type schema.
|
41 |
|
42 | First, build a GraphQL type schema which maps to your code base.
|
43 |
|
44 | ```js
|
45 | import {
|
46 | graphql,
|
47 | GraphQLSchema,
|
48 | GraphQLObjectType,
|
49 | GraphQLString,
|
50 | } from 'graphql';
|
51 |
|
52 | var 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 |
|
67 | This defines a simple schema with one type and one field, that resolves
|
68 | to a fixed value. The `resolve` function can return a value, a promise,
|
69 | or an array of promises. A more complex example is included in the top
|
70 | level [tests](src/__tests__) directory.
|
71 |
|
72 | Then, serve the result of a query against that type schema.
|
73 |
|
74 | ```js
|
75 | var query = '{ hello }';
|
76 |
|
77 | graphql(schema, query).then((result) => {
|
78 | // Prints
|
79 | // {
|
80 | // data: { hello: "world" }
|
81 | // }
|
82 | console.log(result);
|
83 | });
|
84 | ```
|
85 |
|
86 | This runs a query fetching the one field defined. The `graphql` function will
|
87 | first ensure the query is syntactically and semantically valid before executing
|
88 | it, reporting errors otherwise.
|
89 |
|
90 | ```js
|
91 | var query = '{ BoyHowdy }';
|
92 |
|
93 | graphql(schema, query).then((result) => {
|
94 | // Prints
|
95 | // {
|
96 | // errors: [
|
97 | // { message: 'Cannot query field BoyHowdy on RootQueryType',
|
98 | // locations: [ { line: 1, column: 3 } ] }
|
99 | // ]
|
100 | // }
|
101 | console.log(result);
|
102 | });
|
103 | ```
|
104 |
|
105 | **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.
|
106 |
|
107 | ### Want to ride the bleeding edge?
|
108 |
|
109 | The `npm` branch in this repository is automatically maintained to be the last
|
110 | commit to `master` to pass all tests, in the same form found on npm. It is
|
111 | recommended to use builds deployed to npm for many reasons, but if you want to use
|
112 | the latest not-yet-released version of graphql-js, you can do so by depending
|
113 | directly on this branch:
|
114 |
|
115 | ```
|
116 | npm install graphql@git://github.com/graphql/graphql-js.git#npm
|
117 | ```
|
118 |
|
119 | ### Using in a Browser
|
120 |
|
121 | GraphQL.js is a general purpose library and can be used both in a Node server
|
122 | and in the browser. As an example, the [GraphiQL](https://github.com/graphql/graphiql/)
|
123 | tool is built with GraphQL.js!
|
124 |
|
125 | Building a project using GraphQL.js with [webpack](https://webpack.js.org) or
|
126 | [rollup](https://github.com/rollup/rollup) should just work and only include
|
127 | the portions of the library you use. This works because GraphQL.js is distributed
|
128 | with both CommonJS (`require()`) and ESModule (`import`) files. Ensure that any
|
129 | custom build configurations look for `.mjs` files!
|
130 |
|
131 | ### Contributing
|
132 |
|
133 | We actively welcome pull requests, learn how to [contribute](./.github/CONTRIBUTING.md).
|
134 |
|
135 | ### Changelog
|
136 |
|
137 | Changes are tracked as [GitHub releases](https://github.com/graphql/graphql-js/releases).
|
138 |
|
139 | ### License
|
140 |
|
141 | GraphQL.js is [MIT-licensed](./LICENSE).
|
142 |
|
143 | ### Credits
|
144 |
|
145 | The `*.d.ts` files in this project are based on [DefinitelyTyped](https://github.com/DefinitelyTyped/DefinitelyTyped/tree/54712a7e28090c5b1253b746d1878003c954f3ff/types/graphql) definitions written by:
|
146 |
|
147 |
|
148 |
|
149 | - TonyYang https://github.com/TonyPythoneer
|
150 | - Caleb Meredith https://github.com/calebmer
|
151 | - Dominic Watson https://github.com/intellix
|
152 | - Firede https://github.com/firede
|
153 | - Kepennar https://github.com/kepennar
|
154 | - Mikhail Novikov https://github.com/freiksenet
|
155 | - Ivan Goncharov https://github.com/IvanGoncharov
|
156 | - Hagai Cohen https://github.com/DxCx
|
157 | - Ricardo Portugal https://github.com/rportugal
|
158 | - Tim Griesser https://github.com/tgriesser
|
159 | - Dylan Stewart https://github.com/dyst5422
|
160 | - Alessio Dionisi https://github.com/adnsio
|
161 | - Divyendu Singh https://github.com/divyenduz
|
162 | - Brad Zacher https://github.com/bradzacher
|
163 | - Curtis Layne https://github.com/clayne11
|
164 | - Jonathan Cardoso https://github.com/JCMais
|
165 | - Pavel Lang https://github.com/langpavel
|
166 | - Mark Caudill https://github.com/mc0
|
167 | - Martijn Walraven https://github.com/martijnwalraven
|
168 | - Jed Mao https://github.com/jedmao
|