forked from oslabs-beta/QLens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schemabackup.js
156 lines (138 loc) · 3.33 KB
/
schemabackup.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const graphql = require("graphql");
var cloneDeep = require("lodash.clonedeep");
const mongoose = require("mongoose");
const MongoClient = require("mongodb").MongoClient;
const url =
"mongodb+srv://judy:hush@hush.u9hai.mongodb.net/chat?retryWrites=true&w=majority";
const {
GraphQLObjectType,
GraphQLString,
GraphQLSchema,
GraphQLID,
GraphQLInt,
GraphQLList,
} = graphql;
const dummyData = {
users: {
_id: {
primaryKey: true,
type: "Object",
required: true,
},
name: {
type: "string",
required: true,
},
password: {
type: "string",
required: true,
},
username: {
type: "string",
required: true,
},
__v: {
type: "number",
required: true,
},
},
conversations: {
_id: {
primaryKey: true,
type: "string",
required: true,
},
participants: {
type: "Array",
required: true,
},
messages: {
type: "Array",
required: true,
},
__v: {
type: "number",
required: true,
},
},
};
const getGraphQlType = (key, value) => {
switch (true) {
case key.includes("__v"):
break;
case key.includes("_id"):
fieldsObj[key] = { type: GraphQLID };
break;
case value.type.includes("string"):
fieldsObj[key] = { type: GraphQLString };
break;
case value.type.includes("Array"):
fieldsObj[key] = { type: new GraphQLList(GraphQLString) };
break;
case value.type.includes("number"):
fieldsObj[key] = { type: GraphQLInt };
break;
case value.type.includes("Object"):
fieldsObj[key] = { type: GraphQLObjectType };
break;
default:
console.log(value, "Nothing Triggered-----");
break;
}
};
// Function for capitalization
const capitalize = (s) => {
if (typeof s !== "string") return "";
return s.charAt(0).toUpperCase() + s.slice(1);
};
// -------- Object Types ---------------
// Storing graphql object types
let obj = {};
// Storing properties of each mongo db schema
let fieldsObj = {};
let rootQueryObj = {};
for (const property in dummyData) {
for (const [key, value] of Object.entries(dummyData[property])) {
getGraphQlType(key, value);
}
const deep = cloneDeep(fieldsObj);
// Dynamically creating graphql object types
obj[capitalize(`${property}Type`)] = new GraphQLObjectType({
name: capitalize(property),
fields: () => deep,
});
const connectToDb = () => {
MongoClient.connect(url, function (err, client) {
const regex = /\/(\w+)\?/g
const databaseName = url.match(regex)
const databaseString = databaseName.join('').slice(1, databaseName.join('').length - 1)
const collection = client.db(databaseString).collection('users');
// Show that duplicate records got dropped
let returnedItems = collection.find({}).toArray((err, items) => {
return items
})
console.log(returnedItems)
})
}
console.log(connectToDb())
rootQueryObj[property] = {
type: new GraphQLList(obj[capitalize(`${property}Type`)]),
resolve: function resolve(parent, args) {
//console.log('OUTSIDE------------------------------------',collectionData)
//return collectionData
// return _.find({});
return collectionData
},
};
// resetting the fieldsObject
fieldsObj = {};
}
// console.log(rootQueryObj);
// -------- Object Types ---------------
const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: rootQueryObj,
});
module.exports = new GraphQLSchema({
query: RootQuery,
});