Skip to content

Instantly share code, notes, and snippets.

@brysgo
Created July 31, 2015 16:31
Show Gist options
  • Save brysgo/c4abb28928be7df19cd6 to your computer and use it in GitHub Desktop.
Save brysgo/c4abb28928be7df19cd6 to your computer and use it in GitHub Desktop.
Hack to add subdocuments to graffiti schema.
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;
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