'GraphQLMutationResolver ignoring requests
I've been a while trying to figure out why this code doesn't work. That's my first time using GraphQL On one side, I got the service from another fully working project, so I don't think that's the problem. When I try to execute the mutation or the query, it responses with a loggin:null or register:null
User.class
@Entity
public class User implements Serializable {
public enum RoleType {USER};
private Long id;
private String userName;
private String password;
private String firstName;
private String lastName;
private String email;
private RoleType role;
public User() {}
public User(String userName, String password, String firstName, String lastName, String email) {
this.userName = userName;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
//Getters and setters
}
UserServiceImpl
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private PermissionChecker permissionChecker;
@Autowired
private BCryptPasswordEncoder passwordEncoder;
@Autowired
private UserDao userDao;
@Override
public User signUp(User user) throws DuplicateInstanceException {
if (userDao.existsByUserName(user.getUserName())) {
throw new DuplicateInstanceException("project.entities.user", user.getUserName());
}
user.setPassword(passwordEncoder.encode(user.getPassword()));
user.setRole(User.RoleType.USER);
userDao.save(user);
return user;
}
@Override
@Transactional(readOnly=true)
public User login(String userName, String password) throws IncorrectLoginException {
Optional<User> user = userDao.findByUserName(userName);
if (!user.isPresent()) {
throw new IncorrectLoginException(userName, password);
}
if (!passwordEncoder.matches(password, user.get().getPassword())) {
throw new IncorrectLoginException(userName, password);
}
return user.get();
}
@Override
@Transactional(readOnly=true)
public User loginFromId(Long id) throws InstanceNotFoundException {
return permissionChecker.checkUser(id);
}
@Override
public User updateProfile(Long id, String firstName, String lastName, String email) throws InstanceNotFoundException {
User user = permissionChecker.checkUser(id);
user.setFirstName(firstName);
user.setLastName(lastName);
user.setEmail(email);
return user;
}
@Override
public void changePassword(Long id, String oldPassword, String newPassword)
throws InstanceNotFoundException, IncorrectPasswordException {
User user = permissionChecker.checkUser(id);
if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
throw new IncorrectPasswordException();
} else {
user.setPassword(passwordEncoder.encode(newPassword));
}
}
}
user.graphqls
type User {
id: Int!,
userName: String,
password: String,
firstName: String,
lastName: String,
email: String,
role: RoleType
}
enum RoleType {
USER,
ADMIN
}
type Query {
login(userName: String, password: String): User
}
type Mutation {
register(
userName: String!,
password: String!,
firstName: String!,
lastName: String!,
email: String!
): User
}
UserMutationResolver
@Component
public class UserMutationResolver implements GraphQLMutationResolver {
@Autowired
private UserService userService;
public User register(final String userName, final String password, final String firstName, final String lastName, final String email) throws DuplicateInstanceException {
System.out.println("bump");
User user = new User(userName, password, firstName, lastName, email);
user = userService.signUp(user);
return user;
}
}
UserQueryResolver
@Component
public class UserQueryResolver implements GraphQLQueryResolver{
@Autowired
private UserService userService;
public UserQueryResolver() {
}
public User login(final String user, final String pass) throws IncorrectLoginException {
return userService.login(user, pass);
}
}
The query and mutation calls
mutation {
register(
userName: "asd",
password: "asd",
firstName: "asd",
lastName: "asd",
email: "[email protected]"
) {
role
}
}
query {
login(userName: "test", password:"test") {
id,
userName
}
}
I've created the user "test" on the DB just in case the register is the only one giving problems, but it fails anyways
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>test.graphql</groupId>
<artifactId>test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test</name>
<description>test</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-graphql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.14.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.graphql</groupId>
<artifactId>spring-graphql-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphiql-spring-boot-starter</artifactId>
<version>5.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>com.coxautodev</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>com.graphql-java</groupId>
<artifactId>graphql-java-tools</artifactId>
<version>5.2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</pluginRepository>
</pluginRepositories>
</project>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
