|
1 | 1 | ---
|
2 |
| -title: Getting Started With GraphQL.js |
3 |
| -sidebarTitle: Getting Started |
| 2 | +title: Overview |
| 3 | +sidebarTitle: Overview |
4 | 4 | ---
|
5 | 5 |
|
6 |
| -{/* title can be removed in Nextra 4, since sidebar title will take from first h1 */} |
| 6 | +GraphQL.JS is the reference implementation to the [GraphQL Specification](https://spec.graphql.org/draft/), it's designed to be simple to use and easy to understand |
| 7 | +while closely following the Specification. |
7 | 8 |
|
8 |
| -# Getting Started With GraphQL.js |
| 9 | +You can build GraphQL servers, clients, and tools with this library, it's designed so you can choose which parts you use, for example, you can build your own parser |
| 10 | +and use the execution/validation from the library. There also a lot of useful utilities for schema-diffing, working with arguments and [many more](./utilities.mdx). |
9 | 11 |
|
10 |
| -## Prerequisites |
| 12 | +In the following chapters you'll find out more about the three critical pieces of this library |
11 | 13 |
|
12 |
| -Before getting started, you should have Node v6 installed, although the examples should mostly work in previous versions of Node as well. |
13 |
| -For this guide, we won't use any language features that require transpilation, but we will use some ES6 features like |
14 |
| -[Promises](http://www.html5rocks.com/en/tutorials/es6/promises/), classes, |
15 |
| -and arrow functions, so if you aren't familiar with them you might want to read up on them first. |
| 14 | +- The GraphQL language |
| 15 | +- Document validation |
| 16 | +- GraphQL Execution |
16 | 17 |
|
17 |
| -To create a new project and install GraphQL.js in your current directory: |
18 |
| - |
19 |
| -```bash |
20 |
| -npm init |
21 |
| -npm install graphql --save |
22 |
| -``` |
23 |
| - |
24 |
| -## Writing Code |
25 |
| - |
26 |
| -To handle GraphQL queries, we need a schema that defines the `Query` type, and we need an API root with a function called a “resolver” for each API endpoint. For an API that just returns “Hello world!”, we can put this code in a file named `server.js`: |
27 |
| - |
28 |
| -```javascript |
29 |
| -let { graphql, buildSchema } = require('graphql'); |
30 |
| - |
31 |
| -// Construct a schema, using GraphQL schema language |
32 |
| -let schema = buildSchema(` |
33 |
| - type Query { |
34 |
| - hello: String |
35 |
| - } |
36 |
| -`); |
37 |
| - |
38 |
| -// The rootValue provides a resolver function for each API endpoint |
39 |
| -let rootValue = { |
40 |
| - hello() { |
41 |
| - return 'Hello world!'; |
42 |
| - }, |
43 |
| -}; |
44 |
| - |
45 |
| -// Run the GraphQL query '{ hello }' and print out the response |
46 |
| -graphql({ |
47 |
| - schema, |
48 |
| - source: '{ hello }', |
49 |
| - rootValue, |
50 |
| -}).then((response) => { |
51 |
| - console.log(JSON.stringify(response, null, 2)); |
52 |
| -}); |
53 |
| -``` |
54 |
| - |
55 |
| -If you run this with: |
56 |
| - |
57 |
| -```sh |
58 |
| -node server.js |
59 |
| -``` |
60 |
| - |
61 |
| -You should see the GraphQL response printed out: |
62 |
| - |
63 |
| -```json |
64 |
| -{ |
65 |
| - "data": { |
66 |
| - "hello": "Hello world!" |
67 |
| - } |
68 |
| -} |
69 |
| -``` |
70 |
| - |
71 |
| -Congratulations - you just executed a GraphQL query! |
72 |
| - |
73 |
| -For practical applications, you'll probably want to run GraphQL queries from an API server, rather than executing GraphQL with a command line tool. To use GraphQL for an API server over HTTP, check out [Running an Express GraphQL Server](/running-an-express-graphql-server/). |
| 18 | +You can also code along on [a tutorial](./getting-started.mdx). |
0 commit comments