forked from BroadleafCommerce/BroadleafCommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Add GraphQL foundation and cart/order resolvers #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
4
commits into
develop-7.0.x
Choose a base branch
from
devin/1776896179-graphql-foundation-and-cart-resolvers
base: develop-7.0.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1a098b2
Add GraphQL foundation and cart/order resolvers
devin-ai-integration[bot] 8dddcdb
Address review: schema enum, ownership check, active cart guard
devin-ai-integration[bot] fb99d83
Address review: nullable PromoCodeResult.order, guard removePromoCode
devin-ai-integration[bot] 354255b
Address review: itemAttributes in AddToCartInput, null-check OfferCod…
devin-ai-integration[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
89 changes: 89 additions & 0 deletions
89
...k-web/src/main/java/org/broadleafcommerce/core/web/graphql/GraphQLContextInterceptor.java
This file contains hidden or 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,89 @@ | ||
| /*- | ||
| * #%L | ||
| * BroadleafCommerce Framework Web | ||
| * %% | ||
| * Copyright (C) 2009 - 2026 Broadleaf Commerce | ||
| * %% | ||
| * Licensed under the Broadleaf Fair Use License Agreement, Version 1.0 | ||
| * (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt) | ||
| * unless the restrictions on use therein are violated and require payment to Broadleaf in which case | ||
| * the Broadleaf End User License Agreement (EULA), Version 1.1 | ||
| * (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt) | ||
| * shall apply. | ||
| * | ||
| * Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License") | ||
| * between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license. | ||
| * #L% | ||
| */ | ||
| package org.broadleafcommerce.core.web.graphql; | ||
|
|
||
| import org.broadleafcommerce.common.web.BroadleafRequestContext; | ||
| import org.broadleafcommerce.core.order.domain.Order; | ||
| import org.broadleafcommerce.core.order.service.OrderService; | ||
| import org.broadleafcommerce.core.web.order.CartState; | ||
| import org.broadleafcommerce.profile.core.domain.Customer; | ||
| import org.broadleafcommerce.profile.core.service.CustomerService; | ||
| import org.broadleafcommerce.profile.web.core.CustomerState; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.beans.factory.annotation.Qualifier; | ||
| import org.springframework.graphql.server.WebGraphQlInterceptor; | ||
| import org.springframework.graphql.server.WebGraphQlRequest; | ||
| import org.springframework.graphql.server.WebGraphQlResponse; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.web.context.request.RequestAttributes; | ||
| import org.springframework.web.context.request.RequestContextHolder; | ||
| import org.springframework.web.context.request.ServletRequestAttributes; | ||
| import org.springframework.web.context.request.ServletWebRequest; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import jakarta.servlet.http.HttpServletResponse; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| /** | ||
| * Populates the Broadleaf customer and cart context for GraphQL requests. This mirrors the identity | ||
| * resolution pattern used by {@code RestApiCustomerStateFilter} so that query/mutation resolvers | ||
| * can rely on {@link CustomerState} and {@link CartState}. | ||
| */ | ||
| @Component | ||
| public class GraphQLContextInterceptor implements WebGraphQlInterceptor { | ||
|
|
||
| @Autowired | ||
| @Qualifier("blCustomerService") | ||
| protected CustomerService customerService; | ||
|
|
||
| @Autowired | ||
| @Qualifier("blOrderService") | ||
| protected OrderService orderService; | ||
|
|
||
| @Override | ||
| public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) { | ||
| RequestAttributes attributes = RequestContextHolder.getRequestAttributes(); | ||
| if (attributes instanceof ServletRequestAttributes servletAttributes) { | ||
| HttpServletRequest httpRequest = servletAttributes.getRequest(); | ||
| HttpServletResponse httpResponse = servletAttributes.getResponse(); | ||
| populateContext(httpRequest, httpResponse); | ||
| } | ||
| return chain.next(request); | ||
| } | ||
|
|
||
| protected void populateContext(HttpServletRequest request, HttpServletResponse response) { | ||
| BroadleafRequestContext brc = BroadleafRequestContext.getBroadleafRequestContext(); | ||
| if (brc != null && brc.getWebRequest() == null) { | ||
| brc.setWebRequest(new ServletWebRequest(request, response)); | ||
| } | ||
|
|
||
| Customer customer = CustomerState.getCustomer(); | ||
| if (customer == null) { | ||
| customer = customerService.createCustomer(); | ||
| CustomerState.setCustomer(customer); | ||
| } | ||
|
|
||
| if (CartState.getCart() == null && customer != null && customer.getId() != null) { | ||
| Order cart = orderService.findCartForCustomer(customer); | ||
| if (cart != null) { | ||
| CartState.setCart(cart); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } |
73 changes: 73 additions & 0 deletions
73
...rk-web/src/main/java/org/broadleafcommerce/core/web/graphql/GraphQLExceptionResolver.java
This file contains hidden or 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,73 @@ | ||
| /*- | ||
| * #%L | ||
| * BroadleafCommerce Framework Web | ||
| * %% | ||
| * Copyright (C) 2009 - 2026 Broadleaf Commerce | ||
| * %% | ||
| * Licensed under the Broadleaf Fair Use License Agreement, Version 1.0 | ||
| * (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt) | ||
| * unless the restrictions on use therein are violated and require payment to Broadleaf in which case | ||
| * the Broadleaf End User License Agreement (EULA), Version 1.1 | ||
| * (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt) | ||
| * shall apply. | ||
| * | ||
| * Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License") | ||
| * between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license. | ||
| * #L% | ||
| */ | ||
| package org.broadleafcommerce.core.web.graphql; | ||
|
|
||
| import graphql.GraphQLError; | ||
| import graphql.GraphqlErrorBuilder; | ||
| import graphql.schema.DataFetchingEnvironment; | ||
| import org.broadleafcommerce.core.offer.service.exception.OfferMaxUseExceededException; | ||
| import org.broadleafcommerce.core.order.service.exception.AddToCartException; | ||
| import org.broadleafcommerce.core.order.service.exception.IllegalCartOperationException; | ||
| import org.broadleafcommerce.core.order.service.exception.RemoveFromCartException; | ||
| import org.broadleafcommerce.core.order.service.exception.UpdateCartException; | ||
| import org.broadleafcommerce.core.pricing.service.exception.PricingException; | ||
| import org.springframework.graphql.execution.DataFetcherExceptionResolverAdapter; | ||
| import org.springframework.graphql.execution.ErrorType; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| @Component | ||
| public class GraphQLExceptionResolver extends DataFetcherExceptionResolverAdapter { | ||
|
|
||
| @Override | ||
| protected GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironment env) { | ||
| String code = resolveCode(ex); | ||
| Map<String, Object> extensions = new HashMap<>(); | ||
| extensions.put("code", code); | ||
| return GraphqlErrorBuilder.newError(env) | ||
| .errorType(ErrorType.INTERNAL_ERROR) | ||
| .message(ex.getMessage() != null ? ex.getMessage() : code) | ||
| .extensions(extensions) | ||
| .build(); | ||
| } | ||
|
|
||
| protected String resolveCode(Throwable ex) { | ||
| if (ex instanceof OfferMaxUseExceededException) { | ||
| return "OFFER_MAX_USE_EXCEEDED"; | ||
| } | ||
| if (ex instanceof AddToCartException) { | ||
| return "ADD_TO_CART_ERROR"; | ||
| } | ||
| if (ex instanceof RemoveFromCartException) { | ||
| return "REMOVE_FROM_CART_ERROR"; | ||
| } | ||
| if (ex instanceof UpdateCartException) { | ||
| return "UPDATE_CART_ERROR"; | ||
| } | ||
| if (ex instanceof IllegalCartOperationException) { | ||
| return "ILLEGAL_CART_OPERATION"; | ||
| } | ||
| if (ex instanceof PricingException) { | ||
| return "PRICING_ERROR"; | ||
| } | ||
| return "INTERNAL_ERROR"; | ||
| } | ||
|
|
||
| } |
61 changes: 61 additions & 0 deletions
61
...ramework-web/src/main/java/org/broadleafcommerce/core/web/graphql/dto/AddToCartInput.java
This file contains hidden or 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,61 @@ | ||
| /*- | ||
| * #%L | ||
| * BroadleafCommerce Framework Web | ||
| * %% | ||
| * Copyright (C) 2009 - 2026 Broadleaf Commerce | ||
| * %% | ||
| * Licensed under the Broadleaf Fair Use License Agreement, Version 1.0 | ||
| * (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt) | ||
| * unless the restrictions on use therein are violated and require payment to Broadleaf in which case | ||
| * the Broadleaf End User License Agreement (EULA), Version 1.1 | ||
| * (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt) | ||
| * shall apply. | ||
| * | ||
| * Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License") | ||
| * between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license. | ||
| * #L% | ||
| */ | ||
| package org.broadleafcommerce.core.web.graphql.dto; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public class AddToCartInput { | ||
|
|
||
| private String productId; | ||
| private String skuId; | ||
| private int quantity; | ||
| private List<ItemAttributeInput> itemAttributes; | ||
|
|
||
| public String getProductId() { | ||
| return productId; | ||
| } | ||
|
|
||
| public void setProductId(String productId) { | ||
| this.productId = productId; | ||
| } | ||
|
|
||
| public String getSkuId() { | ||
| return skuId; | ||
| } | ||
|
|
||
| public void setSkuId(String skuId) { | ||
| this.skuId = skuId; | ||
| } | ||
|
|
||
| public int getQuantity() { | ||
| return quantity; | ||
| } | ||
|
|
||
| public void setQuantity(int quantity) { | ||
| this.quantity = quantity; | ||
| } | ||
|
|
||
| public List<ItemAttributeInput> getItemAttributes() { | ||
| return itemAttributes; | ||
| } | ||
|
|
||
| public void setItemAttributes(List<ItemAttributeInput> itemAttributes) { | ||
| this.itemAttributes = itemAttributes; | ||
| } | ||
|
|
||
| } | ||
41 changes: 41 additions & 0 deletions
41
...work-web/src/main/java/org/broadleafcommerce/core/web/graphql/dto/ItemAttributeInput.java
This file contains hidden or 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,41 @@ | ||
| /*- | ||
| * #%L | ||
| * BroadleafCommerce Framework Web | ||
| * %% | ||
| * Copyright (C) 2009 - 2026 Broadleaf Commerce | ||
| * %% | ||
| * Licensed under the Broadleaf Fair Use License Agreement, Version 1.0 | ||
| * (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt) | ||
| * unless the restrictions on use therein are violated and require payment to Broadleaf in which case | ||
| * the Broadleaf End User License Agreement (EULA), Version 1.1 | ||
| * (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt) | ||
| * shall apply. | ||
| * | ||
| * Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License") | ||
| * between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license. | ||
| * #L% | ||
| */ | ||
| package org.broadleafcommerce.core.web.graphql.dto; | ||
|
|
||
| public class ItemAttributeInput { | ||
|
|
||
| private String name; | ||
| private String value; | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public String getValue() { | ||
| return value; | ||
| } | ||
|
|
||
| public void setValue(String value) { | ||
| this.value = value; | ||
| } | ||
|
|
||
| } |
46 changes: 46 additions & 0 deletions
46
...amework-web/src/main/java/org/broadleafcommerce/core/web/graphql/dto/PromoCodeResult.java
This file contains hidden or 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,46 @@ | ||
| /*- | ||
| * #%L | ||
| * BroadleafCommerce Framework Web | ||
| * %% | ||
| * Copyright (C) 2009 - 2026 Broadleaf Commerce | ||
| * %% | ||
| * Licensed under the Broadleaf Fair Use License Agreement, Version 1.0 | ||
| * (the "Fair Use License" located at http://license.broadleafcommerce.org/fair_use_license-1.0.txt) | ||
| * unless the restrictions on use therein are violated and require payment to Broadleaf in which case | ||
| * the Broadleaf End User License Agreement (EULA), Version 1.1 | ||
| * (the "Commercial License" located at http://license.broadleafcommerce.org/commercial_license-1.1.txt) | ||
| * shall apply. | ||
| * | ||
| * Alternatively, the Commercial License may be replaced with a mutually agreed upon license (the "Custom License") | ||
| * between you and Broadleaf Commerce. You may not use this file except in compliance with the applicable license. | ||
| * #L% | ||
| */ | ||
| package org.broadleafcommerce.core.web.graphql.dto; | ||
|
|
||
| import org.broadleafcommerce.core.order.domain.Order; | ||
|
|
||
| public class PromoCodeResult { | ||
|
|
||
| private final Order order; | ||
| private final boolean promoAdded; | ||
| private final String errorMessage; | ||
|
|
||
| public PromoCodeResult(Order order, boolean promoAdded, String errorMessage) { | ||
| this.order = order; | ||
| this.promoAdded = promoAdded; | ||
| this.errorMessage = errorMessage; | ||
| } | ||
|
|
||
| public Order getOrder() { | ||
| return order; | ||
| } | ||
|
|
||
| public boolean isPromoAdded() { | ||
| return promoAdded; | ||
| } | ||
|
|
||
| public String getErrorMessage() { | ||
| return errorMessage; | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.