-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade to Spring for GraphQL 1.3 and use built-in federation support (…
…#80) Spring for GraphQL 1.3 adds [built-in federation support](https://spring.io/blog/2024/02/21/spring-for-graphql-1-3-m1-released#apollo-federation) with annotated, controller methods. This PR contains the changes to switch to the built-in support. Also some minor refactoring. I know 1.3 is not due until May 21, and Spring Boot 3.3 until May 23, but I wanted to have the changes ready and make sure all works as expected. For now this is based on release candidates.
- Loading branch information
1 parent
f88fbb2
commit ed6b7a6
Showing
16 changed files
with
122 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
products-subgraph/src/main/java/com/example/products/GraphQLConfiguration.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 8 additions & 17 deletions
25
products-subgraph/src/main/java/com/example/products/ProductsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,25 @@ | ||
package com.example.products; | ||
|
||
import java.util.List; | ||
|
||
import com.example.products.model.Product; | ||
import com.example.products.model.ProductSource; | ||
|
||
import org.springframework.graphql.data.method.annotation.Argument; | ||
import org.springframework.graphql.data.method.annotation.QueryMapping; | ||
import org.springframework.stereotype.Controller; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
@Controller | ||
public class ProductsController { | ||
|
||
private final Map<String, Product> PRODUCTS = Stream.of( | ||
new Product("1","Saturn V", "The Original Super Heavy-Lift Rocket!"), | ||
new Product("2","Lunar Module"), | ||
new Product("3","Space Shuttle"), | ||
new Product("4","Falcon 9", "Reusable Medium-Lift Rocket"), | ||
new Product("5","Dragon", "Reusable Medium-Lift Rocket"), | ||
new Product("6","Starship", "Super Heavy-Lift Reusable Launch Vehicle") | ||
).collect(Collectors.toMap(Product::id, product -> product)); | ||
|
||
@QueryMapping | ||
public Product product(@Argument String id) { | ||
return PRODUCTS.get(id); | ||
public Product product(@Argument Long id) { | ||
return ProductSource.getProduct(id); | ||
} | ||
|
||
@QueryMapping | ||
public List<Product> products() { | ||
return PRODUCTS.values().stream().toList(); | ||
return ProductSource.getProducts(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
products-subgraph/src/main/java/com/example/products/model/ProductSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.example.products.model; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
public final class ProductSource { | ||
|
||
private static final List<Product> productList = List.of( | ||
new Product(1L, "Saturn V", "The Original Super Heavy-Lift Rocket!"), | ||
new Product(2L, "Lunar Module"), | ||
new Product(3L, "Space Shuttle"), | ||
new Product(4L, "Falcon 9", "Reusable Medium-Lift Rocket"), | ||
new Product(5L, "Dragon", "Reusable Medium-Lift Rocket"), | ||
new Product(6L, "Starship", "Super Heavy-Lift Reusable Launch Vehicle") | ||
); | ||
|
||
private static final Map<Long, Product> productMap = | ||
productList.stream().collect(Collectors.toMap(Product::id, product -> product)); | ||
|
||
public static Product getProduct(Long id) { | ||
return productMap.get(id); | ||
} | ||
|
||
public static List<Product> getProducts() { | ||
return productList; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,6 @@ spring: | |
graphql: | ||
graphiql: | ||
enabled: true | ||
logging: | ||
level: | ||
org.springframework.graphql: DEBUG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 0 additions & 44 deletions
44
reviews-subgraph/src/main/java/com/example/reviews/GraphQLConfiguration.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 14 additions & 13 deletions
27
reviews-subgraph/src/main/java/com/example/reviews/ReviewsController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,27 @@ | ||
package com.example.reviews; | ||
|
||
import com.example.reviews.model.Review; | ||
import java.util.List; | ||
|
||
import com.example.reviews.model.Product; | ||
import com.example.reviews.model.Review; | ||
import com.example.reviews.model.ReviewSource; | ||
|
||
import org.springframework.graphql.data.federation.EntityMapping; | ||
import org.springframework.graphql.data.method.annotation.Argument; | ||
import org.springframework.graphql.data.method.annotation.SchemaMapping; | ||
import org.springframework.stereotype.Controller; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Controller | ||
public class ReviewsController { | ||
|
||
private final Map<String, List<Review>> REVIEWS = Map.of( | ||
"2", List.of(new Review("1020", "Very cramped :( Do not recommend.", 2), new Review("1021", "Got me to the Moon!", 4)), | ||
"3", List.of(new Review("1030", 3)), | ||
"4", List.of(new Review("1040", 5), new Review("1041", "Reusable!", 5), new Review("1042", 5)), | ||
"5", List.of(new Review("1050", "Amazing! Would Fly Again!", 5), new Review("1051", 5)) | ||
); | ||
@EntityMapping | ||
public Product product(@Argument Long id) { | ||
return new Product(id); | ||
} | ||
|
||
@SchemaMapping | ||
public List<Review> reviews(Product show) { | ||
return REVIEWS.getOrDefault(show.id(), Collections.emptyList()); | ||
public List<Review> reviews(Product product) { | ||
return ReviewSource.getReviews(product); | ||
} | ||
|
||
} |
2 changes: 1 addition & 1 deletion
2
reviews-subgraph/src/main/java/com/example/reviews/model/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.example.reviews.model; | ||
|
||
public record Product(String id) { | ||
public record Product(Long id) { | ||
public static final String PRODUCT_TYPE = "Product"; | ||
} |
20 changes: 20 additions & 0 deletions
20
reviews-subgraph/src/main/java/com/example/reviews/model/ReviewSource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.example.reviews.model; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public final class ReviewSource { | ||
|
||
private static final Map<Long, List<Review>> reviewMap = Map.of( | ||
2L, List.of(new Review("1020", "Very cramped :( Do not recommend.", 2), new Review("1021", "Got me to the Moon!", 4)), | ||
3L, List.of(new Review("1030", 3)), | ||
4L, List.of(new Review("1040", 5), new Review("1041", "Reusable!", 5), new Review("1042", 5)), | ||
5L, List.of(new Review("1050", "Amazing! Would Fly Again!", 5), new Review("1051", 5)) | ||
); | ||
|
||
public static List<Review> getReviews(Product product) { | ||
return reviewMap.getOrDefault(product.id(), Collections.emptyList()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters