1

大家好,我正在尝试通过使用突变将歌词添加到我的歌曲中,但它返回错误this.findById is not a function。突变格式是正确的,因为我正在学习教程,但是它已经很老了,所以我不确定这是否是一个因素......

突变截图

song_type.js

const mongoose = require('mongoose');
const graphql = require('graphql');
const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLID,
  GraphQLList
} = graphql;
const LyricType = require('./lyric_type');
const Song = mongoose.model('song');

const SongType = new GraphQLObjectType({
  name: 'SongType',
  fields: () => ({
    id: {
      type: GraphQLID
    },
    title: {
      type: GraphQLString
    },
    lyrics: {
      type: new GraphQLList(LyricType),
      resolve(parentValue) {
        return Song.findLyrics(parentValue.id);
      }
    }
  })
});

module.exports = SongType;

突变.js

const graphql = require('graphql');
const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLID
} = graphql;
const mongoose = require('mongoose');
const Song = mongoose.model('song');
const Lyric = mongoose.model('lyric');
const SongType = require('./song_type');
const LyricType = require('./lyric_type');

const mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
    addSong: {
      type: SongType,
      args: {
        title: {
          type: GraphQLString
        }
      },
      resolve(parentValue, {
        title
      }) {
        return (new Song({
          title
        })).save()
      }
    },
    addLyricToSong: {
      type: SongType,
      args: {
        content: {
          type: GraphQLString
        },
        songId: {
          type: GraphQLID
        }
      },
      resolve(parentValue, {
        content,
        songId
      }) {
        return Song.addLyric(songId, content);
      }
    },
    likeLyric: {
      type: LyricType,
      args: {
        id: {
          type: GraphQLID
        }
      },
      resolve(parentValue, {
        id
      }) {
        return Lyric.like(id);
      }
    },
    deleteSong: {
      type: SongType,
      args: {
        id: {
          type: GraphQLID
        }
      },
      resolve(parentValue, {
        id
      }) {
        return Song.remove({
          _id: id
        });
      }
    }
  }
});

module.exports = mutation;

lyric_type.js

const mongoose = require('mongoose');
const graphql = require('graphql');
const {
  GraphQLObjectType,
  GraphQLList,
  GraphQLID,
  GraphQLInt,
  GraphQLString
} = graphql;
const Lyric = mongoose.model('lyric');

const LyricType = new GraphQLObjectType({
  name: 'LyricType',
  fields: () => ({
    id: {
      type: GraphQLID
    },
    likes: {
      type: GraphQLInt
    },
    content: {
      type: GraphQLString
    },
    song: {
      type: require('./song_type'),
      resolve(parentValue) {
        return Lyric.findById(parentValue).populate('song')
          .then(lyric => {
            console.log(lyric)
            return lyric.song
          });
      }
    }
  })
});

module.exports = LyricType;

不确定这是否有足够的文件来解决这个错误,如果不是完整的 repo 在这里 - https://github.com/puyanwei/lyrical-graphql

4

0 回答 0