Skip to content

Instantly share code, notes, and snippets.

@ssomnoremac
Created June 6, 2018 14:24
Show Gist options
  • Save ssomnoremac/ce399411e63c644f746a69f43c808cfe to your computer and use it in GitHub Desktop.
Save ssomnoremac/ce399411e63c644f746a69f43c808cfe to your computer and use it in GitHub Desktop.
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