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
Next Next commit
borked graphql on user schema with nested json
  • Loading branch information
wrannaman committed Jun 6, 2019
commit d3965dc13069e7cb0472e01ec5310dac251d4e57
6 changes: 3 additions & 3 deletions api/controller/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ module.exports = ({ schema, logging, destination, name }) => {
` try {`,
` const { ${extraKeys.join(', ')}} = req.query;`,
` const { ${schemaKeys.join(', ')}} = req.query;`,
` console.log('req query ', req.query);`,
// ` console.log('req query ', req.query);`,
` // The model query`,
` const find = {};`,
` // pagination object (search, sort, filter, etc)`,
Expand Down Expand Up @@ -104,8 +104,8 @@ module.exports = ({ schema, logging, destination, name }) => {
];
const save = [
`// save`,
`console.log('find ', find);`,
`console.log('where', where);`,
// `console.log('find ', find);`,
// `console.log('where', where);`,
`const ${name} = await ${uppercase(name)}.paginate(find, where); // @TODO populate: '<model name>'`,
`return res.json({ ${name}s: ${name} });`
];
Expand Down
1 change: 0 additions & 1 deletion api/graphql/createIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module.exports = ({ destination, flavor }) => {
// if (logging) console.log('creating user-can');
fs.mkdirSync(modelFolder);
}
console.log('create index');
const pretty = beautify(code, { indent_size: 2, space_in_empty_paren: true });
fs.writeFileSync(indexFile, pretty);
};
1 change: 0 additions & 1 deletion api/graphql/createPrepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module.exports = ({ destination, flavor }) => {
// if (logging) console.log('creating user-can');
fs.mkdirSync(modelFolder);
}
console.log('create index');
const pretty = beautify(code, { indent_size: 2, space_in_empty_paren: true });
fs.writeFileSync(indexFile, pretty);
};
10 changes: 3 additions & 7 deletions api/graphql/createResolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ module.exports = ({ destination, flavor, name }) => {
Query: {
${name}: async (root, {_id}) => {
// // @TODO Permissions,
// const permission = userCan('getOne', '${name}', req.user, args);,
// if (!permission) throw new Error('Permission denied for userCan getOne ${name}');,
return await ${uppercase(name)}.findOne({ _id });
},
Expand All @@ -30,15 +29,13 @@ module.exports = ({ destination, flavor, name }) => {
// // @TODO Permissions,
// const permission = userCan('create', '${name}', req.user, args);,
// if (!permission) throw new Error('Permission denied for userCan create ${name}');,

const new${uppercase(name)} = await ${uppercase(name)}.create(args);
console.log('RES', new${uppercase(name)})
return new${uppercase(name)};
},
update${uppercase(name)}: async (root, args, context, info) => {
const argscopy = Object.assign({}, args);
delete argscopy.${name}ID;
let getOne = await ${uppercase(name)}.findOne({ _id: args.${name}ID });
delete argscopy._id;
let getOne = await ${uppercase(name)}.findOne({ _id: args._id });

Object.keys(args).forEach(key => getOne[key] = args[key]);
// // @TODO Permissions,
Expand All @@ -48,7 +45,7 @@ module.exports = ({ destination, flavor, name }) => {
return getOne;
},
delete${uppercase(name)}: async (root, args, context, info) => {
const getOne = await ${uppercase(name)}.findOne({ _id: args.${name}ID });
const getOne = await ${uppercase(name)}.findOne({ _id: args._id });
// // @TODO Permissions,
// const permission = userCan('delete', '${name}', req.user, args);,
// if (!permission) throw new Error('Permission denied for userCan delete ${name}');,
Expand All @@ -67,7 +64,6 @@ module.exports = ({ destination, flavor, name }) => {
// if (logging) console.log('creating user-can');
fs.mkdirSync(modelFolder);
}
console.log('create index');
const pretty = beautify(code, { indent_size: 2, space_in_empty_paren: true });
fs.writeFileSync(indexFile, pretty);
};
7 changes: 3 additions & 4 deletions api/graphql/createTypeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ module.exports = ({ schema, destination, flavor, name }) => {
}
type Mutation {
create${uppercase(name)}(${getGraphqlSchema(schema, 'csv')}): ${uppercase(name)}
update${uppercase(name)}(${name}ID: String, ${getGraphqlSchema(schema, 'csv')}): ${uppercase(name)}
delete${uppercase(name)}(${name}ID: String): ${uppercase(name)}
update${uppercase(name)}(_id: String, ${getGraphqlSchema(schema, 'csv')}): ${uppercase(name)}
delete${uppercase(name)}(_id: String): ${uppercase(name)}
}
schema {
query: Query
Expand All @@ -30,14 +30,13 @@ module.exports = ({ schema, destination, flavor, name }) => {
\`
];

`
`;

// if (logging) console.log('checking user-can');
if (!fs.existsSync(modelFolder)) {
// if (logging) console.log('creating user-can');
fs.mkdirSync(modelFolder);
}
console.log('create index');
const pretty = beautify(code, { indent_size: 2, space_in_empty_paren: true });
fs.writeFileSync(indexFile, pretty);
};
15 changes: 12 additions & 3 deletions api/tests/fakeObject.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
module.exports = (schema) => {
module.exports = (schema, asString = false) => {
const genData = require('./genData');
schema = require(schema).schema; // eslint-disable-line
const fakeObject = {};
Object.keys(schema).forEach((key) => {
fakeObject[key] = genData(schema[key].type);
fakeObject[key] = genData(schema[key].type, schema[key], schema[key].enum);
});
return fakeObject;

if (!asString) return fakeObject;

// into string
let str = [];
Object.keys(fakeObject).forEach(key => {
const value = schema[key].type === 'String' ? `"${fakeObject[key]}"` : fakeObject[key];
str.push(`${key}: ${value}`);
});
return { obj: fakeObject, str };
};
10 changes: 9 additions & 1 deletion api/tests/genData.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
module.exports = (type) => {

module.exports = (type, value, isEnum = false) => {
const subDocHelper = require('./subDocHelper');

if (isEnum) return value[0];
if (!type) {
return subDocHelper(value);
}
switch (type.toLowerCase()) {
case 'string':
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
Expand All @@ -8,6 +15,7 @@ module.exports = (type) => {
return { hi: true, cool: "okay", bean: 123 };
case 'boolean':
return Math.floor(Math.random() * 1000) > 500 ? true : false;
case 'enum':
default:

}
Expand Down
9 changes: 9 additions & 0 deletions api/tests/graphqlSafe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = (prop) => {
console.log('PROP', typeof prop)
switch (typeof prop) {
case "string":
return `"${prop}"`;
default:
return `${prop}`;
}
};
Loading