1

I have a Video Model set up with mongoose in ../models/video It looks like this

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const videoSchema = new Schema({
  title: String,
  video: String,
  slug: String,
});
module.exports = mongoose.model('Video', videoSchema);

In my main Schema file I have a VideoType set up like so

const Video = require('../models/video');
const VideoType = new GraphQLObjectType({
  name: 'video',
  fields:() => ({
    id: {type: GraphQLID},
    title: {type: GraphQLString},
    video: {type: GraphQLString},
    slug: {type: GraphQLString}
  })
});

My root query has one field

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    video: {
      type: VideoType,
      args: {id:{type: GraphQLString}},
      resolve(parent,args){
          return Video.findById(args.id);
      }
    }
  }
});

When I change it to id and findById it brings the object in and the reults are as follows

Query

{
  video(id:"9d1938831e9d347837b815c3"){
    title
    video
    slug
  }
}

Reposnse

{
  "data": {
    "video": {
      "title": "GraphQL Tutorial",
      "video": "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/jflhB57loAU\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>",
      "slug": "graphql-tutorial"
    }
  }
}

When I change it back to slug I use

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    video: {
      type: VideoType,
      args: {slug:{type: GraphQLString}},
      resolve(parent,args){
          return Video.find({slug: args.slug})
      }
    }
  }
});

And I query

{
  video(slug:"graphql-tutorial"){
    title
    video
    slug
  }
}

I get this reposnse

{
  "data": {
    "video": {
      "title": null,
      "video": null,
      "slug": null
    }
  }
}

I'm querying the same slug, but I'm not getting the same result. Does anyone know why?

h3xDev3
  • 11
  • 1
  • Unlike `findById`, `find` returns a Promise that resolves in an array, not a single object. See the dupe question for additional details. – Daniel Rearden Jan 04 '20 at 09:08
  • @DanielRearden that article talks about a lot of stuff. Stuff that's not relevant to what I'm trying to achieve. Maybe there is an answer in there somewhere but is there a simple fix to this? – h3xDev3 Jan 04 '20 at 11:07
  • See Common Scenario #2. You should be returning a single object instead of an array since the type of your field is not a List. – Daniel Rearden Jan 04 '20 at 13:39

0 Answers0