Created
June 6, 2018 14:24
-
-
Save ssomnoremac/ce399411e63c644f746a69f43c808cfe to your computer and use it in GitHub Desktop.
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
import { | |
GraphQLObjectType, GraphQLList, GraphQLString, GraphQLInt, GraphQLBoolean, | |
} from 'graphql' | |
import {attributeFields, resolver} from 'graphql-sequelize'; | |
import db from '../../models' | |
module.exports = modelName => modelRelationFunc => { | |
const relationMethods = { | |
listOf: (gqlRelationType, options={}) => ({ | |
[gqlRelationType.name]: { | |
type: new GraphQLList(gqlRelationType), | |
resolve: resolver(db[modelName][gqlRelationType.name], options) | |
} | |
}), | |
single: (gqlRelationType, options={}, sequelizeRelationName) => { | |
const relationName = sequelizeRelationName || gqlRelationType.name.slice(0, -1) | |
return{ | |
[relationName]: { | |
type: gqlRelationType, | |
resolve: resolver(db[modelName][relationName], options) | |
} | |
} | |
} | |
} | |
return new GraphQLObjectType({ | |
name: modelName, | |
fields: () => ({ | |
...attributeFields(db[modelName], {}), | |
...modelRelationFunc | |
? modelRelationFunc(relationMethods).reduce((a,b) => ({...a, ...b}), {}) | |
: {} | |
}), | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment