Skip to content

Commit 401dcac

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

8 files changed

+737
-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/authentication-and-express-middleware.mdx

+50
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ title: Authentication and Express Middleware
33
sidebarTitle: Authentication & Middleware
44
---
55

6+
import { Tabs } from 'nextra/components'
7+
68
It's simple to use any Express middleware in conjunction with `graphql-http`. In particular, this is a great pattern for handling authentication.
79

810
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.
911

1012
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:
1113

14+
<Tabs items={['Template', 'Classes']}>
15+
<Tabs.Tab>
1216
```js
1317
const express = require('express');
1418
const { createHandler } = require('graphql-http/lib/use/express');
@@ -46,6 +50,52 @@ app.all(
4650
app.listen(4000);
4751
console.log('Running a GraphQL API server at localhost:4000/graphql');
4852
```
53+
</Tabs.Tab>
54+
<Tabs.Tab>
55+
```js
56+
const express = require('express');
57+
const { createHandler } = require('graphql-http/lib/use/express');
58+
const {
59+
GraphQLObjectType,
60+
GraphQLSchema,
61+
GraphQLString,
62+
} = require('graphql');
63+
64+
const schema = new GraphQLSchema({
65+
query: new GraphQLObjectType({
66+
name: 'Query',
67+
fields: { ip: { type: GraphQLString } },
68+
}),
69+
});
70+
71+
function loggingMiddleware(req, res, next) {
72+
console.log('ip:', req.ip);
73+
next();
74+
}
75+
76+
const root = {
77+
ip(args, context) {
78+
return context.ip;
79+
},
80+
};
81+
82+
const app = express();
83+
app.use(loggingMiddleware);
84+
app.all(
85+
'/graphql',
86+
createHandler({
87+
schema: schema,
88+
rootValue: root,
89+
context: (req) => ({
90+
ip: req.raw.ip,
91+
}),
92+
}),
93+
);
94+
app.listen(4000);
95+
console.log('Running a GraphQL API server at localhost:4000/graphql');
96+
```
97+
</Tabs.Tab>
98+
</Tabs>
4999

50100
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`.
51101

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

0 commit comments

Comments
 (0)