0
votes

how can i work with resolvers for mutations after i create type Mutations in graphql-yoga?

i've tried to create resolvers for mutations, but when i run in graph playground, i the code return error.

Playground

and here's my code:

const { GraphQLServer } = require('graphql-yoga')

// 1
const typeDefs = `
type Query {
    users: [User!]!
    user(id: ID!): User
  }

  type Mutation {
    createUser(name: String!): User!
  }

  type User {
    id: ID!
    name: String!
  }
`

// 2
const resolvers = {
    Query: {
      users: () => User,
    },
    Mutation: {
      // WHAT SHOULD I WRITE IN HERE?
    }
  }

// 3
const server = new GraphQLServer({
  typeDefs,
  resolvers,
})
server.start(() => console.log(`Server is running on http://localhost:4000`))

if someone know how can i do for resolvers mutation, can shared with me? thanks

2
What did you try? Mutation resolver looks empty.Raeesaa

2 Answers

0
votes

Resolver for createUser can be defined as follows:

const resolvers = {
    Query: {
      // Query resolvers
    },
    Mutation: {
      createUser: (parent, args) => {
         // Business logic. Maybe save record in database
         // Return created user. I am returning dummy data for now, so that you can test it in playground
         return {id: 1, name: "John} 
      }
    }
  }
-1
votes

Finally it works for me. i used this:

const resolvers = {
  Query: {
    users: () => User
  },
  Mutation: {
    createUser: (source, {input}) => {
        let newUser = [];
        newUser.id = id;
        newUser.name = input.name;

        User.push(newUser);
        return newUser;
    }
  }
}