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,58 @@
/*
* 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 reactor.core.publisher.Mono;
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 Mono<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,58 +0,0 @@
/*
* 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.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", wiring ->
wiring.dataFetcher("employees", env -> this.employeeService.getAllEmployees())
);
builder.type("Employee", wiring ->
wiring.dataFetcher("salary", env -> this.salaryService.getSalaryForEmployee(env.getSource()))
);
builder.type("Mutation", wiring ->
wiring.dataFetcher("updateSalary", env -> {
Map<String, String> input = env.getArgument("input");
String employeeId = input.get("employeeId");
BigDecimal newSalary = new BigDecimal(input.get("salary"));
this.salaryService.updateSalary(employeeId, newSalary);
return null;
})
);
}
}

View File

@@ -0,0 +1,54 @@
/*
* 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 reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.graphql.data.method.annotation.GraphQlController;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
@GraphQlController
public class SampleController {
private final DataRepository repository;
public SampleController(DataRepository dataRepository) {
this.repository = dataRepository;
}
@QueryMapping
public String greeting() {
return this.repository.getBasic();
}
@QueryMapping
public Mono<String> greetingMono() {
return this.repository.getGreeting();
}
@QueryMapping
public Flux<String> greetingsFlux() {
return this.repository.getGreetings();
}
@SubscriptionMapping
public Flux<String> greetings() {
return this.repository.getGreetingsStream();
}
}

View File

@@ -1,41 +0,0 @@
/*
* 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 graphql.schema.idl.RuntimeWiring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.graphql.boot.RuntimeWiringBuilderCustomizer;
import org.springframework.stereotype.Component;
@Component
public class SampleWiring implements RuntimeWiringBuilderCustomizer {
private final DataRepository repository;
public SampleWiring(@Autowired DataRepository dataRepository) {
this.repository = dataRepository;
}
@Override
public void customize(RuntimeWiring.Builder wiringBuilder) {
wiringBuilder.type("Query", builder -> builder.dataFetcher("greeting", env -> this.repository.getBasic()));
wiringBuilder.type("Query", builder -> builder.dataFetcher("greetingMono", env -> this.repository.getGreeting()));
wiringBuilder.type("Query", builder -> builder.dataFetcher("greetingsFlux", env -> this.repository.getGreetings()));
wiringBuilder.type("Subscription", builder -> builder.dataFetcher("greetings", env -> this.repository.getGreetingsStream()));
}
}

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;
})
);
}
}

View File

@@ -0,0 +1,34 @@
/*
* 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.greeting;
import org.springframework.graphql.data.method.annotation.GraphQlController;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
@GraphQlController
public class GreetingController {
@QueryMapping
public String greeting() {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return "Hello " + attributes.getAttribute(RequestAttributeFilter.NAME_ATTRIBUTE, SCOPE_REQUEST);
}
}

View File

@@ -1,23 +0,0 @@
package io.spring.sample.graphql.greeting;
import graphql.schema.idl.RuntimeWiring;
import org.springframework.graphql.boot.RuntimeWiringBuilderCustomizer;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import static org.springframework.web.context.request.RequestAttributes.SCOPE_REQUEST;
@Component
public class GreetingDataWiring implements RuntimeWiringBuilderCustomizer {
@Override
public void customize(RuntimeWiring.Builder builder) {
builder.type("Query", typeWiring -> typeWiring.dataFetcher("greeting", env -> {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return "Hello " + attributes.getAttribute(RequestAttributeFilter.NAME_ATTRIBUTE, SCOPE_REQUEST);
}));
}
}

View File

@@ -0,0 +1,44 @@
/*
* 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.project;
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.QueryMapping;
@GraphQlController
public class ProjectController {
private final SpringProjectsClient client;
public ProjectController(SpringProjectsClient client) {
this.client = client;
}
@QueryMapping
public Project project(@Argument String slug) {
return client.fetchProject(slug);
}
@SchemaMapping
public List<Release> releases(Project project) {
return client.fetchProjectReleases(project.getSlug());
}
}

View File

@@ -1,27 +0,0 @@
package io.spring.sample.graphql.project;
import graphql.schema.idl.RuntimeWiring;
import org.springframework.graphql.boot.RuntimeWiringBuilderCustomizer;
import org.springframework.stereotype.Component;
@Component
public class ProjectDataWiring implements RuntimeWiringBuilderCustomizer {
private final SpringProjectsClient client;
public ProjectDataWiring(SpringProjectsClient client) {
this.client = client;
}
@Override
public void customize(RuntimeWiring.Builder builder) {
builder.type("Query", typeWiring -> typeWiring.dataFetcher("project", env -> {
String slug = env.getArgument("slug");
return client.fetchProject(slug);
})).type("Project", typeWiring -> typeWiring.dataFetcher("releases", env -> {
Project project = env.getSource();
return client.fetchProjectReleases(project.getSlug());
}));
}
}