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?