我正在尝试使用SPQR和Spring Boot,以下是我的客户模型,服务和控制器。
Customer.java
@Entity
@Table(name = "customer")
public class Customer {
private Long id;
private String firstName;
private String middleName;
private String lastName;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@GraphQLQuery
public Long getId() {
return id;
}
public void setId(final Long aId) {
id = aId;
}
@Column(name = "first_name")
@GraphQLQuery(name = "firstName", description = "Customer's given name")
public String getFirstName() {
return firstName;
}
public void setFirstName(final String aFirstName) {
firstName = aFirstName;
}
@Column(name = "middle_name")
@GraphQLQuery(name = "middleName", description = "Customer's middle name")
public String getMiddleName() {
return middleName;
}
public void setMiddleName(final String aMiddleName) {
middleName = aMiddleName;
}
@Column(name = "last_name")
@GraphQLQuery(name = "lastName", description = "Customer's last/surname name")
public String getLastName() {
return lastName;
}
public void setLastName(final String aLastName) {
lastName = aLastName;
}
}
CustomerServiceImpl.java
@Service
public class CustomerServiceImpl implements CustomerService {
private CustomerRepository customerRepository;
@Autowired
public CustomerServiceImpl(final CustomerRepository aCustomerRepository) {
customerRepository = aCustomerRepository;
}
@GraphQLQuery(name = "customers")
@Override
public List<Customer> getCustomers() {
Iterable<Customer> customers = customerRepository.findAll();
return StreamSupport.stream(customers.spliterator(), true).collect(Collectors.toList());
}
@GraphQLQuery(name = "findCustomer")
@Override
public Customer getCustomer(@GraphQLArgument(name = "id") final Long id) {
Optional<Customer> customer = customerRepository.findById(id);
if (customer.isPresent()) {
return customer.get();
}
return new Customer();
}
@GraphQLQuery(name = "customers")
@Override
public List<Customer> getCustomerByLastName(@GraphQLArgument(name = "lastName") final String lastName) {
List<Customer> customers = customerRepository.findCustomerByLastNameStartingWith(lastName);
return customers;
}
@GraphQLMutation(name = "createCustomer")
@Override
public Customer save(@GraphQLArgument(name = "customer") Customer customer) {
Assert.assertNotNull(customer);
Customer createdCustomer = customerRepository.save(customer);
return createdCustomer;
}
}
控制器
@RestController
public class GraphQLServiceController {
private static final Logger LOGGER = LoggerFactory.getLogger(GraphQLServiceController.class);
private GraphQL graphQL;
@Autowired
public GraphQLServiceController(final CustomerService customerService) {
GraphQLSchema schema = new GraphQLSchemaGenerator()
.withBasePackages("com.example.graphsample")
.withOperationsFromSingleton(customerService)
.generate();
graphQL = GraphQL.newGraphQL(schema).build();
LOGGER.info("Generated GraphQL Schema using SPQR");
}
@PostMapping(value = "/graphql", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Map<String, Object> indexFromAnnotated(@RequestBody Map<String, String> request, HttpServletRequest raw) {
ExecutionResult executionResult = graphQL.execute(ExecutionInput.newExecutionInput()
.query(request.get("query"))
.operationName(request.get("operationName"))
.context(raw)
.build());
return executionResult.toSpecification();
}
}
当我尝试调用变异(createCustomer)时,我得到的回应不足。看来SPQR不会从模型定义的字段中生成CustomerInput?我错过了什么吗?
GraphQL请求:
mutation {
createCustomer(customer: {
firstName: "Grace",
middleName: "John",
lastName: "Doe"
}) {
firstName
middleName
lastName
}
}
响应
{
"errors": [
{
"message": "Validation error of type WrongType: argument 'customer' with value 'ObjectValue{objectFields=[ObjectField{name='firstName', value=StringValue{value='Grace'}}, ObjectField{name='middleName', value=StringValue{value='John'}}, ObjectField{name='lastName', value=StringValue{value='Doe'}}]}' contains a field not in 'CustomerInput': 'firstName' @ 'createCustomer'",
"locations": [
{
"line": 3,
"column": 20
}
]
}
]
}