graffiti-mongoose icon indicating copy to clipboard operation
graffiti-mongoose copied to clipboard

support sub-document type

Open hekike opened this issue 10 years ago • 11 comments

Support sub-documents:

var childSchema = new Schema({ name: 'string' });

var parentSchema = new Schema({
  children: [childSchema]
})

hekike avatar Jul 27 '15 07:07 hekike

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 avatar Jul 31 '15 16:07 brysgo

@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);

hekike avatar Aug 01 '15 07:08 hekike

I guess I misunderstood. I was unable to query through an objectid reference before. Is that supported? Where can I find an example?

brysgo avatar Aug 01 '15 13:08 brysgo

@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.

hekike avatar Aug 01 '15 13:08 hekike

Thanks!

brysgo avatar Aug 01 '15 22:08 brysgo

@brysgo done. ObjectID support with reference is finished and released as v1.6.0.

hekike avatar Aug 02 '15 08:08 hekike

+1 for support for sub document and object type

ivawzh avatar Oct 20 '15 21:10 ivawzh

Hey, first thanks for this report. Do you have any update of this issue?

AbrahamAlcaina avatar Nov 30 '15 11:11 AbrahamAlcaina

FWIW I think this is fixed in https://github.com/RisingStack/graffiti-mongoose/commit/c473304dbbf14792e6ba65fdffdcec3f929187f6

jashmenn avatar Dec 23 '15 22:12 jashmenn

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!

nodkz avatar Mar 23 '16 08:03 nodkz

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!

wait...

tothandras avatar Mar 23 '16 09:03 tothandras