Skip to content

Commit 7a50589

Browse files
enisdenjobenjie
andauthored
Recommend graphql-http instead of deprecated express-graphql (#1616)
* graphql-http * Update src/content/graphql-js/APIReference-GraphQLHTTP.md Co-authored-by: Benjie <[email protected]> * Update src/content/graphql-js/Guides-ConstructingTypes.md Co-authored-by: Benjie <[email protected]> * more explain * ruru it is * unused img --------- Co-authored-by: Benjie <[email protected]>
1 parent 756acd1 commit 7a50589

15 files changed

+92
-99
lines changed

notes/ContributingToCodePage.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ Here's an optimal example of what we're looking for:
3939
name: Express GraphQL
4040
description: The reference implementation of a GraphQL API server over an Express webserver. You can use this to run GraphQL in conjunction with a regular Express webserver, or as a standalone GraphQL server.
4141
url: /graphql-js/running-an-express-graphql-server/
42-
github: graphql/express-graphql
43-
npm: "express-graphql"
42+
github: graphql/graphql-http
43+
npm: "graphql-http"
4444
---
4545

46-
To run an `express-graphql` hello world server:
46+
To run an `graphql-http` hello world server:
4747

4848
```bash
49-
npm install express express-graphql graphql
49+
npm install express graphql-http graphql
5050
```
5151

5252
Then run `node server.js` with this code in `server.js`:
5353

5454
```js
5555
var express = require("express")
56-
var { graphqlHTTP } = require("express-graphql")
56+
var { createHandler } = require("graphql-http/lib/use/express")
5757
var { buildSchema } = require("graphql")
5858

5959
var schema = buildSchema(`
@@ -65,12 +65,11 @@ var schema = buildSchema(`
6565
var root = { hello: () => "Hello world!" }
6666

6767
var app = express()
68-
app.use(
68+
app.all(
6969
"/graphql",
70-
graphqlHTTP({
70+
createHandler({
7171
schema: schema,
7272
rootValue: root,
73-
graphiql: true,
7473
})
7574
)
7675
app.listen(4000, () => console.log("Now browse to localhost:4000/graphql"))

src/content/faq/Specification.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The entire process behind each release is open source. You can monitor specifica
1515

1616
GraphQL is still evolving and contributions are very welcome! The specification (including the [latest working draft](https://spec.graphql.org/)) is open source. [Contributor guidelines](https://github.com/graphql/graphql-spec/blob/main/CONTRIBUTING.md) are available on GitHub.
1717

18-
There are more ways to get involved with GraphQL beyond the specification though. Updating the content on [this website and the documentation](https://github.com/graphql/graphql.github.io), for example. Or contributing to [graphql-js](https://github.com/graphql/graphql-js), [express-graphql](https://github.com/graphql/express-graphql), [GraphiQL](https://github.com/graphql/graphiql), or [one of the many other projects](https://github.com/graphql/) maintained by the [GraphQL Foundation](#what-is-the-graphql-foundation).
18+
There are more ways to get involved with GraphQL beyond the specification though. Updating the content on [this website and the documentation](https://github.com/graphql/graphql.github.io), for example. Or contributing to [graphql-js](https://github.com/graphql/graphql-js), [graphql-http](https://github.com/graphql/graphql-http), [GraphiQL](https://github.com/graphql/graphiql), or [one of the many other projects](https://github.com/graphql/) maintained by the [GraphQL Foundation](#what-is-the-graphql-foundation).
1919

2020
### What is GraphQL Specification membership?
2121

src/content/graphql-js/APIReference-ExpressGraphQL.md

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
title: graphql-http
3+
layout: docs
4+
category: API Reference
5+
permalink: /graphql-js/graphql-http/
6+
sublinks: createHandler
7+
next: /graphql-js/graphql/
8+
---
9+
10+
The [official `graphql-http` package](https://github.com/graphql/graphql-http) provides a simple way to create a fully compliant GraphQL server. It has a handler for Node.js native [`http`](https://nodejs.org/api/http.html), together with handlers for well-known frameworks like [Express](https://expressjs.com/), [Fastify](https://www.fastify.io/) and [Koa](https://koajs.com/); as well as handlers for different runtimes like [Deno](https://deno.land/) and [Bun](https://bun.sh/).
11+
12+
## Express
13+
14+
```js
15+
import { createHandler } from "graphql-http/lib/use/express" // ES6
16+
const { createHandler } = require("graphql-http/lib/use/express") // CommonJS
17+
```
18+
19+
### createHandler
20+
21+
```js
22+
createHandler({
23+
schema: GraphQLSchema,
24+
rootValue?: ?any,
25+
context?: ?any,
26+
formatError?: ?Function,
27+
validationRules?: ?Array<any>,
28+
}): Handler
29+
```
30+
31+
Constructs an Express handler based on a GraphQL schema.
32+
33+
See the [tutorial](/graphql-js/running-an-express-graphql-server/) for sample usage.
34+
35+
See the [GitHub README](https://github.com/graphql/graphql-http) for more extensive documentation, including how to use `graphql-http` with other server frameworks and runtimes.

src/content/graphql-js/Guides-ConstructingTypes.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Constructing Types
33
layout: docs
44
category: Advanced Guides
55
permalink: /graphql-js/constructing-types/
6-
next: /graphql-js/express-graphql/
6+
next: /graphql-js/graphql-http/
77
---
88

99
For many apps, you can define a fixed schema when the application starts, and define it using GraphQL schema language. In some cases, it's useful to construct a schema programmatically. You can do this using the `GraphQLSchema` constructor.
@@ -14,7 +14,7 @@ For example, let's say we are building a simple API that lets you fetch user dat
1414

1515
```javascript
1616
var express = require("express")
17-
var { graphqlHTTP } = require("express-graphql")
17+
var { createHandler } = require("graphql-http/lib/use/express")
1818
var { buildSchema } = require("graphql")
1919

2020
var schema = buildSchema(`
@@ -47,12 +47,11 @@ var root = {
4747
}
4848

4949
var app = express()
50-
app.use(
50+
app.all(
5151
"/graphql",
52-
graphqlHTTP({
52+
createHandler({
5353
schema: schema,
5454
rootValue: root,
55-
graphiql: true,
5655
})
5756
)
5857
app.listen(4000)
@@ -63,7 +62,7 @@ We can implement this same API without using GraphQL schema language:
6362

6463
```javascript
6564
var express = require("express")
66-
var { graphqlHTTP } = require("express-graphql")
65+
var { createHandler } = require("graphql-http/lib/use/express")
6766
var graphql = require("graphql")
6867

6968
// Maps id to User object
@@ -107,11 +106,10 @@ var queryType = new graphql.GraphQLObjectType({
107106
var schema = new graphql.GraphQLSchema({ query: queryType })
108107

109108
var app = express()
110-
app.use(
109+
app.all(
111110
"/graphql",
112-
graphqlHTTP({
111+
createHandler({
113112
schema: schema,
114-
graphiql: true,
115113
})
116114
)
117115
app.listen(4000)

src/content/graphql-js/Tutorial-Authentication.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ permalink: /graphql-js/authentication-and-express-middleware/
77
next: /graphql-js/constructing-types/
88
---
99

10-
It's simple to use any Express middleware in conjunction with `express-graphql`. In particular, this is a great pattern for handling authentication.
10+
It's simple to use any Express middleware in conjunction with `graphql-http`. In particular, this is a great pattern for handling authentication.
1111

1212
To use middleware with a GraphQL resolver, just use the middleware like you would with a normal Express app. The `request` object is then available as the second argument in any resolver.
1313

1414
For example, let's say we wanted our server to log the IP address of every request, and we also want to write an API that returns the IP address of the caller. We can do the former with middleware, and the latter by accessing the `request` object in a resolver. Here's server code that implements this:
1515

1616
```javascript
1717
var express = require("express")
18-
var { graphqlHTTP } = require("express-graphql")
18+
var { createHandler } = require("graphql-http/lib/use/express")
1919
var { buildSchema } = require("graphql")
2020

2121
var schema = buildSchema(`
@@ -37,9 +37,9 @@ var root = {
3737

3838
var app = express()
3939
app.use(loggingMiddleware)
40-
app.use(
40+
app.all(
4141
"/graphql",
42-
graphqlHTTP({
42+
createHandler({
4343
schema: schema,
4444
rootValue: root,
4545
graphiql: true,
@@ -49,7 +49,7 @@ app.listen(4000)
4949
console.log("Running a GraphQL API server at localhost:4000/graphql")
5050
```
5151

52-
In a REST API, authentication is often handled with a header, that contains an auth token which proves what user is making this request. Express middleware processes these headers and puts authentication data on the Express `request` object. Some middleware modules that handle authentication like this are [Passport](http://passportjs.org/), [express-jwt](https://github.com/auth0/express-jwt), and [express-session](https://github.com/expressjs/session). Each of these modules works with `express-graphql`.
52+
In a REST API, authentication is often handled with a header, that contains an auth token which proves what user is making this request. Express middleware processes these headers and puts authentication data on the Express `request` object. Some middleware modules that handle authentication like this are [Passport](http://passportjs.org/), [express-jwt](https://github.com/auth0/express-jwt), and [express-session](https://github.com/expressjs/session). Each of these modules works with `graphql-http`.
5353

5454
If you aren't familiar with any of these authentication mechanisms, we recommend using `express-jwt` because it's simple without sacrificing any future flexibility.
5555

src/content/graphql-js/Tutorial-BasicTypes.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Each of these types maps straightforwardly to JavaScript, so you can just return
1818

1919
```javascript
2020
var express = require("express")
21-
var { graphqlHTTP } = require("express-graphql")
21+
var { createHandler } = require("graphql-http/lib/use/express")
2222
var { buildSchema } = require("graphql")
2323

2424
// Construct a schema, using GraphQL schema language
@@ -44,12 +44,11 @@ var root = {
4444
}
4545

4646
var app = express()
47-
app.use(
47+
app.all(
4848
"/graphql",
49-
graphqlHTTP({
49+
createHandler({
5050
schema: schema,
5151
rootValue: root,
52-
graphiql: true,
5352
})
5453
)
5554
app.listen(4000)

src/content/graphql-js/Tutorial-ExpressGraphQL.md

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,16 @@ next: /graphql-js/graphql-clients/
1010
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:
1111

1212
```bash
13-
npm install express express-graphql graphql --save
13+
npm install express graphql-http graphql ruru --save
1414
```
1515

16-
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 `express-graphql` library to mount a GraphQL API server on the “/graphql” HTTP endpoint:
16+
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:
1717

1818
```javascript
1919
var express = require("express")
20-
var { graphqlHTTP } = require("express-graphql")
20+
var { createHandler } = require("graphql-http/lib/use/express")
2121
var { buildSchema } = require("graphql")
22+
var { ruruHTML } = require("ruru/server")
2223

2324
// Construct a schema, using GraphQL schema language
2425
var schema = buildSchema(`
@@ -35,14 +36,23 @@ var root = {
3536
}
3637

3738
var app = express()
38-
app.use(
39+
40+
// Create and use the GraphQL handler.
41+
app.all(
3942
"/graphql",
40-
graphqlHTTP({
43+
createHandler({
4144
schema: schema,
4245
rootValue: root,
43-
graphiql: true,
4446
})
4547
)
48+
49+
// Serve the GraphiQL IDE.
50+
app.get("/", (_req, res) => {
51+
res.type("html")
52+
res.end(ruruHTML({ endpoint: "/graphql" }))
53+
})
54+
55+
// Start the server at port
4656
app.listen(4000)
4757
console.log("Running a GraphQL API server at http://localhost:4000/graphql")
4858
```
@@ -53,10 +63,6 @@ You can run this GraphQL server with:
5363
node server.js
5464
```
5565

56-
Since we configured `graphqlHTTP` with `graphiql: true`, you can use the GraphiQL tool to manually issue GraphQL queries. If you navigate in a web browser to [http://localhost:4000/graphql](http://localhost:4000/graphql), you should see an interface that lets you enter queries. It should look like:
57-
58-
![hello world graphql example](/img/hello.png)
59-
60-
This screen shot shows the GraphQL query `{ hello }` being issued and giving a result of `{ data: { hello: 'Hello world!' } }`. GraphiQL is a great tool for debugging and inspecting a server, so we recommend running it whenever your application is in development mode.
66+
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.
6167

62-
At this point you have learned how to run a GraphQL server and how to use GraphiQL interface to issue queries. The next step is to learn how to [issue GraphQL queries from client code](/graphql-js/graphql-clients/).
68+
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/).

src/content/graphql-js/Tutorial-GraphQLClients.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ permalink: /graphql-js/graphql-clients/
66
next: /graphql-js/basic-types/
77
---
88

9-
Since a GraphQL API has more underlying structure than a REST API, there are more powerful clients like [Relay](https://facebook.github.io/relay/) which can automatically handle batching, caching, and other features. But you don't need a complex client to call a GraphQL server. With `express-graphql`, you can just send an HTTP POST request to the endpoint you mounted your GraphQL server on, passing the GraphQL query as the `query` field in a JSON payload.
9+
Since a GraphQL API has more underlying structure than a REST API, there are more powerful clients like [Relay](https://facebook.github.io/relay/) which can automatically handle batching, caching, and other features. But you don't need a complex client to call a GraphQL server. With `graphql-http`, you can just send an HTTP POST request to the endpoint you mounted your GraphQL server on, passing the GraphQL query as the `query` field in a JSON payload.
1010

1111
For example, let's say we mounted a GraphQL server on http://localhost:4000/graphql as in the example code for [running an Express GraphQL server](/graphql-js/running-an-express-graphql-server/), and we want to send the GraphQL query `{ hello }`. We can do this from the command line with `curl`. If you paste this into a terminal:
1212

src/content/graphql-js/Tutorial-Mutations.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Here's some runnable code that implements this schema, keeping the data in memor
7373

7474
```javascript
7575
var express = require("express")
76-
var { graphqlHTTP } = require("express-graphql")
76+
var { createHandler } = require("graphql-http/lib/use/express")
7777
var { buildSchema } = require("graphql")
7878

7979
// Construct a schema, using GraphQL schema language
@@ -136,12 +136,11 @@ var root = {
136136
}
137137

138138
var app = express()
139-
app.use(
139+
app.all(
140140
"/graphql",
141-
graphqlHTTP({
141+
createHandler({
142142
schema: schema,
143143
rootValue: root,
144-
graphiql: true,
145144
})
146145
)
147146
app.listen(4000, () => {

0 commit comments

Comments
 (0)