Метод метода расширения GraphQL из общего класса JavaJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Anonymous
 Метод метода расширения GraphQL из общего класса Java

Сообщение Anonymous »

Я переписываю Spring Boot Rest API на GraphQL
У меня есть общий Rest Controller, общий для всех контроллеров

Код: Выделить всё

public class GenericController{

@Autowired
private GenericService genericService;

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity findById(@PathVariable P id) {
return new ResponseEntity(genericService.findById(id), HttpStatus.OK);
}

@RequestMapping(value = "/findAll", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity findAll() {
return new ResponseEntity(genericService.findAll(), HttpStatus.OK);
}

@RequestMapping(value = "/create", method = RequestMethod.POST)
@ResponseBody
public ResponseEntity create(@RequestBody @Valid E e) {
return new ResponseEntity(genericService.save(e), HttpStatus.OK);
}

}
И контроллеры расширяют универсальный контроллер с универсальным типом Entity

Код: Выделить всё

@RestController
@RequestMapping("/Customer")
public class CustomerController extends GenericController implements GraphQLQueryResolver{
public List findAllCustomer(){//This method is only for GraphQL
return findAll();
}
}

Код: Выделить всё

@RestController
@RequestMapping("/Product")
public class ProductController extends GenericController implements GraphQLQueryResolver{
public List findAllProduct(){//This method is only for GraphQL
return findAll();
}
}
Схема GraphQL

Код: Выделить всё

type CustomerEntity {
id: ID!
name: String!
email: String!
}

type ProductEndity {
id: ID!
name: String!
price: Int!
}

type Query {
findAllCustomer: [CustomerEntity]
findAllProduct: [ProductEndity]
}
при таком дизайне нет необходимости писать метод CRUD кода в CustomerController/ProductController, но по-прежнему использовать метод CRUD (findById, findAll, create,...) из общего для CustomerEntity и ProductEntity
p>
С помощью RestAPI я использую одно имя метода (findAll) для двух путей /Customer и /Product

Код: Выделить всё

http://localhost:7888/Customer/findAll
http://localhost:7888/Product/findAll
Но с GraphQL мне нужно использовать 2 метода findAllCustomer и findAllProduct

Код: Выделить всё

query {
findAllCustomer{
id name email
}
}

Код: Выделить всё

query {
findAllProduct{
id name price
}
}
Вопрос:
Как я могу использовать одно имя метода (findAll) как для клиента, так и для продукта? Нет необходимости писать отдельные методы для каждого контроллера. , аналогичный RestAPI
Просто так (я так думаю :) )

Код: Выделить всё

query {
Customer{
findAll{
id name email
}
}
}

Код: Выделить всё

query {
Product{
findAll{
id name price
}
}
}
Всем спасибо!

Подробнее здесь: https://stackoverflow.com/questions/739 ... java-class
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»