support sub-document type
Support sub-documents:
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
children: [childSchema]
})
Here is the hack I'm using to add subdocuments after I get the types back from graffiti-mongoose.
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])
})
}
}
}
}
In case it helps whomever is implementing this.
@brysgo thanks, but I mean under subdocument when it is:
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
children: [childSchema]
})
For Array of ObjectId references graffiti works, like this:
var personSchema = Schema({
_id : Number,
name : String,
age : Number,
stories : [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});
var storySchema = Schema({
_creator : { type: Number, ref: 'Person' },
title : String,
fans : [{ type: Number, ref: 'Person' }]
});
var Story = mongoose.model('Story', storySchema);
var Person = mongoose.model('Person', personSchema);
I guess I misunderstood. I was unable to query through an objectid reference before. Is that supported? Where can I find an example?
@brysgo https://github.com/RisingStack/graffiti-example but I just realised that it's only array of refs. Simple ref is missing, I'm going to do during this weekend.
Thanks!
@brysgo done. ObjectID support with reference is finished and released as v1.6.0.
+1 for support for sub document and object type
Hey, first thanks for this report. Do you have any update of this issue?
FWIW I think this is fixed in https://github.com/RisingStack/graffiti-mongoose/commit/c473304dbbf14792e6ba65fdffdcec3f929187f6
Supporting EmbeddedSchemas, Enum, InputObjectType in mutations:
see https://github.com/RisingStack/graffiti-mongoose/pull/104
Just wait when PR was accepted, or fork my branch https://github.com/nodkz/graffiti-mongoose
PS. I hate tests ;) and it difficult for me to write them. So if somebody add tests for my changes, it will be cool. Thanks!
What did I just read... @nodkz
PS. I hate tests ;) and it difficult for me to write them. So if somebody add tests for my changes, it will be cool. Thanks!
