You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The simplest way to run a GraphQL API server is to use [Express](https://expressjs.com), a popular web application framework for Node.js. You will need to install two additional dependencies:
7
7
8
8
```bash
9
-
npm install express graphql-http graphql ruru --save
9
+
npm install express graphql-http graphql --save
10
10
```
11
11
12
12
Let's modify our “hello world” example so that it's an API server rather than a script that runs a single query. We can use the 'express' module to run a webserver, and instead of executing a query directly with the `graphql` function, we can use the `graphql-http` library to mount a GraphQL API server on the “/graphql” HTTP endpoint:
@@ -15,7 +15,6 @@ Let's modify our “hello world” example so that it's an API server rather tha
15
15
var express =require("express")
16
16
var { createHandler } =require("graphql-http/lib/use/express")
17
17
var { buildSchema } =require("graphql")
18
-
var { ruruHTML } =require("ruru/server")
19
18
20
19
// Construct a schema, using GraphQL schema language
21
20
var schema =buildSchema(`
@@ -42,12 +41,6 @@ app.all(
42
41
})
43
42
)
44
43
45
-
// Serve the GraphiQL IDE.
46
-
app.get("/", (_req, res) => {
47
-
res.type("html")
48
-
res.end(ruruHTML({ endpoint:"/graphql" }))
49
-
})
50
-
51
44
// Start the server at port
52
45
app.listen(4000)
53
46
console.log("Running a GraphQL API server at http://localhost:4000/graphql")
@@ -59,6 +52,23 @@ You can run this GraphQL server with:
59
52
node server.js
60
53
```
61
54
62
-
You can use the Graph_i_QL IDE tool to issue GraphQL queries directly in the browser. If you navigate to [http://localhost:4000](http://localhost:4000), you should see an interface that lets you enter queries.
55
+
## Using GraphiQL
56
+
57
+
[GraphiQL](https://github.com/graphql/graphiql) is GraphQL's IDE; a great way of querying and exploring your GraphQL API.
58
+
One easy way to add it to your server is via the MIT-licensed [ruru](https://github.com/graphile/crystal/blob/main/grafast/ruru/README.md) package which bundles a prebuilt GraphiQL with some popular enhancements.
59
+
To do so, install the `ruru` module with `npm install --save ruru` and then add the following to your `server.js` file, then restart the `node server.js` command:
60
+
61
+
```js
62
+
var { ruruHTML } =require("ruru/server")
63
+
64
+
// Serve the GraphiQL IDE.
65
+
app.get("/", (_req, res) => {
66
+
res.type("html")
67
+
res.end(ruruHTML({ endpoint:"/graphql" }))
68
+
})
69
+
```
70
+
71
+
If you navigate to [http://localhost:4000](http://localhost:4000), you should see an interface that lets you enter queries;
72
+
now you can use the GraphiQL IDE tool to issue GraphQL queries directly in the browser.
63
73
64
74
At this point you have learned how to run a GraphQL server. The next step is to learn how to [issue GraphQL queries from client code](/graphql-js/graphql-clients/).
0 commit comments