【发布时间】:2021-01-16 03:53:27
【问题描述】:
我正在尝试使用 graphql 和 apollo-server 显示一门课程并更新课程主题。 这是我的代码:
const { ApolloServer, gql } = require('apollo-server');
var typeDefs=gql`
input CourseInput {
id: Int
title: String
author: String
description: String
topic: String
url: String
}
type Course {
id: Int
title: String
author: String
description:String
topic:String
url: String
}
type Query {
course(id: Int!): Course
courses(topic:String!):[Course]
allcourses:[Course]
hello: String
}
type Mutation {
updateCourseTopic(id: Int !, topic: String !): Course
createCourse(input: CourseInput): [Course]
deleteCourse(id: Int !): [Course]
}
`;
var coursesData = [
{
id: 1,
title: 'The Complete Node.js Developer Course',
author: 'Andrew Mead, Rob Percival',
description: 'Learn Node.js by building real-world applications with Node, Express, MongoDB, Mocha, and more!',
topic: 'Node.js',
url: 'https://codingthesmartway.com/courses/nodejs/'
},
{
id: 2,
title: 'Node.js, Express & MongoDB Dev to Deployment',
author: 'Brad Traversy',
description: 'Learn by example building & deploying real-world Node.js applications from absolute scratch',
topic: 'Node.js',
url: 'https://codingthesmartway.com/courses/nodejs-express-mongodb/'
},
{
id: 3,
title: 'JavaScript: Understanding The Weird Parts',
author: 'Anthony Alicea',
description: 'An advanced JavaScript course for everyone! Scope, closures, prototypes, this, build your own framework, and more.',
topic: 'JavaScript',
url: 'https://codingthesmartway.com/courses/understand-javascript/'
}
]
var getCourses = function(args){
console.log("delila2")
if(args.topic){
console.log("delila2")
var topic=args.topic;
return coursesData.filter(course=>
course.topic===topic
);
}
else return coursesData
}
var resolvers= {
Query:{
course:getcourse,
courses:getCourses,
allcourses:getAllCourses,
hello: ()=>"Delila"
},
Mutation: {
updateCourseTopic:updateCourseTopic,
createCourse:createCourse,
deleteCourse: deleteCourse,
}
};
function getcourse(args){
var id=args.id;
return coursesData.filter(course=>{
return course.id==id
})[0]
}
function getAllCourses(){
console.log("all courses")
return coursesData;
}
function updateCourseTopic (id, topic){
console.log("id je" ,id)
coursesData.map(course=>{
if(course.id===id){
course.topic=topic;
return course
}
});
console.log("svi", coursesData);
return coursesData.filter(course=>course.id===id)[0]
}
function createCourse(input){
var id = 4;
coursesData = [...coursesData, input.input];
console.log("input je" ,input.input)
console.log("coursesdata" ,coursesData)
//coursesData[id] = input;
return coursesData;
}
function deleteCourse(args){
var id=args;
coursesData.splice(id, 1);
return coursesData;
}
// The ApolloServer constructor requires two parameters: your schema
// definition and your set of resolvers.
const server = new ApolloServer({ typeDefs, resolvers });
// The `listen` method launches a web server.
server.listen().then(({ url }) => {
console.log(`???? Server ready at ${url}`);
});
在我的本地主机服务器 http://localhost:4000 上使用 graphql 和此代码,我正在尝试获取数据以获取一门特定课程并更新课程主题。
获取单门课程:
query getSingleCourse($courseID: Int !){
course(id:$courseID){
title
author
description
url
topic
}
}
{
"courseID": 3
}
更新课程主题:
mutation updateCourse($id: Int!, $topic: String !) {
updateCourseTopic(id:$id, topic:$topic)
{
title
url
description
author
topic
}
}
{
"id": 1,
"topic": "something new"
}
getSingleCourse 的问题在于
“错误”:[ { "message": "无法读取未定义的属性 'id'", “地点”:[ { “线”:2, “列”:3 } ], “小路”: [ “课程” ], “扩展”:{ “代码”:“INTERNAL_SERVER_ERROR”, “例外”: { “堆栈跟踪”: [ "TypeError: 无法读取未定义的属性 'id'", “在getcourse(/Users/admin/newProject/server4.js:116:17)”,
与updateCourse类似,id和topic未定义
{ “数据”:{ “更新课程主题”:空 } }
谢谢
【问题讨论】:
-
console.log("args: ", args) ..... "解析器可以选择接受四个位置参数:(parent, args, context, info) . 详细了解这些参数 args 参数是一个包含所有 GraphQL 参数的对象”
-
所以在解析器中不可能调用另一个函数,例如
course:getCourse?我需要在那里实现它当然:(parent,args,context,info) {...} -
它不是调用 fn,只是定义,将处理程序分配给属性......只是在 fn 解析器中使用正确的 ARGS!第一个参数不是
args...那里没有传递的变量/参数! -
我明白了,谢谢
标签: node.js graphql apollo-server