/
index.ts
106 lines (91 loc) · 2.87 KB
/
index.ts
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { DatabaseManager } from '@accounts/database-manager';
import { AccountsModule } from '@accounts/graphql-api';
import MongoDBInterface from '@accounts/mongo';
import { AccountsPassword } from '@accounts/password';
import { AccountsServer } from '@accounts/server';
import { ApolloServer, makeExecutableSchema } from 'apollo-server';
import gql from 'graphql-tag';
import { mergeResolvers, mergeTypeDefs } from 'graphql-toolkit';
import mongoose from 'mongoose';
const start = async () => {
// Create database connection
await mongoose.connect('mongodb://localhost:27017/accounts-js-graphql-example', {
useNewUrlParser: true,
});
const mongoConn = mongoose.connection;
// Build a storage for storing users
const userStorage = new MongoDBInterface(mongoConn);
// Create database manager (create user, find users, sessions etc) for accounts-js
const accountsDb = new DatabaseManager({
sessionStorage: userStorage,
userStorage,
});
const accountsPassword = new AccountsPassword({
// This option is called when a new user create an account
// Inside we can apply our logic to validate the user fields
validateNewUser: user => {
// For example we can allow only some kind of emails
if (user.email.endsWith('.xyz')) {
throw new Error('Invalid email');
}
return user;
},
});
// Create accounts server that holds a lower level of all accounts operations
const accountsServer = new AccountsServer(
{ db: accountsDb, tokenSecret: 'secret' },
{
password: accountsPassword,
}
);
// Creates resolvers, type definitions, and schema directives used by accounts-js
const accountsGraphQL = AccountsModule.forRoot({
accountsServer,
});
const typeDefs = gql`
type PrivateType @auth {
field: String
}
# Our custom fields to add to the user
extend input CreateUserInput {
profile: CreateUserProfileInput!
}
input CreateUserProfileInput {
firstName: String!
lastName: String!
}
type Query {
publicField: String
privateField: String @auth
privateType: PrivateType
}
type Mutation {
_: String
}
`;
const resolvers = {
Query: {
publicField: () => 'public',
privateField: () => 'private',
privateType: () => ({
field: () => 'private',
}),
},
};
const schema = makeExecutableSchema({
typeDefs: mergeTypeDefs([typeDefs, accountsGraphQL.typeDefs]),
resolvers: mergeResolvers([accountsGraphQL.resolvers, resolvers]),
schemaDirectives: {
...accountsGraphQL.schemaDirectives,
},
});
// Create the Apollo Server that takes a schema and configures internal stuff
const server = new ApolloServer({
schema,
context: accountsGraphQL.context,
});
server.listen(4000).then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
};
start();