Skip to content

Commit

Permalink
Formatting GraphQL RootQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenlabrie committed Feb 9, 2021
1 parent 3cbf3da commit 5808faa
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 47 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"lodash.clonedeep": "^4.5.0",
"mongodb": "^3.6.4",
"mongoose": "^5.11.14",
"pluralize": "^8.0.0",
"react": "^17.0.1",
"react-ace": "^9.3.0",
"react-codemirror2": "^7.2.1",
Expand Down
62 changes: 39 additions & 23 deletions server/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const graphql = require("graphql");
var cloneDeep = require("lodash.clonedeep");
const mongoose = require("mongoose");
const MongoClient = require("mongodb").MongoClient;
var pluralize = require('pluralize')

const {
GraphQLObjectType,
Expand Down Expand Up @@ -34,19 +35,19 @@ const getGraphQlType = (key, value) => {
break;
case value.type.includes("string"):
fieldsObj[key] = { type: GraphQLString };
strFieldsObj[key] = `{ type: GraphQLString }`;
strFieldsObj[`|${key}`] = `{ type: GraphQLString }`;
break;
case value.type.includes("Array"):
fieldsObj[key] = { type: new GraphQLList(GraphQLString) };
strFieldsObj[key] = `{ type: GraphQLString }`;
strFieldsObj[key] = `|{ type: GraphQLString }`;
break;
case value.type.includes("number"):
fieldsObj[key] = { type: GraphQLInt };
strFieldsObj[key] = `{ type: GraphQLInt }`;
strFieldsObj[key] = `|{ type: GraphQLInt }`;
break;
case value.type.includes("Object"):
fieldsObj[key] = { type: GraphQLObjectType };
strFieldsObj[key] = `{ type: GraphQLObjectType }`;
strFieldsObj[key] = `|{ type: GraphQLObjectType }`;
break;
default:
console.log(value, "Nothing Triggered-----");
Expand All @@ -66,7 +67,9 @@ let obj = {};
// Storing properties of each mongo db schema
let fieldsObj = {};
let strFieldsObj = {};
let stringObj = {}
let stringObj = {};
let strRootQueryObj = {};
let sendRootQueryObj = {};
for (const property in data) {
for (const [key, value] of Object.entries(data[property])) {
getGraphQlType(key, value);
Expand Down Expand Up @@ -99,26 +102,14 @@ async function run() {
}
const deep = cloneDeep(fieldsObj);
const strDeep = cloneDeep(strFieldsObj)

// Dynamically creating graphql object types
obj[capitalize(`${property}Type`)] = new GraphQLObjectType({
name: capitalize(property),
fields: () => deep,
});



// function formatQueries(query){
// return(
// `const RootQuery = new GraphQLObjectType({\n` +
// ` name: 'RootQueryType',\n` +
// ` fields: {` +
// `${query}\n`
// )
// }




// formats the graphQL schema to send to the front end
stringObj[property] = `const ${capitalize(`${property}Type`)} = new GraphQLObjectType({\n` +
` name: '${capitalize(property)}',\n` +
` fields: () => (\n` +
Expand All @@ -133,18 +124,43 @@ async function run() {
return run()
},
};



strRootQueryObj[pluralize.singular(property)] = `{|` +
` type: ${capitalize(`${property}Type`)},|`+
` args: { id: { type: GraphQLID }},|` +
` resolve(parent, args) {|` +
` return ${capitalize(pluralize.singular(property))}.findById(args.id);|` +
` } |` +
` }|`

strRootQueryObj[` ${property}`] = `{|` +
` type: new GraphQLList(${capitalize(`${property}Type`)}),|` +
` resolve(parent, args) {|` +
` return ${capitalize(pluralize.singular(property))}.find({});|`+
` }|` +
` },|`



// resetting the fieldsObject
console.log('this is stringObj ====>', strFieldsObj);
// console.log('this is stringObj ====>', strFieldsObj);
fieldsObj = {};
strFieldsObj = {};
}

sendRootQueryObj.queries = ` const RootQuery = new GraphQLObjectType({\n` +
` name: "RootQueryType",\n ` +
` fields: ${JSON.stringify(strRootQueryObj)});|`

const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: rootQueryObj,
})

res.locals.types = stringObj

res.locals.types = stringObj;
res.locals.queries = sendRootQueryObj;
next()
}

Expand All @@ -159,6 +175,6 @@ module.exports = {
schema: new GraphQLSchema({
query: RootQuery,
})
}
}


2 changes: 1 addition & 1 deletion server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ app.use('/graphql', graphqlHTTP({ schema: schemaRoute, graphiql: true }))

//Post request to get the selectedSchemas from the front end submit button
app.post('/selectedSchemas', schemaRoute.converter.migrateSchema, (req, res) => {
res.status(200).json({types: res.locals.types});
res.status(200).json({types: res.locals.types, queries: res.locals.queries});
console.log("ahhhhhhh", req.body.selectedSchemas)
console.log("ahhhhhhh", req.body.uriId)
//res.locals.rootQuery\
Expand Down
3 changes: 0 additions & 3 deletions src/Components/DropDownMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@ const DropDownMenu = ({schemaData, uriData, sendSchemas, addCheckmark}) => {
<div>
<button onClick={sendSchemas}>Add Selected Schemas</button>
</div>


</div>

)
}
export default DropDownMenu;
Expand Down
11 changes: 1 addition & 10 deletions src/Components/MongoDBURI.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const MongoDBURI = ({schemaData, uriData, submitbtn, geturi, sendSchemas, addChe
<div className="formContainer">
<div>
<form>
<input type="text" placeholder="enter MongoDBURI ..." onChange= {geturi} value="mongodb+srv://judy:coderepforum@coderep-forum-idfny.mongodb.net/Forum?retryWrites=true&w=majority"/>
<input type="text" placeholder="enter MongoDBURI ..." onChange= {geturi} />
<input className="URISubmitButton" type="submit" value="Submit" onClick={submitbtn}/>
</form>
</div>
Expand All @@ -17,12 +17,3 @@ const MongoDBURI = ({schemaData, uriData, submitbtn, geturi, sendSchemas, addChe
}

export default MongoDBURI;



//To-Dos:
//onsubmit, onchange function
//fetch mongoDBSchemas from backend
//figure out state
//get each schema from data obj, send it to DropDownMenu using object.keys to get keys of arr then pass to DropDownMenu
//in DropDownMenu, we would pair one CheckBox with one Schema
21 changes: 13 additions & 8 deletions src/Components/MongoSchemaIDE.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,31 @@ const MongoSchemaIDE = ({schemaData, selectedSchemaData, graphQLSchema}) => {
const [data, setData] = useState([]);
const [graphData, setGraphData] = useState({});

console.log("UPDATED ROOTQUERY OBJ+++", graphQLSchema)
// iterates over the graphQLSchema and removes the double quotes
// after a comma, adds a new line
let concat = ''
for (let key in graphQLSchema) {
console.log(graphQLSchema[key])
const newKey = graphQLSchema[key].replace(/["]+/g, '');
let array = newKey.split('');
console.log(array);
for (let i = 0; i < array.length; i+=1) {
if (array[i] === ',') {
concat += array[i]
concat += '\n '
console.log(array[i])
} else if (array[i] === '{' && array[i - 1] === '{') {
// if (array[i] === ',') {
// concat += array[i]
// concat += '\n '
// }
if (array[i] === '|') {
array[i] = '\n';
concat += array[i];
}
else if (array[i] === '{' && array[i - 1] === '{') {
continue;
}
else {
concat += array[i]
}
}
}
console.log('concat is......', concat);


return(
<div>
Expand Down
4 changes: 2 additions & 2 deletions src/containers/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Container = () => {
headers: {
"Content-Type" : "application/json"
},
body: JSON.stringify({val: uriId})
body: JSON.stringify({val: 'mongodb+srv://judy:coderepforum@coderep-forum-idfny.mongodb.net/Forum?retryWrites=true&w=majority'})
})
.then(res => res.json())
.then((data) => {
Expand Down Expand Up @@ -73,7 +73,7 @@ const Container = () => {
})
.then(res => res.json())
.then(data => {
console.log('DATA!!!!', data.types);
console.log('DATA!!!!', data);
setGraphQLSchema(data.types);
})
.catch((error) => {
Expand Down

0 comments on commit 5808faa

Please sign in to comment.