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

Help needed: support null values of referenced fields #89

Closed
rturk opened this issue Feb 2, 2016 · 3 comments
Closed

Help needed: support null values of referenced fields #89

rturk opened this issue Feb 2, 2016 · 3 comments

Comments

@rturk
Copy link

rturk commented Feb 2, 2016

Need help with null values when using reference fields.

I have a NewsFeed schema that contains a Product, or a Post, or a Video. Defined as follows:

NewsFeed = new mongoose.Schema({
  date: { type: Date, required: true, default: Date.now },
  product: { type: ObjectId, ref: 'Product', required: false},
  post: { type: ObjectId, ref: 'Post', required: false },
  video: { type: ObjectId, ref: 'Post', required: false }, 
});

This means that aNewsFeed document contains a date and one element (one of product, post or video).

Ideally I what to use the GraphQL query bellow and fetch my newsfeed ordered by date and containing the applicable newsfeed element with the other elements absent or null.

query{
  viewer{
    homenewsfeeds(first:5,orderBy:DATE_DESC){
      count,
      edges{
        node{
          date,
          product {
            id,
            name
          },
          post{
            id,
            title,
            author {
              id,
              name
            }
          }
          video{
            id,
            title,
            duration,
          }
        }
      }
    }
  }
}

However I get the following error:

"statusCode": 400,
  "error": "Bad Request",
  "message": "Cannot read property 'toString' of null\nCannot read property 'toString' of null\nCannot read property 'toString' of null\nCannot read property 'toString' of null\nCannot read property 'toString' of null" 

My understating is that GraphQl is trying to load the referenced object even if is not in the current element.

I've already attempted to re-create the newsfeed forcing the elements to be required defaulting to null, with no success. I'm getting the same error

NewsFeed = new mongoose.Schema({
  date: { type: Date, required: true, default: Date.now },
  product: { type: ObjectId, ref: 'Product', required: true, default: null},
  post: { type: ObjectId, ref: 'Post', required: true, default: null},
  video: { type: ObjectId, ref: 'Post', required: true, default: null }, 
});

Another option would be refactor the NewsFeed as described bellow. However this will defeat the purpose of simpler GraphQL queries.

NewsFeed = new mongoose.Schema({
  date: { type: Date, required: true, default: Date.now },
  target: { type: ObjectId},
  type: { type: String, enum: ['Post','Product','Video']},
});

Comments? This is a limitation of GraphQL or current Mongoose-GraphQl implementation?

@rturk
Copy link
Author

rturk commented Feb 2, 2016

@sibeliusseraphini

@rturk
Copy link
Author

rturk commented Mar 28, 2016

I'll test this again latter this week using latest releases..

@rturk
Copy link
Author

rturk commented Apr 4, 2016

With #100 my use case described above works

@rturk rturk closed this as completed Apr 4, 2016
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

Successfully merging a pull request may close this issue.

1 participant