Skip to content

Commit 7a298ee

Browse files
committed
Thanks prettier
1 parent 4af2c5d commit 7a298ee

9 files changed

+222
-217
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
/denoDist
88
/websiteDist
99
/website/out
10+
/website/**/*.mdx
1011
.next

website/pages/authentication-and-express-middleware.mdx

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,37 @@ To use middleware with a GraphQL resolver, just use the middleware like you woul
1111

1212
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:
1313

14-
<Tabs items={['Template', 'Classes']}>
14+
<Tabs items={['SDL', 'Code']}>
1515
<Tabs.Tab>
1616
```js
1717
const express = require('express');
1818
const { createHandler } = require('graphql-http/lib/use/express');
1919
const { buildSchema } = require('graphql');
2020

21-
const schema = buildSchema(` type Query { ip: String }`);
21+
const schema = buildSchema(`type Query { ip: String }`);
2222

2323
function loggingMiddleware(req, res, next) {
24-
console.log('ip:', req.ip);
25-
next();
24+
console.log('ip:', req.ip);
25+
next();
2626
}
2727

2828
const root = {
29-
ip(args, context) {
30-
return context.ip;
31-
},
29+
ip(args, context) {
30+
return context.ip;
31+
},
3232
};
3333

3434
const app = express();
3535
app.use(loggingMiddleware);
3636
app.all(
37-
'/graphql',
38-
createHandler({
39-
schema: schema,
40-
rootValue: root,
41-
context: (req) => ({
42-
ip: req.raw.ip,
43-
}),
44-
}),
37+
'/graphql',
38+
createHandler({
39+
schema: schema,
40+
rootValue: root,
41+
context: (req) => ({
42+
ip: req.raw.ip,
43+
}),
44+
}),
4545
);
4646
app.listen(4000);
4747
console.log('Running a GraphQL API server at localhost:4000/graphql');

website/pages/basic-types.mdx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,36 +14,42 @@ To use a list type, surround the type in square brackets, so `[Int]` is a list o
1414

1515
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:
1616

17-
<Tabs items={['Template', 'Classes']}>
17+
<Tabs items={['SDL', 'Code']}>
1818
<Tabs.Tab>
1919
```js
2020
const express = require('express');
2121
const { createHandler } = require('graphql-http/lib/use/express');
2222
const { buildSchema } = require('graphql');
2323

2424
// Construct a schema, using GraphQL schema language
25-
const schema = buildSchema(` type Query { quoteOfTheDay: String random: Float! rollThreeDice: [Int] }`);
25+
const schema = buildSchema(`
26+
type Query {
27+
quoteOfTheDay: String
28+
random: Float!
29+
rollThreeDice: [Int]
30+
}
31+
`);
2632

2733
// The root provides a resolver function for each API endpoint
2834
const root = {
29-
quoteOfTheDay() {
30-
return Math.random() < 0.5 ? 'Take it easy' : 'Salvation lies within';
31-
},
32-
random() {
33-
return Math.random();
34-
},
35-
rollThreeDice() {
36-
return [1, 2, 3].map((\_) => 1 + Math.floor(Math.random() \* 6));
37-
},
35+
quoteOfTheDay() {
36+
return Math.random() < 0.5 ? 'Take it easy' : 'Salvation lies within';
37+
},
38+
random() {
39+
return Math.random();
40+
},
41+
rollThreeDice() {
42+
return [1, 2, 3].map((\_) => 1 + Math.floor(Math.random() \* 6));
43+
},
3844
};
3945

4046
const app = express();
4147
app.all(
42-
'/graphql',
43-
createHandler({
44-
schema: schema,
45-
rootValue: root,
46-
}),
48+
'/graphql',
49+
createHandler({
50+
schema: schema,
51+
rootValue: root,
52+
}),
4753
);
4854
app.listen(4000);
4955
console.log('Running a GraphQL API server at localhost:4000/graphql');

website/pages/constructing-types.mdx

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ When you are using the `GraphQLSchema` constructor to create a schema, instead o
1010

1111
For example, let's say we are building a simple API that lets you fetch user data for a few hardcoded users based on an id. Using `buildSchema` we could write a server with:
1212

13-
<Tabs items={['Template', 'Classes']}>
13+
<Tabs items={['SDL', 'Code']}>
1414
<Tabs.Tab>
1515
```js
1616
const express = require('express');
@@ -19,40 +19,40 @@ const { buildSchema } = require('graphql');
1919

2020
const schema = buildSchema(`
2121
type User {
22-
id: String
23-
name: String
22+
id: String
23+
name: String
2424
}
2525
2626
type Query {
27-
user(id: String): User
27+
user(id: String): User
2828
}
2929
`);
3030

3131
// Maps id to User object
3232
const fakeDatabase = {
33-
a: {
34-
id: 'a',
35-
name: 'alice',
36-
},
37-
b: {
38-
id: 'b',
39-
name: 'bob',
40-
},
33+
a: {
34+
id: 'a',
35+
name: 'alice',
36+
},
37+
b: {
38+
id: 'b',
39+
name: 'bob',
40+
},
4141
};
4242

4343
const root = {
44-
user({ id }) {
45-
return fakeDatabase[id];
46-
},
44+
user({ id }) {
45+
return fakeDatabase[id];
46+
},
4747
};
4848

4949
const app = express();
5050
app.all(
51-
'/graphql',
52-
createHandler({
53-
schema: schema,
54-
rootValue: root,
55-
}),
51+
'/graphql',
52+
createHandler({
53+
schema: schema,
54+
rootValue: root,
55+
}),
5656
);
5757
app.listen(4000);
5858
console.log('Running a GraphQL API server at localhost:4000/graphql');

website/pages/getting-started.mdx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ npm install graphql --save
3030

3131
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`:
3232

33-
<Tabs items={['Template', 'Classes']}>
33+
<Tabs items={['SDL', 'Code']}>
3434
<Tabs.Tab>
3535
```javascript
3636
const { graphql, buildSchema } = require('graphql');
@@ -40,18 +40,19 @@ const schema = buildSchema(`type Query { hello: String } `);
4040

4141
// The rootValue provides a resolver function for each API endpoint
4242
const rootValue = {
43-
hello() {
44-
return 'Hello world!';
45-
},
43+
hello() {
44+
return 'Hello world!';
45+
},
4646
};
4747

4848
// Run the GraphQL query '{ hello }' and print out the response
4949
graphql({
50-
schema,
51-
source: '{ hello }',
52-
rootValue,
53-
}).then((response) => {
54-
console.log(response);
50+
schema,
51+
source: '{ hello }',
52+
rootValue,
53+
}).then((response) => {
54+
console.log(response);
55+
});
5556
});
5657

5758
````

website/pages/mutations-and-input-types.mdx

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,16 @@ If you have an API endpoint that alters data, like inserting data into a databas
88

99
Let's say we have a “message of the day” server, where anyone can update the message of the day, and anyone can read the current one. The GraphQL schema for this is simply:
1010

11-
<Tabs items={['Template', 'Classes']}>
11+
<Tabs items={['SDL', 'Code']}>
1212
<Tabs.Tab>
1313
```graphql
1414
type Mutation {
1515
setMessage(message: String): String
1616
}
1717

1818
type Query {
19-
getMessage: String
19+
getMessage: String
2020
}
21-
2221
````
2322
</Tabs.Tab>
2423
<Tabs.Tab>
@@ -69,7 +68,7 @@ You don't need anything more than this to implement mutations. But in many cases
6968

7069
For example, instead of a single message of the day, let's say we have many messages, indexed in a database by the `id` field, and each message has both a `content` string and an `author` string. We want a mutation API both for creating a new message and for updating an old message. We could use the schema:
7170

72-
<Tabs items={['Template', 'Classes']}>
71+
<Tabs items={['SDL', 'Code']}>
7372
<Tabs.Tab>
7473
```graphql
7574
input MessageInput {
@@ -78,18 +77,18 @@ input MessageInput {
7877
}
7978

8079
type Message {
81-
id: ID!
82-
content: String
83-
author: String
80+
id: ID!
81+
content: String
82+
author: String
8483
}
8584

8685
type Query {
87-
getMessage(id: ID!): Message
86+
getMessage(id: ID!): Message
8887
}
8988

9089
type Mutation {
91-
createMessage(input: MessageInput): Message
92-
updateMessage(id: ID!, input: MessageInput): Message
90+
createMessage(input: MessageInput): Message
91+
updateMessage(id: ID!, input: MessageInput): Message
9392
}
9493

9594
````
@@ -166,80 +165,79 @@ Naming input types with `Input` on the end is a useful convention, because you w
166165

167166
Here's some runnable code that implements this schema, keeping the data in memory:
168167

169-
<Tabs items={['Template', 'Classes']}>
168+
<Tabs items={['SDL', 'Code']}>
170169
<Tabs.Tab>
171170
```js
172171
const express = require('express');
173172
const { createHandler } = require('graphql-http/lib/use/express');
174173
const { buildSchema } = require('graphql');
175174

176175
// Construct a schema, using GraphQL schema language
177-
const schema = buildSchema(/_ GraphQL _/ `
176+
const schema = buildSchema(`
178177
input MessageInput {
179-
content: String
180-
author: String
178+
content: String
179+
author: String
181180
}
182181

183182
type Message {
184-
id: ID!
185-
content: String
186-
author: String
183+
id: ID!
184+
content: String
185+
author: String
187186
}
188187

189188
type Query {
190-
getMessage(id: ID!): Message
189+
getMessage(id: ID!): Message
191190
}
192191

193192
type Mutation {
194-
createMessage(input: MessageInput): Message
195-
updateMessage(id: ID!, input: MessageInput): Message
193+
createMessage(input: MessageInput): Message
194+
updateMessage(id: ID!, input: MessageInput): Message
196195
}
197196
`);
198197

199198
// If Message had any complex fields, we'd put them on this object.
200199
class Message {
201-
constructor(id, { content, author }) {
202-
this.id = id;
203-
this.content = content;
204-
this.author = author;
205-
}
200+
constructor(id, { content, author }) {
201+
this.id = id;
202+
this.content = content;
203+
this.author = author;
204+
}
206205
}
207206

208207
// Maps username to content
209208
const fakeDatabase = {};
210209

211210
const root = {
212-
getMessage({ id }) {
213-
if (!fakeDatabase[id]) {
214-
throw new Error('no message exists with id ' + id);
215-
}
216-
return new Message(id, fakeDatabase[id]);
217-
},
218-
createMessage({ input }) {
219-
// Create a random id for our "database".
220-
const id = require('crypto').randomBytes(10).toString('hex');
211+
getMessage({ id }) {
212+
if (!fakeDatabase[id]) {
213+
throw new Error('no message exists with id ' + id);
214+
}
215+
return new Message(id, fakeDatabase[id]);
216+
},
217+
createMessage({ input }) {
218+
// Create a random id for our "database".
219+
const id = require('crypto').randomBytes(10).toString('hex');
221220

222221
fakeDatabase[id] = input;
223222
return new Message(id, input);
224-
225-
},
226-
updateMessage({ id, input }) {
227-
if (!fakeDatabase[id]) {
228-
throw new Error('no message exists with id ' + id);
229-
}
230-
// This replaces all old data, but some apps might want partial update.
231-
fakeDatabase[id] = input;
232-
return new Message(id, input);
233-
},
223+
},
224+
updateMessage({ id, input }) {
225+
if (!fakeDatabase[id]) {
226+
throw new Error('no message exists with id ' + id);
227+
}
228+
// This replaces all old data, but some apps might want partial update.
229+
fakeDatabase[id] = input;
230+
return new Message(id, input);
231+
},
234232
};
235233

236234
const app = express();
237235
app.all(
238-
'/graphql',
239-
createHandler({
240-
schema: schema,
241-
rootValue: root,
242-
}),
236+
'/graphql',
237+
createHandler({
238+
schema: schema,
239+
rootValue: root,
240+
}),
243241
);
244242
app.listen(4000, () => {
245243
console.log('Running a GraphQL API server at localhost:4000/graphql');

0 commit comments

Comments
 (0)