|
4 | 4 | import express, { Request } from "express"; |
5 | 5 | // import cors from "cors"; |
6 | 6 | import mongoose from "mongoose"; |
7 | | -import { ApolloServer, gql } from "apollo-server-express"; |
| 7 | +import { ApolloServer } from "apollo-server-express"; |
8 | 8 | import { makeExecutableSchema } from "@graphql-tools/schema"; |
9 | 9 |
|
10 | | -import { MongoMemoryServer } from "mongodb-memory-server"; // @see https://github.com/nodkz/mongodb-memory-server |
11 | 10 | import { |
12 | | - buildDefaultQueryResolvers, |
13 | | - createGraphqlModelServer, |
14 | 11 | buildApolloSchema, |
15 | 12 | createContext, |
16 | 13 | createDataSources, |
17 | 14 | } from "@vulcanjs/graphql/server"; |
18 | | -import { createMongooseConnector } from "@vulcanjs/mongo"; |
19 | 15 | import { addDefaultMongoConnector } from "@vulcanjs/mongo-apollo"; |
20 | 16 |
|
21 | 17 | import http from "http"; |
22 | 18 |
|
23 | | -// Init an in-memory Mongo server |
24 | | -// TODO: use a real db like in Vulcan Next |
25 | | -let mongod; |
26 | | -let mongoUri; |
27 | | -const startMongo = async () => { |
28 | | - // Spin up a dummy mongo server |
29 | | - mongod = await MongoMemoryServer.create(); |
30 | | - mongoUri = mongod.getUri(); |
31 | | - console.log("MongoUri", mongoUri); |
32 | | - // const port = await mongod.getPort(); |
33 | | - // const dbPath = await mongod.getDbPath(); |
34 | | - // const dbName = await mongod.getDbName(); |
35 | | - // Connect mongoose client |
36 | | - await mongoose.connect(mongoUri); |
37 | | -}; |
38 | | -const closeMongo = async () => { |
39 | | - console.log("Exiting, close Mongo connection"); |
40 | | - // remove the collection |
41 | | - // disconnect the client |
42 | | - await mongoose.disconnect(); |
43 | | - console.log("Disconneced mongoose"); |
44 | | - // stop mongo server |
45 | | - await mongod.stop(); |
46 | | - console.log("Closed connection"); |
47 | | - process.exit(0); |
48 | | -}; |
49 | | - |
50 | | -// Demo model |
51 | | -/** |
52 | | - * Demo model |
53 | | - * |
54 | | - * Try this query for example: |
55 | | - * |
56 | | - * query contribs { |
57 | | - contributors { |
58 | | - results { |
59 | | - name |
60 | | - myself { |
61 | | - name |
62 | | - } |
63 | | - } |
64 | | - } |
65 | | -} |
66 | | - */ |
67 | | -const Contributor = createGraphqlModelServer({ |
68 | | - name: "Contributor", |
69 | | - schema: { |
70 | | - _id: { |
71 | | - type: String, |
72 | | - optional: true, |
73 | | - canRead: ["guests"], |
74 | | - canCreate: ["guests"], |
75 | | - canUpdate: ["guests"], |
76 | | - //canDelete: ["guests"], |
77 | | - }, |
78 | | - name: { |
79 | | - type: String, |
80 | | - optional: true, |
81 | | - canRead: ["guests"], |
82 | | - canCreate: ["guests"], |
83 | | - canUpdate: ["guests"], |
84 | | - //canDelete: ["guests"], |
85 | | - }, |
86 | | - // Virtual field that queries the contributor itself |
87 | | - // This is just a dumb demo for dataSources |
88 | | - myselfVirtual: { |
89 | | - type: String, |
90 | | - canRead: ["guests"], |
91 | | - canCreate: [], |
92 | | - canUpdate: [], |
93 | | - resolveAs: { |
94 | | - fieldName: "myself", |
95 | | - typeName: "Contributor", |
96 | | - resolver: async (root /*: ContributorDocument*/, args, context) => { |
97 | | - return await context.dataSources["Contributor"].findOneById(root._id); |
98 | | - }, |
99 | | - }, |
100 | | - }, |
| 19 | +import { startMongo, closeMongo } from "./inMemoryMongo"; |
| 20 | +import { Contributor, models } from "./models"; |
101 | 21 |
|
102 | | - // TODO: add resolved field using data source |
103 | | - }, |
104 | | - graphql: { |
105 | | - typeName: "Contributor", |
106 | | - multiTypeName: "Contributors", |
107 | | - queryResolvers: buildDefaultQueryResolvers({ |
108 | | - typeName: "Contributor", |
109 | | - }), |
110 | | - }, |
111 | | - permissions: { |
112 | | - canRead: ["guests"], |
113 | | - canCreate: ["guests"], |
114 | | - }, |
115 | | -}); |
116 | | -const contributorConnector = createMongooseConnector(Contributor, { |
117 | | - // Passing an instance is only needed in local development or if you have multiple mongoose connections |
118 | | - // Otherwise the default export of "mongoose" is always the default connection |
119 | | - mongooseInstance: mongoose, |
120 | | -}); |
121 | | -Contributor.graphql.connector = contributorConnector; |
122 | | -//await mongoose.models["contributors"].deleteMany(); |
123 | | -const models = [Contributor]; |
124 | | -// Will add relevant data sources where necessary |
| 22 | +// Will add relevant data sources and connectors if necessary |
| 23 | +// Using Mongo as a default |
125 | 24 | addDefaultMongoConnector(models); |
126 | 25 |
|
127 | | -// Graphql schema |
| 26 | +// Graphql resolvers and typedefs |
128 | 27 | const vulcanRawSchema = buildApolloSchema(models); |
| 28 | +// Executable graphq schema |
129 | 29 | const vulcanSchema = makeExecutableSchema(vulcanRawSchema); |
130 | 30 |
|
131 | 31 | const contextForModels = createContext(models); |
@@ -165,8 +65,15 @@ const startServer = async () => { |
165 | 65 | const seedDb = async () => { |
166 | 66 | // insert some dummy data just for testing |
167 | 67 | console.log("Seeding..."); |
168 | | - await contributorConnector.delete({}); |
169 | | - await contributorConnector.create({ name: "John Doe" }); |
| 68 | + /** |
| 69 | + * NOTE: calling the mongoose model directly WON'T run |
| 70 | + * the model callbacks. |
| 71 | + * |
| 72 | + * You may instead want to use a "mutator" |
| 73 | + */ |
| 74 | + const contributorMongooseModel = mongoose.models[Contributor.name]; |
| 75 | + await contributorMongooseModel.remove({}); |
| 76 | + await contributorMongooseModel.create({ name: "John Doe" }); |
170 | 77 | console.log("Done seeding db with 1 contributor"); |
171 | 78 | }; |
172 | 79 | const start = async () => { |
|
0 commit comments