Created
July 21, 2020 23:11
-
-
Save magicmark/36ef55640b82bc6d8db2e05d706c8ef9 to your computer and use it in GitHub Desktop.
Generate a dummy graphql schema
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @file Helper tool to generate a huge schema for testing purposes | |
* | |
* Usage: | |
* | |
* $ ./generate-schema <numberOfTypes> <fieldsPerType> | |
*/ | |
const assert = require("assert"); | |
function getType(typeName, numFields) { | |
const fields = []; | |
Array(numFields) | |
.fill("") | |
.forEach((_, idx) => { | |
const fieldName = `dummyField${idx + 1}`; | |
fields.push( | |
// prettier-ignore | |
[ | |
` """`, | |
` Description for field: ${fieldName}`, | |
` """`, | |
` ${fieldName}: String`, | |
].join("\n") | |
); | |
}); | |
return [ | |
// prettier-ignore | |
`"""`, | |
`Description for type: ${typeName}`, | |
`"""`, | |
`type ${typeName} {`, | |
fields.join("\n\n"), | |
`}`, | |
].join("\n"); | |
} | |
function main({ | |
// How many types should we generate? | |
numberOfTypes, | |
// How many fields per type should we generate? | |
fieldsPerType, | |
}) { | |
// check the function inputs | |
assert(Number.isInteger(numberOfTypes), "numberOfTypes must be a number"); | |
assert(Number.isInteger(fieldsPerType), "fieldsPerType must be a number"); | |
const types = []; | |
Array(numberOfTypes) | |
.fill("") | |
.forEach((_, idx) => { | |
types.push(getType(`DummyType${idx + 1}`, fieldsPerType)); | |
}); | |
const schema = types.join("\n\n"); | |
return schema; | |
} | |
module.exports = main; | |
if (!process.env.NODE_ENV && require.main === module) { | |
const [ | |
, | |
, | |
// How many types should we generate? | |
numberOfTypes, | |
// How many fields per type should we generate? | |
fieldsPerType, | |
] = process.argv; | |
console.log( | |
main({ | |
numberOfTypes: parseInt(numberOfTypes, 10), | |
fieldsPerType: parseInt(fieldsPerType, 10), | |
}) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment