Skip to content

Commit b036677

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

File tree

5 files changed

+253
-27
lines changed

5 files changed

+253
-27
lines changed

Diff for: src/type/schema.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ import type {
1414
} from '../language/ast';
1515
import { OperationTypeNode } from '../language/ast';
1616

17-
import type {
18-
GraphQLAbstractType,
19-
GraphQLInterfaceType,
20-
GraphQLNamedType,
17+
import {
2118
GraphQLObjectType,
22-
GraphQLType,
19+
type GraphQLAbstractType,
20+
type GraphQLInterfaceType,
21+
type GraphQLNamedType,
22+
type GraphQLType,
2323
} from './definition';
2424
import {
2525
getNamedType,

Diff for: website/pages/basic-types.mdx

+57
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
title: Basic Types
33
---
44

5+
import { Tabs } from 'nextra/components';
6+
57
In most situations, all you need to do is to specify the types for your API using the GraphQL schema language, taken as an argument to the `buildSchema` function.
68

79
The GraphQL schema language supports the scalar types of `String`, `Int`, `Float`, `Boolean`, and `ID`, so you can use these directly in the schema you pass to `buildSchema`.
@@ -12,6 +14,8 @@ To use a list type, surround the type in square brackets, so `[Int]` is a list o
1214

1315
Each of these types maps straightforwardly to JavaScript, so you can just return plain old JavaScript objects in APIs that return these types. Here's an example that shows how to use some of these basic types:
1416

17+
<Tabs items={['Template', 'Classes']}>
18+
<Tabs.Tab>
1519
```js
1620
const express = require('express');
1721
const { createHandler } = require('graphql-http/lib/use/express');
@@ -50,6 +54,59 @@ app.all(
5054
app.listen(4000);
5155
console.log('Running a GraphQL API server at localhost:4000/graphql');
5256
```
57+
</Tabs.Tab>
58+
<Tabs.Tab>
59+
```js
60+
const express = require('express');
61+
const { createHandler } = require('graphql-http/lib/use/express');
62+
const {
63+
GraphQLObjectType,
64+
GraphQLSchema,
65+
GraphQLString,
66+
GraphQLFloat,
67+
GraphQLList,
68+
} = require('graphql');
69+
70+
// Construct a schema
71+
const schema = new GraphQLSchema({
72+
query: new GraphQLObjectType({
73+
name: 'Query',
74+
fields: {
75+
quoteOfTheDay: { type: GraphQLString },
76+
random: { type: GraphQLFloat },
77+
rollThreeDice: { type: new GraphQLList(GraphQLFloat) },
78+
},
79+
}),
80+
});
81+
82+
// The root provides a resolver function for each API endpoint
83+
const root = {
84+
quoteOfTheDay() {
85+
return Math.random() < 0.5 ? 'Take it easy' : 'Salvation lies within';
86+
},
87+
random() {
88+
return Math.random();
89+
},
90+
rollThreeDice() {
91+
return [1, 2, 3].map((_) => 1 + Math.floor(Math.random() * 6));
92+
},
93+
};
94+
95+
const app = express();
96+
97+
app.all(
98+
'/graphql',
99+
createHandler({
100+
schema: schema,
101+
rootValue: root,
102+
}),
103+
);
104+
105+
app.listen(4000);
106+
console.log('Running a GraphQL API server at localhost:4000/graphql');
107+
```
108+
</Tabs.Tab>
109+
</Tabs>
53110

54111
If you run this code with `node server.js` and browse to http://localhost:4000/graphql you can try out these APIs.
55112

Diff for: website/pages/getting-started.mdx

+42-9
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

Diff for: website/pages/passing-arguments.mdx

+95
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
title: Passing Arguments
33
---
44

5+
import { Tabs } from 'nextra/components';
6+
57
Just like a REST API, it's common to pass arguments to an endpoint in a GraphQL API. By defining the arguments in the schema language, typechecking happens automatically. Each argument must be named and have a type. For example, in the [Basic Types documentation](/basic-types/) we had an endpoint called `rollThreeDice`:
68

79
```graphql
@@ -12,11 +14,44 @@ type Query {
1214

1315
Instead of hard codingthree”, we might want a more general function that rolls `numDice` dice, each of which have `numSides` sides. We can add arguments to the GraphQL schema language like this:
1416

17+
<Tabs items={['Template', 'Classes']}>
18+
<Tabs.Tab>
1519
```graphql
1620
type Query {
1721
rollDice(numDice: Int!, numSides: Int): [Int]
1822
}
1923
```
24+
</Tabs.Tab>
25+
<Tabs.Tab>
26+
```js
27+
const {
28+
GraphQLObjectType,
29+
GraphQLNonNull,
30+
GraphQLInt,
31+
GraphQLString,
32+
GraphQLList,
33+
GraphQLFloat,
34+
} = require('graphql');
35+
36+
new GraphQLObjectType({
37+
name: 'Query',
38+
fields: {
39+
rollDice: {
40+
type: new GraphQLList(GraphQLFloat),
41+
args: {
42+
numDice: {
43+
type: new GraphQLNonNull(GraphQLInt)
44+
},
45+
numSides: {
46+
type: new GraphQLNonNull(GraphQLInt)
47+
},
48+
},
49+
},
50+
},
51+
})
52+
```
53+
</Tabs.Tab>
54+
</Tabs>
2055

2156
The exclamation point in `Int!` indicates that `numDice` can't be null, which means we can skip a bit of validation logic to make our server code simpler. We can let `numSides` be null and assume that by default a die has 6 sides.
2257

@@ -52,6 +87,8 @@ If you're familiar with destructuring, this is a bit nicer because the line of c
5287

5388
The entire code for a server that hosts this `rollDice` API is:
5489

90+
<Tabs items={['Template', 'Classes']}>
91+
<Tabs.Tab>
5592
```js
5693
const express = require('express');
5794
const { createHandler } = require('graphql-http/lib/use/express');
@@ -86,6 +123,64 @@ app.all(
86123
app.listen(4000);
87124
console.log('Running a GraphQL API server at localhost:4000/graphql');
88125
```
126+
</Tabs.Tab>
127+
<Tabs.Tab>
128+
```js
129+
const express = require('express');
130+
const { createHandler } = require('graphql-http/lib/use/express');
131+
const {
132+
GraphQLObjectType,
133+
GraphQLNonNull,
134+
GraphQLInt,
135+
GraphQLString,
136+
GraphQLList,
137+
GraphQLFloat,
138+
} = require('graphql');
139+
140+
// Construct a schema, using GraphQL schema language
141+
const schema = new GraphQLSchema({
142+
query: new GraphQLObjectType({
143+
name: 'Query',
144+
fields: {
145+
rollDice: {
146+
type: new GraphQLList(GraphQLFloat),
147+
args: {
148+
numDice: {
149+
type: new GraphQLNonNull(GraphQLInt)
150+
},
151+
numSides: {
152+
type: new GraphQLNonNull(GraphQLInt)
153+
},
154+
},
155+
},
156+
},
157+
})
158+
})
159+
160+
// The root provides a resolver function for each API endpoint
161+
const root = {
162+
rollDice({ numDice, numSides }) {
163+
const output = [];
164+
for (const i = 0; i < numDice; i++) {
165+
output.push(1 + Math.floor(Math.random() * (numSides || 6)));
166+
}
167+
return output;
168+
},
169+
};
170+
171+
const app = express();
172+
app.all(
173+
'/graphql',
174+
createHandler({
175+
schema: schema,
176+
rootValue: root,
177+
}),
178+
);
179+
app.listen(4000);
180+
console.log('Running a GraphQL API server at localhost:4000/graphql');
181+
```
182+
</Tabs.Tab>
183+
</Tabs>
89184

90185
When you call this API, you have to pass each argument by name. So for the server above, you could issue this GraphQL query to roll three six-sided dice:
91186

Diff for: website/pages/running-an-express-graphql-server.mdx

+54-13
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@ 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

14-
```js
15-
const express = require('express');
16-
const { createHandler } = require('graphql-http/lib/use/express');
16+
<Tabs items={['Template', 'Classes']}>
17+
<Tabs.Tab>
18+
```javascript
1719
const { buildSchema } = require('graphql');
20+
const { createHandler } = require('graphql-http/lib/use/express');
21+
const express = require('express');
1822

1923
// Construct a schema, using GraphQL schema language
20-
const schema = buildSchema(`
21-
type Query {
22-
hello: String
23-
}
24-
`);
25-
26-
// The root provides a resolver function for each API endpoint
27-
const root = {
24+
const schema = buildSchema(`type Query { hello: String } `);
25+
26+
// The rootValue provides a resolver function for each API endpoint
27+
const rootValue = {
2828
hello() {
2929
return 'Hello world!';
3030
},
@@ -44,7 +44,48 @@ app.all(
4444
// Start the server at port
4545
app.listen(4000);
4646
console.log('Running a GraphQL API server at http://localhost:4000/graphql');
47-
```
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>
4889
4990
You can run this GraphQL server with:
5091

0 commit comments

Comments
 (0)