-
When I started Spring Boot and GraphQL API creating process I faced issue related to Annotation. @component. ### QueryResolver .class_ public class TotalQueryResolver implements GraphQLQueryResolver {
private final CategoryRepository categoryRepository;
public Iterable<Category> allCategories(){
return categoryRepository.findAll();
}
public Category category(Integer id){
return categoryRepository.findById(id).orElseGet(null);
}
} MutationResolver.class @Component
public class TotalMutationResolver implements GraphQLMutationResolver {
@Autowired
private CategoryRepository categoryRepository;
public Category addCategory(String name) {
Category category = new Category();
category.setName(name);
return categoryRepository.saveAndFlush(category);
}
public List<Category> allCategories(){
return categoryRepository.findAll();
}
public Category updateCategory(Long id, String name){
Category category = new Category();
category.setCategory_id(id);
category.setName(name);
return categoryRepository.saveAndFlush(category);
}
public Boolean deleteCategory(Integer id){
categoryRepository.deleteById(id);
return true;
}
} When I compile the source no any error, however in broweser. (404 error) type Query {
allCategories: [Category]
category(id: ID!): Category
}
type Mutation {
addCategory(name: String!): Category!
updateCategory(id: ID!, name: String!): Category!
deleteCategory(id: ID!): Boolean
}
type Category {
id: ID
name: String!
} In attachment figure, after adding @component result; |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
I don't understand what are you asking. Can you paste the original exception with the stack-trace here instead of a screenshot from IntelliJ? |
Beta Was this translation helpful? Give feedback.
-
@vojtapol thank you for response. I attached here. Regarding issues in the code . @RequiredArgsConstructor
@Component
public class TotalQueryResolver implements GraphQLQueryResolver {
private final CategoryRepository categoryRepository;
(same as before source code)
} @Component
public class TotalMutationResolver implements GraphQLMutationResolver {
@Autowired
private CategoryRepository categoryRepository;
} after compile my source code.
|
Beta Was this translation helpful? Give feedback.
-
More .... source here. |
Beta Was this translation helpful? Give feedback.
-
The exception is telling you that there is no resolver method or field called This is the POJO: @Data
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
@Entity
@Table(name = "category")
public class Category implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false, unique = true)
private Long category_id;
private String name;
private String value;
private String type;
private String icon;
private String slug;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "category_id")
private Collection<Product> products = new ArrayList<>();
} You have two options:
|
Beta Was this translation helpful? Give feedback.
-
@vojtapol |
Beta Was this translation helpful? Give feedback.
The exception is telling you that there is no resolver method or field called
id
orgetId()
on theCategory
type.This is the POJO: