Input type in apollo graphql isnt working properly

547 Views Asked by At

I am using apollo graphql and for creating new data a want to use input type data. I found documentation about this https://www.apollographql.com/docs/apollo-server/schema/schema/#input-types but there is nowhere about how to call it from resolvers. This is my code:

    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}`);

On my localhost:4000 I am making query to create new course like this:

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"
  }
}

But result of adding new course is always null. I think there is problem with resolver but even I did research I couldn't find the solution. If someone knows where is problem please post answer and help me!

You can look at imageenter image description here

Thank you

0

There are 0 best solutions below