Last active
January 29, 2021 16:08
-
-
Save BatuhanW/82b5f52b4c664748a780ee30f88d47b8 to your computer and use it in GitHub Desktop.
Small script to parse GraphQL Schemas and console.log some stats about them
This file contains hidden or 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
// Place *.graphql files ./schemas folder and update schemas object | |
const fs = require('fs'); | |
const graphql = require('graphql'); | |
const schemas = [ | |
{ name: 'github', metaQueries: 16 }, | |
{ name: 'gitlab', metaQueries: 7 } | |
]; | |
const result = {}; | |
schemas.map(({ name, metaQueries }) => { | |
const definitions = graphql.parse( | |
fs.readFileSync(`./schemas/${name}.graphql`).toString() | |
).definitions; | |
const typeReducer = (field) => { | |
if (!field) return ''; | |
if (field.kind === 'NamedType') return field.name.value; | |
return typeReducer(field.type); | |
}; | |
const queriesCount = definitions.find( | |
(definition) => | |
(definition && definition.name && definition.name.value === 'Query') || | |
(definition && definition.name && definition.name.value === 'QueryRoot') | |
).fields.length; | |
const mutationCount = definitions.find( | |
(definition) => | |
definition && definition.name && definition.name.value === 'Mutation' | |
).fields.length; | |
const typesCount = definitions.length; | |
result[name] = { | |
businessQueries: queriesCount - metaQueries, | |
metaQueries, | |
mutationCount, | |
typesCount, | |
typePerQuery: typesCount / (queriesCount - metaQueries), | |
}; | |
}); | |
console.log(result); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment