Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphql #2

Merged
merged 8 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
wayyyy better graphql schema :)
  • Loading branch information
wrannaman committed Jun 6, 2019
commit 77dc0c2984c80c14e5a2c686cb72fd64e11a0907
40 changes: 25 additions & 15 deletions api/graphql/getGraphqlSchema.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
module.exports = (schema, returnAs = 'csv') => {
const getType = (type) => {
switch (type) {
case 'Number':
return 'Float';
case 'Boolean':
return 'Boolean';
case 'ObjectId':
case 'String':
default:
return 'String';
}
};
const handleObject = ((schema, key) => {
if (schema[key].type) return `${key}: ${getType(schema[key].type)}`;
return `${key}: {
${Object.keys(schema[key]).map((value) => {
return `${value}: ${getType(schema[key][value].type)}`;
}).join(' ')}
}`;
});
const getGQLSchema = (schema, returnAs = 'csv', round = 0) => {
const str = [];
Object.keys(schema).forEach(key => {
let type = "String";
switch (schema[key].type) {
case 'Number':
type = 'Float';
break;
case 'Boolean':
type = 'Boolean';
break;
case 'ObjectId':
case 'String':
default:
}
str.push(`${key}: ${type}`);
Object.keys(schema).forEach((key) => {
const toAdd = handleObject(schema, key)
str.push(toAdd);
});
return returnAs === 'csv' ? str.join(', ') : str.join('\n\t\t\t');
}
module.exports = getGQLSchema;
File renamed without changes.
3 changes: 2 additions & 1 deletion api/samples/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"type": "String",
"trim": true,
"select": false,
"immutable": true
"immutable": true,
"required": true
},
"intro": {
"type": "Boolean",
Expand Down
4 changes: 4 additions & 0 deletions api/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
"mongoose": "^5.5.12",
"express-graphql": "^0.8.0",
"graphql": "^14.3.1",
"graphql-compose": "^7.1.0",
"graphql-compose-connection": "^6.0.3",
"graphql-compose-mongoose": "^7.0.3",
"graphql-compose-pagination": "^6.0.3",
"graphql-server-express": "^1.4.0",
"graphql-tools": "^4.0.4",
"mongoose-paginate-v2": "^1.3.0",
Expand Down
43 changes: 39 additions & 4 deletions api/server/serverIndex.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const beautify = require('js-beautify').js;
const { uppercase } = require('../utils');

module.exports = ({ destination, logging, name, flavor }) => {
const modelFolder = `${destination}`;
Expand All @@ -10,13 +11,47 @@ module.exports = ({ destination, logging, name, flavor }) => {
// graphql
const graphqlHTTP = require('express-graphql');
const { makeExecutableSchema } = require('graphql-tools');
const { typeDefs, resolvers } = require('./graphql')
const schema = makeExecutableSchema({ typeDefs, resolvers });
// const { typeDefs, resolvers } = require('./graphql');
// const schema = makeExecutableSchema({ typeDefs, resolvers });
// app.use('/graphql', graphqlHTTP({
// schema,
// graphiql: true
// }));

const { composeWithMongoose } = require('graphql-compose-mongoose/node8');
const { schemaComposer } = require('graphql-compose');
const customizationOptions = {}; // left it empty for simplicity, described below
const ${uppercase(name)} = require('./models/${name}');
const ${uppercase(name)}TC = composeWithMongoose(${uppercase(name)}, customizationOptions);

schemaComposer.Query.addFields({
${name}ById: UserTC.getResolver('findById'),
${name}ByIds: UserTC.getResolver('findByIds'),
${name}One: UserTC.getResolver('findOne'),
${name}Many: UserTC.getResolver('findMany'),
${name}Count: UserTC.getResolver('count'),
${name}Connection: UserTC.getResolver('connection'),
${name}Pagination: UserTC.getResolver('pagination'),
});

schemaComposer.Mutation.addFields({
${name}CreateOne: UserTC.getResolver('createOne'),
${name}CreateMany: UserTC.getResolver('createMany'),
${name}UpdateById: UserTC.getResolver('updateById'),
${name}UpdateOne: UserTC.getResolver('updateOne'),
${name}UpdateMany: UserTC.getResolver('updateMany'),
${name}RemoveById: UserTC.getResolver('removeById'),
${name}RemoveOne: UserTC.getResolver('removeOne'),
${name}RemoveMany: UserTC.getResolver('removeMany'),
});

const graphqlSchema = schemaComposer.buildSchema();

app.use('/graphql', graphqlHTTP({
schema,
schema: graphqlSchema,
graphiql: true
}));
`
`;
code.push(`
const express = require('express');
const fs = require('fs');
Expand Down
20 changes: 17 additions & 3 deletions api/tests/fakeObject.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = (schema, asString = false) => {
const fo = (schema, asString = false, parseSchema = true) => {
const genData = require('./genData');
schema = require(schema).schema; // eslint-disable-line
if (parseSchema) schema = require(schema).schema; // eslint-disable-line
const fakeObject = {};
Object.keys(schema).forEach((key) => {
fakeObject[key] = genData(schema[key].type, schema[key], schema[key].enum);
Expand All @@ -12,7 +12,21 @@ module.exports = (schema, asString = false) => {
const str = [];
Object.keys(fakeObject).forEach(key => {
const value = schema[key].type === 'String' ? `"${fakeObject[key]}"` : fakeObject[key];
str.push(`${key}: ${value}`);
const isObject = typeof value === 'object';
let abstractedValue = value;
if (isObject) {
if (asString) {
const _str = [];
Object.keys(value).forEach(_key => {
_str.push(`${_key}: ${value[_key]}`)
})
abstractedValue = `" ${_str.join(', ')} "`
}

}

str.push(`${key}: ${abstractedValue}`);
});
return { obj: fakeObject, str: str.join(', ') };
};
module.exports = fo;
4 changes: 3 additions & 1 deletion api/tests/genData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
module.exports = (type, value, isEnum = false) => {
const subDocHelper = require('./subDocHelper');

if (isEnum) return value[0];
if (isEnum) {
return isEnum[0];
}
if (!type) {
return subDocHelper(value);
}
Expand Down
Loading