Support for annotated DataFetcher's

Closes gh-61, gh-90
This commit is contained in:
Rossen Stoyanchev
2021-08-04 16:35:57 +01:00
parent c2a996e564
commit 0d0b84dc8a
41 changed files with 2783 additions and 217 deletions

View File

@@ -0,0 +1,56 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.graphql.data.method.annotation.GraphQlController;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
@GraphQlController
public class SalaryController {
private final EmployeeService employeeService;
private final SalaryService salaryService;
public SalaryController(EmployeeService employeeService, SalaryService salaryService) {
this.employeeService = employeeService;
this.salaryService = salaryService;
}
@QueryMapping
public List<Employee> employees() {
return this.employeeService.getAllEmployees();
}
@SchemaMapping
public BigDecimal salary(Employee employee) {
return this.salaryService.getSalaryForEmployee(employee);
}
@MutationMapping
public void updateSalary(@Argument("input") SalaryInput salaryInput) {
String employeeId = salaryInput.getEmployeeId();
BigDecimal salary = salaryInput.getNewSalary();
this.salaryService.updateSalary(employeeId, salary);
}
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.sample.graphql;
import java.math.BigDecimal;
public class SalaryInput {
private String employeeId;
private BigDecimal newSalary;
public String getEmployeeId() {
return employeeId;
}
public void setEmployeeId(String employeeId) {
this.employeeId = employeeId;
}
public BigDecimal getNewSalary() {
return newSalary;
}
public void setNewSalary(BigDecimal newSalary) {
this.newSalary = newSalary;
}
}

View File

@@ -1,48 +0,0 @@
package io.spring.sample.graphql;
import java.math.BigDecimal;
import java.util.Map;
import graphql.schema.idl.RuntimeWiring;
import org.springframework.graphql.boot.RuntimeWiringBuilderCustomizer;
import org.springframework.stereotype.Component;
@Component
public class SampleWiring implements RuntimeWiringBuilderCustomizer {
final EmployeeService employeeService;
final SalaryService salaryService;
public SampleWiring(EmployeeService employeeService, SalaryService salaryService) {
this.employeeService = employeeService;
this.salaryService = salaryService;
}
@Override
public void customize(RuntimeWiring.Builder builder) {
builder.type("Query", wiringBuilder ->
wiringBuilder.dataFetcher("employees", env ->
employeeService.getAllEmployees()
)
);
builder.type("Employee", wiringBuilder ->
wiringBuilder.dataFetcher("salary", env -> {
Employee employee = env.getSource();
return salaryService.getSalaryForEmployee(employee);
})
);
builder.type("Mutation", wiringBuilder ->
wiringBuilder.dataFetcher("updateSalary", env -> {
Map<String, String> input = env.getArgument("input");
String employeeId = input.get("employeeId");
BigDecimal newSalary = new BigDecimal(input.get("salary"));
salaryService.updateSalary(employeeId, newSalary);
return null;
})
);
}
}