forked from BroadleafCommerce/BroadleafCommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
GraphQL Foundation — Dependencies, Schema, and Configuration #15
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
5
commits into
develop-7.0.x
Choose a base branch
from
devin/graphql-ticket1-foundation
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
5 commits
Select commit
Hold shift + click to select a range
8ed857a
GraphQL Foundation — dependencies, schema, and configuration
devin-ai-integration[bot] fa74884
Address review: fix customer id parsing and relocate GraphQL properties
devin-ai-integration[bot] 646d89a
Address review: null-safe exception message in GraphQLExceptionResolver
devin-ai-integration[bot] fae686b
Address review: use ExtendedScalars.GraphQLBigDecimal for monetary pr…
devin-ai-integration[bot] efb3400
Address review: guard against NumberFormatException on oversized cust…
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
38 changes: 38 additions & 0 deletions
38
...eaf-framework-web/src/main/java/org/broadleafcommerce/core/web/graphql/GraphQLConfig.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,38 @@ | ||
| /*- | ||
| * #%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.scalars.ExtendedScalars; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
| import org.springframework.graphql.execution.RuntimeWiringConfigurer; | ||
|
|
||
| /** | ||
| * Registers Broadleaf-specific GraphQL runtime wiring, including the | ||
| * {@code BigDecimal} scalar used for monetary amounts. The scalar preserves | ||
| * the full precision of {@link java.math.BigDecimal}; serializing via | ||
| * {@code GraphQLFloat} would silently degrade values to IEEE 754 doubles. | ||
| */ | ||
| @Configuration | ||
| public class GraphQLConfig { | ||
|
|
||
| @Bean | ||
| public RuntimeWiringConfigurer runtimeWiringConfigurer() { | ||
| return wiringBuilder -> wiringBuilder.scalar(ExtendedScalars.GraphQLBigDecimal); | ||
| } | ||
| } |
185 changes: 185 additions & 0 deletions
185
...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,185 @@ | ||
| /*- | ||
| * #%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.apache.commons.lang3.math.NumberUtils; | ||
| import org.apache.commons.logging.Log; | ||
| import org.apache.commons.logging.LogFactory; | ||
| import org.broadleafcommerce.common.util.StringUtil; | ||
| 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 java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| import jakarta.servlet.http.HttpServletRequest; | ||
| import reactor.core.publisher.Mono; | ||
|
|
||
| /** | ||
| * A {@link WebGraphQlInterceptor} that establishes the Broadleaf customer and cart | ||
| * context for GraphQL requests. The customer identity is resolved following the same | ||
| * precedence used by {@code RestApiCustomerStateFilter}: request attribute first, then | ||
| * request parameter, then request header. If no customer can be resolved, an anonymous | ||
| * customer is created. Once the customer is established, the active cart is populated | ||
| * into {@link CartState} for downstream resolvers. | ||
| */ | ||
| @Component | ||
| public class GraphQLContextInterceptor implements WebGraphQlInterceptor { | ||
|
|
||
| public static final String CUSTOMER_ID_ATTRIBUTE = "customerId"; | ||
| public static final String BLC_RULE_MAP_PARAM = "blRuleMap"; | ||
|
|
||
| protected static final Log LOG = LogFactory.getLog(GraphQLContextInterceptor.class); | ||
|
|
||
| protected final CustomerService customerService; | ||
| protected final OrderService orderService; | ||
|
|
||
| @Autowired | ||
| public GraphQLContextInterceptor( | ||
| @Qualifier("blCustomerService") CustomerService customerService, | ||
| @Qualifier("blOrderService") OrderService orderService | ||
| ) { | ||
| this.customerService = customerService; | ||
| this.orderService = orderService; | ||
| } | ||
|
|
||
| @Override | ||
| public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) { | ||
| HttpServletRequest httpRequest = extractHttpRequest(request); | ||
| if (httpRequest != null) { | ||
| populateCustomerAndCart(httpRequest); | ||
| } | ||
| return chain.next(request); | ||
| } | ||
|
|
||
| protected HttpServletRequest extractHttpRequest(WebGraphQlRequest request) { | ||
| RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); | ||
| if (requestAttributes instanceof ServletRequestAttributes servletRequestAttributes) { | ||
| return servletRequestAttributes.getRequest(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| protected void populateCustomerAndCart(HttpServletRequest request) { | ||
| Customer customer = resolveCustomer(request); | ||
| if (customer == null) { | ||
| customer = customerService.createCustomer(); | ||
| CustomerState.setCustomer(customer); | ||
| setupCustomerForRuleProcessing(customer, request); | ||
| } | ||
| populateCart(customer); | ||
| } | ||
|
|
||
| protected Customer resolveCustomer(HttpServletRequest request) { | ||
| String customerId = readCustomerId(request); | ||
|
|
||
| if (customerId != null && customerId.trim().length() > 0) { | ||
| Long parsedId = parseCustomerId(customerId); | ||
| if (parsedId != null) { | ||
| Customer customer = customerService.readCustomerById(parsedId); | ||
| if (customer != null) { | ||
| ensureWebRequest(request); | ||
| CustomerState.setCustomer(customer); | ||
| setupCustomerForRuleProcessing(customer, request); | ||
| return customer; | ||
| } | ||
| } else { | ||
| LOG.warn(String.format("The customer id passed in '%s' was not a valid long", StringUtil.sanitize(customerId))); | ||
| } | ||
| } else if (LOG.isDebugEnabled()) { | ||
| LOG.debug("No customer ID was found for the GraphQL request. In order to look up a customer for the request" | ||
| + " send a request parameter or request header for the '" + CUSTOMER_ID_ATTRIBUTE + "' attribute"); | ||
| } | ||
|
|
||
| Customer existingCustomer = CustomerState.getCustomer(); | ||
| if (existingCustomer != null) { | ||
| return existingCustomer; | ||
| } | ||
|
|
||
| ensureWebRequest(request); | ||
| return null; | ||
| } | ||
|
|
||
| protected Long parseCustomerId(String customerId) { | ||
| if (!NumberUtils.isDigits(customerId)) { | ||
| return null; | ||
| } | ||
| try { | ||
| return Long.valueOf(customerId); | ||
| } catch (NumberFormatException ex) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| protected String readCustomerId(HttpServletRequest request) { | ||
| String customerId = null; | ||
| if (request.getAttribute(CUSTOMER_ID_ATTRIBUTE) != null) { | ||
| customerId = String.valueOf(request.getAttribute(CUSTOMER_ID_ATTRIBUTE)); | ||
| } | ||
| if (customerId == null) { | ||
| customerId = request.getParameter(CUSTOMER_ID_ATTRIBUTE); | ||
| } | ||
| if (customerId == null) { | ||
| customerId = request.getHeader(CUSTOMER_ID_ATTRIBUTE); | ||
| } | ||
| return customerId; | ||
| } | ||
|
|
||
| protected void ensureWebRequest(HttpServletRequest request) { | ||
| BroadleafRequestContext ctx = BroadleafRequestContext.getBroadleafRequestContext(); | ||
| if (ctx != null && ctx.getWebRequest() == null) { | ||
| ctx.setWebRequest(new ServletWebRequest(request)); | ||
| } | ||
| } | ||
|
|
||
| protected void populateCart(Customer customer) { | ||
| if (customer == null) { | ||
| return; | ||
| } | ||
| Order cart = orderService.findCartForCustomer(customer); | ||
| if (cart != null) { | ||
| CartState.setCart(cart); | ||
| } | ||
| } | ||
|
|
||
| protected void setupCustomerForRuleProcessing(Customer customer, HttpServletRequest request) { | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, Object> ruleMap = (Map<String, Object>) request.getAttribute(BLC_RULE_MAP_PARAM); | ||
| if (ruleMap == null) { | ||
| ruleMap = new HashMap<>(); | ||
| } | ||
| ruleMap.put("customer", customer); | ||
| request.setAttribute(BLC_RULE_MAP_PARAM, ruleMap); | ||
| } | ||
|
|
||
| } |
92 changes: 92 additions & 0 deletions
92
...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,92 @@ | ||
| /*- | ||
| * #%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.ErrorClassification; | ||
| import graphql.ErrorType; | ||
| import graphql.GraphQLError; | ||
| import graphql.GraphqlErrorBuilder; | ||
| import graphql.schema.DataFetchingEnvironment; | ||
| import org.broadleafcommerce.core.checkout.service.exception.CheckoutException; | ||
| 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.stereotype.Component; | ||
|
|
||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Maps Broadleaf domain exceptions to structured GraphQL errors with stable | ||
| * {@code code} extensions so clients can branch on a known error taxonomy. | ||
| */ | ||
| @Component | ||
| public class GraphQLExceptionResolver extends DataFetcherExceptionResolverAdapter { | ||
|
|
||
| public static final String CODE_ADD_TO_CART = "ADD_TO_CART_ERROR"; | ||
| public static final String CODE_PRICING = "PRICING_ERROR"; | ||
| public static final String CODE_OFFER_MAX_USE_EXCEEDED = "OFFER_MAX_USE_EXCEEDED"; | ||
| public static final String CODE_ILLEGAL_CART_OPERATION = "ILLEGAL_CART_OPERATION"; | ||
| public static final String CODE_CHECKOUT = "CHECKOUT_ERROR"; | ||
| public static final String CODE_REMOVE_FROM_CART = "REMOVE_FROM_CART_ERROR"; | ||
| public static final String CODE_UPDATE_CART = "UPDATE_CART_ERROR"; | ||
|
|
||
| @Override | ||
| protected GraphQLError resolveToSingleError(Throwable ex, DataFetchingEnvironment env) { | ||
| if (ex instanceof AddToCartException) { | ||
| return buildError(ex, env, ErrorType.ValidationError, CODE_ADD_TO_CART); | ||
| } | ||
| if (ex instanceof RemoveFromCartException) { | ||
| return buildError(ex, env, ErrorType.ValidationError, CODE_REMOVE_FROM_CART); | ||
| } | ||
| if (ex instanceof UpdateCartException) { | ||
| return buildError(ex, env, ErrorType.ValidationError, CODE_UPDATE_CART); | ||
| } | ||
| if (ex instanceof IllegalCartOperationException) { | ||
| return buildError(ex, env, ErrorType.ValidationError, CODE_ILLEGAL_CART_OPERATION); | ||
| } | ||
| if (ex instanceof OfferMaxUseExceededException) { | ||
| return buildError(ex, env, ErrorType.ValidationError, CODE_OFFER_MAX_USE_EXCEEDED); | ||
| } | ||
| if (ex instanceof CheckoutException) { | ||
| return buildError(ex, env, ErrorType.ValidationError, CODE_CHECKOUT); | ||
| } | ||
| if (ex instanceof PricingException) { | ||
| return buildError(ex, env, ErrorType.DataFetchingException, CODE_PRICING); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| protected GraphQLError buildError( | ||
| Throwable ex, | ||
| DataFetchingEnvironment env, | ||
| ErrorClassification classification, | ||
| String code | ||
| ) { | ||
| String message = ex.getMessage() != null ? ex.getMessage() : ex.getClass().getSimpleName(); | ||
| return GraphqlErrorBuilder.newError(env) | ||
| .message(message) | ||
| .errorType(classification) | ||
| .extensions(Map.of("code", code)) | ||
| .build(); | ||
| } | ||
|
|
||
| } | ||
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
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.