Skip to content

Commit ff51604

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

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

website/pages/getting-started.mdx

Lines changed: 45 additions & 10 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,48 @@ 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

31-
```javascript
32-
let { graphql, buildSchema } = require('graphql');
33+
<Tabs items={['Template', 'Classes']}>
34+
<Tabs.Tab>
35+
```javascript
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 } `);
40+
41+
// The rootValue provides a resolver function for each API endpoint
42+
const rootValue = {
43+
hello() {
44+
return 'Hello world!';
45+
},
46+
};
47+
48+
// Run the GraphQL query '{ hello }' and print out the response
49+
graphql({
50+
schema,
51+
source: '{ hello }',
52+
rootValue,
53+
}).then((response) => {
54+
console.log(response);
55+
});
56+
57+
````
58+
</Tabs.Tab>
59+
<Tabs.Tab>
60+
```javascript
61+
const { graphql, buildSchema } = require('graphql');
62+
63+
// Construct a schema
64+
const schema = new GraphQLSchema({
65+
query: new GraphQLObjectType({
66+
name: 'Query',
67+
fields: {
68+
hello: { type: GraphQLString },
69+
},
70+
}),
71+
});
4072
4173
// The rootValue provides a resolver function for each API endpoint
42-
let rootValue = {
74+
const rootValue = {
4375
hello() {
4476
return 'Hello world!';
4577
},
@@ -53,7 +85,10 @@ graphql({
5385
}).then((response) => {
5486
console.log(response);
5587
});
56-
```
88+
````
89+
90+
</Tabs.Tab>
91+
</Tabs>
5792
5893
If you run this with:
5994

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ 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

810
```bash
@@ -11,6 +13,7 @@ npm install express graphql-http graphql --save
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+
1417
```js
1518
const express = require('express');
1619
const { createHandler } = require('graphql-http/lib/use/express');

0 commit comments

Comments
 (0)