阿波罗graphql中的输入类型无法正常工作

时间:2020-10-01 08:11:09

标签: graphql apollo-server

我正在使用apollo graphql,并且要创建新数据要使用输入类型数据。 我找到了有关此https://www.apollographql.com/docs/apollo-server/schema/schema/#input-types的文档,但是如何从解析器调用它没有任何地方。 这是我的代码:

    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 Mutation {
        createCourse(input: CourseInput): [Course]
    }
    `;
    var coursesData = [
        {
            id: 1,
            title: 'First one',
            author: 'Andrew Mead, Rob Percival',
            description: 'Learn Node.js',
            topic: 'Node.js',
            url: 'https://example.com'
        },
        {
            id: 2,
            title: 'Node.js, Express & MongoDB Dev to Deployment',
            author: 'Brad Traversy',
            description: 'Learn by example building & deploying reah',
            topic: 'Node.js',
            url: 'https://newbook.com'
        },
    ]
var resolvers= {
Mutation: {
  createCourse:(parent,{input})=>{
    coursesData = [...coursesData, input];
    console.log("input je" ,input)
    console.log("coursesdata" ,coursesData)
    return coursesData;
  }
},
};
const server = new ApolloServer({ typeDefs, resolvers,tracing:true });

// The `listen` method launches a web server.
server.listen().then(({ url }) => {
  console.log(`?  Server ready at ${url}`);

在我的本地主机:4000上,我正在查询以创建新课程,如下所示:

mutation createCourse($input: CourseInput) {
  createCourse(input: $input) {
    id
    title
    author
    description
    topic
    url
  }
}
{
  "input": {
    "id": 4,
    "title": "ovo je novi kurs",
    "author": "delila",
    "description": "neki novi description",
    "topic": "programming",
    "url": "google.com"
  }
}

但是添加新课程的结果始终为null。我认为解析器存在问题,但是即使我做了研究,也找不到解决方案。如果有人知道问题出在哪里,请发布答案并帮助我!

您可以查看图片enter image description here

谢谢

0 个答案:

没有答案