Created
July 19, 2017 16:22
-
-
Save sarkistlt/5d41c6a3a6d8a6c23599415faaa853f1 to your computer and use it in GitHub Desktop.
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
import { | |
GraphQLBoolean, | |
GraphQLEnumType, | |
GraphQLFloat, | |
GraphQLInputObjectType, | |
GraphQLInt, | |
GraphQLInterfaceType, | |
GraphQLList, | |
GraphQLObjectType, | |
GraphQLString, | |
GraphQLUnionType | |
} from 'graphql'; | |
const randomName = (len) => { | |
let text = ''; | |
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
for (let i = 0; i < len; i++) { | |
text += possible.charAt(Math.floor(Math.random() * possible.length)); | |
} | |
return text; | |
}; | |
const mapToObject = (mainObj, prop, instance) => { | |
switch (instance) { | |
case 'ObjectID': | |
mainObj[prop] = { type: GraphQLString }; | |
break; | |
case 'String': | |
mainObj[prop] = { type: GraphQLString }; | |
break; | |
case 'Date': | |
mainObj[prop] = { type: GraphQLString }; | |
break; | |
case 'Mixed': | |
mainObj[prop] = { type: GraphQLString }; | |
break; | |
case 'Boolean': | |
mainObj[prop] = { type: GraphQLBoolean }; | |
break; | |
case 'Buffer': | |
mainObj[prop] = { type: GraphQLBoolean }; | |
break; | |
case 'Number': | |
mainObj[prop] = { type: GraphQLInt }; | |
break; | |
case 'Array': | |
mainObj[prop] = { type: new GraphQLList(GraphQLString) }; | |
break; | |
} | |
return mainObj; | |
}; | |
function createType(args) { | |
if (args.schema && args.schema.paths) { | |
let fieldsReturn = {}; | |
const GQLS = { | |
name: args.name, | |
description: args.description, | |
fields: () => {}, | |
}; | |
const tmpArgsObj = { ...args.schema.paths }; | |
const newSchemaObject = {}; | |
const noChildSchema = {}; | |
for (const key in tmpArgsObj) { | |
if (tmpArgsObj[key].hasOwnProperty('schema')) { | |
newSchemaObject[key] = tmpArgsObj[key]; | |
tmpArgsObj[key] = {}; | |
} | |
} | |
for (const key in tmpArgsObj) { | |
if (tmpArgsObj.hasOwnProperty(key) && !tmpArgsObj[key].schema) { | |
noChildSchema[key] = tmpArgsObj[key]; | |
} | |
} | |
Object.keys(noChildSchema).forEach((k) => { | |
if (!noChildSchema[k].hasOwnProperty('schema')) { | |
const path = k.split('.'); | |
const last = path.pop(); | |
if (path.length) { | |
path.reduce((r, a) => r[a] = r[a] || {}, noChildSchema)[last] = noChildSchema[k]; | |
path.reduce((r, a) => r[a] = r[a] || {}, noChildSchema)[last].path = last; | |
delete noChildSchema[k]; | |
} | |
} | |
}); | |
for (const key in noChildSchema) { | |
if (noChildSchema.hasOwnProperty(key) && !newSchemaObject.hasOwnProperty(key)) { | |
newSchemaObject[key] = noChildSchema[key]; | |
} | |
} | |
for (const key in newSchemaObject) { | |
if (newSchemaObject.hasOwnProperty(key)) { | |
if (!newSchemaObject[key].hasOwnProperty('instance')) { | |
const subArgs = { | |
name: `${key}SubType_${randomName(10)}`, | |
description: `sub-object type for ${key}`, | |
class: args.class, | |
schema: { paths: newSchemaObject[key] }, | |
exclude: args.exclude, | |
}; | |
fieldsReturn[key] = { type: createType(subArgs) }; | |
} else if (newSchemaObject[key].schema) { | |
const subArgs = { | |
name: `${newSchemaObject[key].path}SubType_${randomName(10)}`, | |
description: `sub-object type for ${args.name}`, | |
class: args.class, | |
schema: newSchemaObject[key].schema, | |
exclude: args.exclude, | |
}; | |
const typeElement = createType(subArgs); | |
fieldsReturn[key] = { type: new GraphQLList(typeElement) }; | |
} else if ( | |
newSchemaObject[key] && | |
newSchemaObject[key].path && | |
newSchemaObject[key].instance && | |
newSchemaObject[key].path !== '__v' && !newSchemaObject[key].schema | |
) { | |
fieldsReturn = mapToObject( | |
fieldsReturn, | |
newSchemaObject[key].path, | |
newSchemaObject[key].instance, | |
newSchemaObject); | |
} | |
} | |
} | |
if (args.exclude) { | |
args.exclude.forEach((prop) => { | |
if (fieldsReturn[prop]) { | |
delete fieldsReturn[prop]; | |
} | |
}); | |
} | |
if (args.extend) { | |
fieldsReturn = { ...fieldsReturn, ...args.extend }; | |
} | |
// to support old version | |
if (args.props) { | |
fieldsReturn = { ...fieldsReturn, ...args.props }; | |
} | |
GQLS.fields = () => fieldsReturn; | |
if (args.class === 'GraphQLObjectType') { | |
return new GraphQLObjectType(GQLS); | |
} else if (args.class === 'GraphQLInputObjectType') { | |
return new GraphQLInputObjectType(GQLS); | |
} else if (args.class === 'GraphQLInterfaceType') { | |
return new GraphQLInterfaceType(GQLS); | |
} else if (args.class === 'GraphQLUnionType') { | |
return new GraphQLUnionType(GQLS); | |
} else if (args.class === 'GraphQLEnumType') { | |
return new GraphQLEnumType(GQLS); | |
} | |
return new SyntaxError('Enter correct graphQL class name.'); | |
} | |
} | |
export default createType; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment