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

Creating nested objects on a Schema

Open DaveyEdwards opened this issue 9 years ago • 0 comments

Not sure if this is possible right now (or good practice). When I build the schema everything goes fine, but when I try and use GraphiQL to query

page { header { title}}

I get this

{ "statusCode": 400, "error": "Bad Request", "message": "User Error: expected iterable, but did not find one for field Page.header." }

Something like the following is what I tried instead of having every object into its own schema. I'm still relatively new to best practices of mongo, but thought it would make for faster queries. My thinking for this was my collections would be only users and pages. Instead of users, pages, headers, contents, footers... etc.

const PageSchema = new mongoose.Schema({
  header: {
    title: { type: String },
    subtitle: { type: String },
    description: { type: String },

    creator: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'User'
    }
  },
  content: {
    string: { type: String },
    number: { type: Number },
    boolean: { type: Boolean },
  },

  children: [{
    type: mongoose.Schema.Types.ObjectId,
    ref: 'Page'
  }],

  footer: {
    siblings: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Page'
    }],

    parents: [{
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Page'
    }],
  }
});

DaveyEdwards avatar Apr 11 '16 23:04 DaveyEdwards