I’m building a midia tracker with Spring Boot with GraphQL, but I when I try to send a mutation to create a user, flowing error is throw:
java.lang.NullPointerException: Cannot invoke "com.espacogeek.geek.inputs.UserInput.password()" because "userInput" is null
Because when a debug UserInput is null when registerUser() is called, what I doing wrong to GraphQL doesn’t populate UserInput? Can someone help me please?
If you need more information please tell me.
Code:
user.graphqls:
type Query {
findUserByUsername(username: String!): User
findUserById(id: ID!): User
findUserByEmail(email: String!): User
}
type Mutation {
registerUser(user: UserInput!): User
}
type User {
id: ID
username: String
email: String
}
input UserInput {
username: String!
email: String!
password: String!
}
UserInput.java
package com.espacogeek.geek.inputs;
public record UserInput(String username, String email, String password) {}
UserController.java
package com.espacogeek.geek.controllers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.stereotype.Controller;
import com.espacogeek.geek.inputs.UserInput;
import com.espacogeek.geek.modals.UserModal;
import com.espacogeek.geek.services.User.UserService;
import at.favre.lib.crypto.bcrypt.BCrypt;
@Controller
public class UserController {
@Autowired
private UserService userService;
//Some codes...
@MutationMapping
public UserModal registerUser(@Argument UserInput userInput) {
var passwordCrypted = BCrypt.withDefaults().hash(12, userInput.password().toCharArray());
UserModal user = new UserModal(null, userInput.username(), userInput.email(), passwordCrypted);
return userService.save(user);
}
}