Skip to content

Commit 076abdb

Browse files
committed
Provide people with tabs so they can use classes as well
1 parent e9b6b62 commit 076abdb

File tree

2 files changed

+119
-10
lines changed

2 files changed

+119
-10
lines changed

website/pages/getting-started.mdx

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ title: Getting Started With GraphQL.js
33
sidebarTitle: Getting Started
44
---
55

6+
import { Tabs } from 'nextra/components';
7+
68
{/* title can be removed in Nextra 4, since sidebar title will take from first h1 */}
79

810
# Getting Started With GraphQL.js
@@ -19,7 +21,7 @@ and arrow functions, so if you aren't familiar with them you might want to read
1921
2022
To create a new project and install GraphQL.js in your current directory:
2123

22-
```bash
24+
```sh npm2yarn
2325
npm init
2426
npm install graphql --save
2527
```
@@ -28,18 +30,16 @@ npm install graphql --save
2830

2931
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`:
3032

33+
<Tabs items={['Template', 'Classes']}>
34+
<Tabs.Tab>
3135
```javascript
32-
let { graphql, buildSchema } = require('graphql');
36+
const { graphql, buildSchema } = require('graphql');
3337

3438
// Construct a schema, using GraphQL schema language
35-
let schema = buildSchema(`
36-
type Query {
37-
hello: String
38-
}
39-
`);
39+
const schema = buildSchema(`type Query { hello: String } `);
4040

4141
// The rootValue provides a resolver function for each API endpoint
42-
let rootValue = {
42+
const rootValue = {
4343
hello() {
4444
return 'Hello world!';
4545
},
@@ -53,7 +53,40 @@ graphql({
5353
}).then((response) => {
5454
console.log(response);
5555
});
56-
```
56+
````
57+
</Tabs.Tab>
58+
<Tabs.Tab>
59+
```javascript
60+
const { graphql, GraphQLSchema, GraphQLObjectType } = require('graphql');
61+
62+
// Construct a schema
63+
const schema = new GraphQLSchema({
64+
query: new GraphQLObjectType({
65+
name: 'Query',
66+
fields: {
67+
hello: { type: GraphQLString },
68+
},
69+
}),
70+
});
71+
72+
// The rootValue provides a resolver function for each API endpoint
73+
const rootValue = {
74+
hello() {
75+
return 'Hello world!';
76+
},
77+
};
78+
79+
// Run the GraphQL query '{ hello }' and print out the response
80+
graphql({
81+
schema,
82+
source: '{ hello }',
83+
rootValue,
84+
}).then((response) => {
85+
console.log(response);
86+
});
87+
````
88+
</Tabs.Tab>
89+
</Tabs>
5790
5891
If you run this with:
5992

website/pages/running-an-express-graphql-server.mdx

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,90 @@ title: Running an Express GraphQL Server
33
sidebarTitle: Running Express + GraphQL
44
---
55

6+
import { Tabs } from 'nextra/components';
7+
68
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:
79

8-
```bash
10+
```sh npm2yarn
911
npm install express graphql-http graphql --save
1012
```
1113

1214
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:
1315

16+
<Tabs items={['Template', 'Classes']}>
17+
<Tabs.Tab>
18+
```javascript
19+
const { buildSchema } = require('graphql');
20+
const { createHandler } = require('graphql-http/lib/use/express');
21+
const express = require('express');
22+
23+
// Construct a schema, using GraphQL schema language
24+
const schema = buildSchema(`type Query { hello: String } `);
25+
26+
// The rootValue provides a resolver function for each API endpoint
27+
const rootValue = {
28+
hello() {
29+
return 'Hello world!';
30+
},
31+
};
32+
33+
const app = express();
34+
35+
// Create and use the GraphQL handler.
36+
app.all(
37+
'/graphql',
38+
createHandler({
39+
schema: schema,
40+
rootValue: root,
41+
}),
42+
);
43+
44+
// Start the server at port
45+
app.listen(4000);
46+
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
47+
````
48+
</Tabs.Tab>
49+
<Tabs.Tab>
50+
```javascript
51+
const { GraphQLObjectType, GraphQLSchema } = require('graphql');
52+
const { createHandler } = require('graphql-http/lib/use/express');
53+
const express = require('express');
54+
55+
// Construct a schema
56+
const schema = new GraphQLSchema({
57+
query: new GraphQLObjectType({
58+
name: 'Query',
59+
fields: {
60+
hello: { type: GraphQLString },
61+
},
62+
}),
63+
});
64+
65+
// The rootValue provides a resolver function for each API endpoint
66+
const rootValue = {
67+
hello() {
68+
return 'Hello world!';
69+
},
70+
};
71+
72+
const app = express();
73+
74+
// Create and use the GraphQL handler.
75+
app.all(
76+
'/graphql',
77+
createHandler({
78+
schema: schema,
79+
rootValue: root,
80+
}),
81+
);
82+
83+
// Start the server at port
84+
app.listen(4000);
85+
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
86+
````
87+
</Tabs.Tab>
88+
</Tabs>
89+
1490
```js
1591
const express = require('express');
1692
const { createHandler } = require('graphql-http/lib/use/express');

0 commit comments

Comments
 (0)