UNPKG

4.16 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://travis-ci.org/graphql/graphql-js.svg?branch=master)](https://travis-ci.org/graphql/graphql-js?branch=master)
7[![Coverage Status](https://coveralls.io/repos/graphql/graphql-js/badge.svg?branch=master)](https://coveralls.io/r/graphql/graphql-js?branch=master)
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
15## Getting Started
16
17An overview of GraphQL in general is available in the
18[README](https://github.com/facebook/graphql/blob/master/README.md) for the
19[Specification for GraphQL](https://github.com/facebook/graphql). That overview
20describes a simple set of GraphQL examples that exist as [tests](src/__tests__)
21in this repository. A good way to get started with this repository is to walk
22through that README and the corresponding tests in parallel.
23
24### Using GraphQL.js
25
26Install GraphQL.js from npm
27
28With yarn:
29
30```sh
31yarn add graphql
32```
33
34or alternatively using npm:
35
36```sh
37npm install --save graphql
38```
39
40GraphQL.js provides two important capabilities: building a type schema, and
41serving queries against that type schema.
42
43First, build a GraphQL type schema which maps to your code base.
44
45```js
46import {
47 graphql,
48 GraphQLSchema,
49 GraphQLObjectType,
50 GraphQLString
51} from 'graphql';
52
53var schema = new GraphQLSchema({
54 query: new GraphQLObjectType({
55 name: 'RootQueryType',
56 fields: {
57 hello: {
58 type: GraphQLString,
59 resolve() {
60 return 'world';
61 }
62 }
63 }
64 })
65});
66```
67
68This defines a simple schema with one type and one field, that resolves
69to a fixed value. The `resolve` function can return a value, a promise,
70or an array of promises. A more complex example is included in the top
71level [tests](src/__tests__) directory.
72
73Then, serve the result of a query against that type schema.
74
75```js
76var query = '{ hello }';
77
78graphql(schema, query).then(result => {
79
80 // Prints
81 // {
82 // data: { hello: "world" }
83 // }
84 console.log(result);
85
86});
87```
88
89This runs a query fetching the one field defined. The `graphql` function will
90first ensure the query is syntactically and semantically valid before executing
91it, reporting errors otherwise.
92
93```js
94var query = '{ boyhowdy }';
95
96graphql(schema, query).then(result => {
97
98 // Prints
99 // {
100 // errors: [
101 // { message: 'Cannot query field boyhowdy on RootQueryType',
102 // locations: [ { line: 1, column: 3 } ] }
103 // ]
104 // }
105 console.log(result);
106
107});
108```
109
110### Want to ride the bleeding edge?
111
112The `npm` branch in this repository is automatically maintained to be the last
113commit to `master` to pass all tests, in the same form found on npm. It is
114recommended to use builds deployed to npm for many reasons, but if you want to use
115the latest not-yet-released version of graphql-js, you can do so by depending
116directly on this branch:
117
118```
119npm install graphql@git://github.com/graphql/graphql-js.git#npm
120```
121
122### Using in a Browser
123
124GraphQL.js is a general purpose library and can be used both in a Node server
125and in the browser. As an example, the [GraphiQL](https://github.com/graphql/graphiql/)
126tool is built with GraphQL.js!
127
128Building a project using GraphQL.js with [webpack](https://webpack.js.org) or
129[rollup](https://github.com/rollup/rollup) should just work and only include
130the portions of the library you use. This works because GraphQL.js is distributed
131with both CommonJS (`require()`) and ESModule (`import`) files. Ensure that any
132custom build configurations look for `.mjs` files!
133
134### Contributing
135
136We actively welcome pull requests, learn how to
137[contribute](https://github.com/graphql/graphql-js/blob/master/.github/CONTRIBUTING.md).
138
139### Changelog
140
141Changes are tracked as [GitHub releases](https://github.com/graphql/graphql-js/releases).
142
143### License
144
145GraphQL.js is [MIT-licensed](https://github.com/graphql/graphql-js/blob/master/LICENSE).