任何人请帮助pppp!我正在尝试使用 TMDB 电影 api 来发布评级,请查看此处的链接以获取他们的文档:https://developer.themoviedb.org/reference/movie-add- rating,但是我面临一个问题,它一直存在当我实现 Graphql 代码进行 Mutation 时,显示以下错误,我该如何解决它:
"errors": [
{
"message": "String cannot represent value: { success: false, status_code: 34, status_message: \"The resource you requested could not be found.\" }",
"locations": [
{
"line": 4,
"column": 5
}
],
"path": [
"addMovieRatings",
"message"
],
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
"stacktrace": [
"GraphQLError: String cannot represent value: { success: false, status_code: 34, status_message: \"The resource you requested could not be found.\" }",
" at GraphQLScalarType.serialize
这是我的 3 个代码文件:
schema.js:
type Rating {
id: ID!
rating: Float!
}
type AddMovieRatingsResponse {
code: Int!
success: Boolean!
message: String!
rating: Rating
}
type Mutation {
addMovieRatings(id: ID!, rating: Float!): AddMovieRatingsResponse!
}
电影-api.js:
class MovieAPI extends RESTDataSource {
baseURL = `https://api.themoviedb.org/3/`;
async addMovieRatings(id, rating) {
const response = await this.patch(
`movie/${id}/rating?api_key=${process.env.API_KEY}`,
{ rating }
);
return response;
}
}
module.exports = MovieAPI;
resolvers.js:
const resolvers = {
Mutation: {
addMovieRatings: async (_, { id, rating }, { dataSources }) => {
try {
const result = await dataSources.movieAPI.addMovieRatings(id, rating);
console.log(result);
return {
code: 200,
success: true,
message: `Successfully added rating for movie with id: ${id}`,
rating: result.rating,
};
} catch (err) {
return {
code: err.extensions.response.status,
success: false,
message: err.extensions.response.body,
rating: null,
};
}
},
},
}
线索就在错误消息中:
“找不到您请求的资源。”
您没有在您的
patch
: 中包含服务器的基本 URL
改变:
class MovieAPI extends RESTDataSource {
baseURL = `https://api.themoviedb.org/3/`;
async addMovieRatings(id, rating) {
const response = await this.patch(
`movie/${id}/rating?api_key=${process.env.API_KEY}`,
{ rating }
);
return response;
}
}
至:
class MovieAPI extends RESTDataSource {
baseURL = `https://api.themoviedb.org/3/`;
async addMovieRatings(id, rating) {
const response = await this.patch(
`${baseURL}movie/${id}/rating?api_key=${process.env.API_KEY}`,
{ rating }
);
return response;
}
}