Ошибка GraphQL - this.findById не является функцией - PullRequest
1 голос
/ 04 марта 2020

Привет, ребята. Я пытаюсь добавить текст к своим песням с помощью мутации, но он возвращает ошибку this.findById is not a function. Формат мутаций правильный, так как я следую учебному пособию, однако он довольно старый, поэтому я не уверен, что это фактор ...

Screenshot of mutation

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;

Не совсем уверен, достаточно ли файлов для решить эту ошибку, если не полное репо здесь - https://github.com/puyanwei/lyrical-graphql

...