Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating nested objects on a Schema #110

Open
DaveyEdwards opened this issue Apr 11, 2016 · 0 comments
Open

Creating nested objects on a Schema #110

DaveyEdwards opened this issue Apr 11, 2016 · 0 comments

Comments

@DaveyEdwards
Copy link

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'
    }],
  }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant