Created
July 31, 2015 16:31
-
-
Save brysgo/c4abb28928be7df19cd6 to your computer and use it in GitHub Desktop.
Hack to add subdocuments to graffiti schema.
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 {getTypes} from '@risingstack/graffiti-mongoose'; | |
import Models, { User } from '../mongoose'; | |
import Subdocuments from './subdocuments'; | |
let types = getTypes(Models) | |
let UserType = types.User; | |
// FIXME: Hack subdocument for now | |
Object.assign(UserType._typeConfig.fields, { | |
bestFriend: Subdocuments.singular(User, UserType), | |
friends: Subdocuments.plural(User, UserType), | |
}); | |
module.exports = types; |
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 ObjectID from 'bson-objectid'; | |
import { | |
GraphQLList, | |
} from 'graphql' | |
export default { | |
plural: (model, type) => { | |
return { | |
type: new GraphQLList(type), | |
resolve: (modelInstance, params, source, fieldASTs) => { | |
let ids = modelInstance[fieldASTs.name.value].map(ObjectID); | |
return model.find({ _id: { '$in': ids } }); | |
} | |
} | |
}, | |
singular: (model, type) => { | |
return { | |
type: type, | |
resolve: (modelInstance, params, source, fieldASTs) => { | |
return model.findOne({ | |
_id: ObjectID(modelInstance[fieldASTs.name.value]) | |
}) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment