-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3.apollo-mongo.js
More file actions
83 lines (76 loc) · 2.75 KB
/
3.apollo-mongo.js
File metadata and controls
83 lines (76 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { ApolloServer } from '@apollo/server';
import { expressMiddleware } from '@apollo/server/express4';
import { ApolloServerPluginDrainHttpServer } from '@apollo/server/plugin/drainHttpServer'
import express from 'express';
import http from 'http';
import cors from 'cors';
import bodyParser from 'body-parser';
import { confacSchema } from './3.apollo-mongo-schema.js';
import { connectToDatabase, ObjectId } from './4.confac/helpers/db.cjs';
const resolvers = {
User: {
id: parent => parent.id ?? parent._id,
},
Query: {
hello: () => 'Apollo, about to make contact to mongo',
users: async (_, __, context) => await context.db.collection('users').find().toArray(),
user: async (_, {id}, context) => {
const result = await context.db.collection('users').findOne({_id: new ObjectId(id)})
return result;
},
},
Mutation: {
createUser: async (_, {name, firstName, roles}, context) => {
const user = {
email: `${firstName} ${name}`.toLocaleLowerCase().replace(/\s/g, '-') + '@itenium.be',
name,
firstName,
alias: name.toLowerCase().replace(/\s/g, '-'),
active: true,
roles,
audit: {
createdOn: new Date().toISOString(),
createdBy: context.user.id,
}
}
const inserted = await context.db.collection('users').insertOne(user);
const createdUser = inserted.ops[0]
return createdUser;
},
deleteUser: async (_, {id}, context) => {
const deleteResponse = await context.db.collection('users').findOneAndUpdate({_id: new ObjectId(id)}, {$set: {active: false}});
return !!deleteResponse.value;
},
updateUser: async (_, {id, name, firstName, roles}, context) => {
// const {value: originalUser} = await context.db.collection('users')
// .findOneAndUpdate({_id: new ObjectId(id)}, {$set: user}, {returnOriginal: true});
},
}
};
const app = express();
const httpServer = http.createServer(app);
const server = new ApolloServer({
typeDefs: confacSchema,
resolvers,
plugins: [ApolloServerPluginDrainHttpServer({ httpServer })],
});
await server.start();
app.use(
cors(),
bodyParser.json(),
expressMiddleware(server, {
context: async ({ req, res }) => {
const db = await connectToDatabase();
const randomUser = await db.collection('users').findOne()
return {
db,
user: {id: randomUser._id}
// user: parseJwt(req.headers.authorization)
}
},
}),
);
await new Promise(resolve => httpServer.listen({ port: 4000 }, resolve));
console.log('🚀 GraphQL confac server listening to port 4000');
console.log('See node/README.md for instructions if you want to setup a local mongo');
console.log('Otherwise: check Slack for the confac-test connection string');