Skip to content

Commit

Permalink
functionality for converting RootQueryType
Browse files Browse the repository at this point in the history
  • Loading branch information
jdiorio2393 committed Feb 5, 2021
1 parent ae1f880 commit 93c6960
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 92 deletions.
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"babili-webpack-plugin": "^0.1.2",
"concurrently": "^5.3.0",
"css-loader": "^5.0.1",
"electron": "^11.2.1",
"electron": "^11.2.2",
"electron-devtools-installer": "^3.1.1",
"electron-packager": "^15.2.0",
"file-loader": "^6.2.0",
Expand Down
186 changes: 100 additions & 86 deletions server/schema.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const graphql = require('graphql');
var cloneDeep = require('lodash.clonedeep');
const mongoose = require('mongoose');
const {
GraphQLObjectType,
GraphQLString,
Expand All @@ -9,105 +10,118 @@ const {
GraphQLList,
} = graphql;


const dummyData =
{
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,
},
_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,
},
_id: {
primaryKey: true,
type: 'string',
required: true,
},
participants: {
type: 'Array',
required: true,
},
messages: {
type: 'Array',
required: true,
},
__v: {
type: 'number',
required: true,
},
},
}
};

const capitalize = (s) => {
if (typeof s !== 'string') return ''
return s.charAt(0).toUpperCase() + s.slice(1)
}
let obj = {}
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;
}
};

let fieldsObj = {}
// 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])) {
fieldsObj[key] = {type: GraphQLString}
}
var deep = cloneDeep(fieldsObj);
console.log(deep === fieldsObj);
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,
});

obj[capitalize(`${property}Type`)] = new GraphQLObjectType({
name: capitalize(property),
fields: () => (deep)
})
console.log('HEREEEEEEEEEEEEEEEEEEEE',fieldsObj, obj)
fieldsObj = {}
rootQueryObj[property] = {
type: new GraphQLList(obj[capitalize(`${property}Type`)]),
resolve: function resolve(parent, args) {
return _.find({});
},
};
// resetting the fieldsObject
fieldsObj = {};
}

console.log(rootQueryObj);
// -------- Object Types ---------------

const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
// entry points into graphql queries
user: {
type: obj.UsersType,
// getting one type of book. tell args that id will be passed in and it will be of type ID.
args: { id: { type: GraphQLID } },
resolve(parent, args) {
// code to get data from db
// return _.find(books, { id: args.id });
return _.find({});
},
},
// author: {
// type: AuthorType,
// args: { id: { type: GraphQLID } },
// resolve(parent, args) {
// return _.find(authors, { id: args.id });
// },
// },
// books: {
// type: new GraphQLList(BookType),
// resolve(parent, args) {
// return books;
// },
// },
// authors: {
// type: new GraphQLList(AuthorType),
// resolve(parent, args) {
// return authors;
// },
// },
},
fields: rootQueryObj,
});

module.exports = new GraphQLSchema({
query: RootQuery,
})
query: RootQuery,
});

0 comments on commit 93c6960

Please sign in to comment.