Skip to content

Commit d458074

Browse files
committed
Prettify
1 parent 0447d62 commit d458074

File tree

4 files changed

+105
-101
lines changed

4 files changed

+105
-101
lines changed

graphql-server.js

+29-26
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
"use strict";
2-
const graphqlHTTP = require("express-graphql");
3-
const {buildSchema} = require("graphql");
4-
const express = require("express");
5-
const app = express();
1+
'use strict'
2+
const graphqlHTTP = require('express-graphql')
3+
const { buildSchema } = require('graphql')
4+
const express = require('express')
5+
const app = express()
66

77
// Get the Mongoose models used for querying the database
8-
const {User, Group} = require("./models.js");
8+
const { User, Group } = require('./models.js')
99

1010
// Start up a GraphQL endpoint listening at /graphql
11-
app.use("/graphql", graphqlHTTP({
11+
app.use(
12+
'/graphql',
13+
graphqlHTTP({
1214
// We construct our GraphQL schema which has three types:
1315
// The User, Group, and Query types (through which all
1416
// queries for data are defined)
@@ -34,26 +36,27 @@ app.use("/graphql", graphqlHTTP({
3436
// The methods that we'll use to get the data for our
3537
// main queries
3638
rootValue: {
37-
// Get a user based on the ID and return it as a Promise
38-
user({id}) {
39-
return User.findById(id);
40-
},
41-
// Get an array of users and return them as a Promise
42-
users() {
43-
return User.find({});
44-
},
45-
// Get a group based on the ID and return it as a Promise
46-
group({id}) {
47-
return Group.findById(id);
48-
},
49-
// Get an array of groups and return them as a Promise
50-
groups() {
51-
return Group.find({});
52-
},
39+
// Get a user based on the ID and return it as a Promise
40+
user({ id }) {
41+
return User.findById(id)
42+
},
43+
// Get an array of users and return them as a Promise
44+
users() {
45+
return User.find({})
46+
},
47+
// Get a group based on the ID and return it as a Promise
48+
group({ id }) {
49+
return Group.findById(id)
50+
},
51+
// Get an array of groups and return them as a Promise
52+
groups() {
53+
return Group.find({})
54+
}
5355
},
5456
// Display the GraphiQL web interface (for easy usage!)
55-
graphiql: true,
56-
}));
57+
graphiql: true
58+
})
59+
)
5760

5861
// Start the application, listening on port 3000
59-
app.listen(3000);
62+
app.listen(3000)

models.js

+17-18
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
1-
"use strict";
2-
const mongoose = require("mongoose");
1+
'use strict'
2+
const mongoose = require('mongoose')
33

44
// Connect to the local MongoDB database named “testdb”
5-
mongoose.connect("mongodb://localhost/testdb",
6-
{useMongoClient: true});
5+
mongoose.connect('mongodb://localhost/testdb', { useMongoClient: true })
76

87
// Use real promises
9-
mongoose.Promise = global.Promise;
8+
mongoose.Promise = global.Promise
109

1110
// Create a Group schema to be stored in the MongoDB database
1211
const GroupSchema = new mongoose.Schema({
13-
_id: String,
14-
name: String,
15-
});
12+
_id: String,
13+
name: String
14+
})
1615

1716
// Turn that schema into a model that we can query
18-
const Group = mongoose.model("Group", GroupSchema);
17+
const Group = mongoose.model('Group', GroupSchema)
1918

2019
// Create a User schema to be stored in the MongoDB database
2120
const UserSchema = new mongoose.Schema({
22-
_id: String,
23-
username: String,
24-
groupId: String,
25-
});
21+
_id: String,
22+
username: String,
23+
groupId: String
24+
})
2625

2726
// Retrieve the group associated with the user
2827
UserSchema.methods.group = function() {
29-
// Use .exec() to ensure a true Promise is returned
30-
return Group.findById(this.groupId).exec();
31-
};
28+
// Use .exec() to ensure a true Promise is returned
29+
return Group.findById(this.groupId).exec()
30+
}
3231

3332
// Turn that schema into a model that we can query
34-
const User = mongoose.model("User", UserSchema);
33+
const User = mongoose.model('User', UserSchema)
3534

36-
module.exports = {User, Group};
35+
module.exports = { User, Group }

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"name": "graphql-rest-api-demo",
33
"version": "0.0.1",
4-
"description": "A demo of what an equivalent REST API and GraphQL interface look like.",
4+
"description":
5+
"A demo of what an equivalent REST API and GraphQL interface look like.",
56
"main": "graphql-server.js",
67
"scripts": {
78
"test": "echo \"Error: no test specified\" && exit 1"

rest-server.js

+57-56
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,76 @@
1-
"use strict";
2-
const express = require("express");
3-
const app = express();
1+
'use strict'
2+
const express = require('express')
3+
const app = express()
44

55
// Get the Mongoose models used for querying the database
6-
const {User} = require("./models.js");
6+
const { User } = require('./models.js')
77

88
// A list of the fields that are allowed to be accessed
9-
const defaultFields = ["_id", "username", "group"];
9+
const defaultFields = ['_id', 'username', 'group']
1010

1111
// Filter a user object based on the requested fields
1212
const filterFields = async function(req, user) {
13-
// We assume the fields are a comma-separated list of field
14-
// names, if none is specified then we return all fields.
15-
const fieldKeys = req.query.fields
16-
? req.query.fields.split(",")
17-
: defaultFields;
13+
// We assume the fields are a comma-separated list of field
14+
// names, if none is specified then we return all fields.
15+
const fieldKeys = req.query.fields
16+
? req.query.fields.split(',')
17+
: defaultFields
1818

19-
// Generate a new object that contains only those fields.
20-
const filteredUser = {};
21-
for (const field of fieldKeys) {
22-
// If the field is a function then we expect it to return
23-
// a promise which we will immediately resolve.
24-
if (typeof user[field] === "function") {
25-
filteredUser[field] = await user[field]();
26-
} else {
27-
filteredUser[field] = user[field];
28-
}
19+
// Generate a new object that contains only those fields.
20+
const filteredUser = {}
21+
for (const field of fieldKeys) {
22+
// If the field is a function then we expect it to return
23+
// a promise which we will immediately resolve.
24+
if (typeof user[field] === 'function') {
25+
filteredUser[field] = await user[field]()
26+
} else {
27+
filteredUser[field] = user[field]
2928
}
30-
return filteredUser;
31-
};
29+
}
30+
return filteredUser
31+
}
3232

3333
// Listen for all GET requests to /users/:id URL (where the
3434
// ID is the ID of the user account)
35-
app.get("/users/:id", (req, res) => {
36-
// Try to find the user by their id (_id field), using the ID
37-
// parameter from the URL.
38-
User.findById(req.params.id, async (err, user) => {
39-
if (err) {
40-
// The DB returned an error so we return a 500 error
41-
return res.status(500).end();
42-
}
35+
app.get('/users/:id', (req, res) => {
36+
// Try to find the user by their id (_id field), using the ID
37+
// parameter from the URL.
38+
User.findById(req.params.id, async (err, user) => {
39+
if (err) {
40+
// The DB returned an error so we return a 500 error
41+
return res.status(500).end()
42+
}
4343

44-
if (!user) {
45-
// No user was found so we return a 404 error
46-
return res.status(404).end();
47-
}
44+
if (!user) {
45+
// No user was found so we return a 404 error
46+
return res.status(404).end()
47+
}
4848

49-
// Return the user to the client (automatically serialized
50-
// as a JSON string). We need to wait for all of the fields
51-
// to load before we can return the results.
52-
res.send(await filterFields(req, user));
53-
});
54-
});
49+
// Return the user to the client (automatically serialized
50+
// as a JSON string). We need to wait for all of the fields
51+
// to load before we can return the results.
52+
res.send(await filterFields(req, user))
53+
})
54+
})
5555

5656
// Listen for all GET requests to /users
57-
app.get("/users", (req, res) => {
58-
// Find all of the users in the database collection (we pass in
59-
// an empty collection as we aren't filtering the results)
60-
User.find({}, async (err, users) => {
61-
if (err) {
62-
// The DB returned an error so we return a 500 error
63-
return res.status(500).end();
64-
}
57+
app.get('/users', (req, res) => {
58+
// Find all of the users in the database collection (we pass in
59+
// an empty collection as we aren't filtering the results)
60+
User.find({}, async (err, users) => {
61+
if (err) {
62+
// The DB returned an error so we return a 500 error
63+
return res.status(500).end()
64+
}
6565

66-
// Return the array of users to the client (automatically
67-
// serialized as a JSON string) We need to wait for all
68-
// of the Promises to resolve for all of the users.
69-
res.send(await Promise.all(users.map(async (user) =>
70-
await filterFields(req, user))));
71-
});
72-
});
66+
// Return the array of users to the client (automatically
67+
// serialized as a JSON string) We need to wait for all
68+
// of the Promises to resolve for all of the users.
69+
res.send(
70+
await Promise.all(users.map(async user => await filterFields(req, user)))
71+
)
72+
})
73+
})
7374

7475
// Start the application, listening on port 3000
75-
app.listen(3000);
76+
app.listen(3000)

0 commit comments

Comments
 (0)