UPSTREAM FIX

magento2-39971: GraphQL customer orders fail with null Discount.applied_to on order items

Community fix magento2-39971 merged into magento/magento2 on 2025-08-11, released in 2.4.9; applies cleanly to 17 releases from 2.4.7 to 2.4.8-p5.

GraphQL customer orders fail with null Discount.applied_to on order items edited

Pull request title
Error getting order item discounts applied_to for customer order via GraphQl customer request
Pull request
magento/magento2#39971
Issues
#39963 human
Author
@rogerdz
Merged
2025-08-11
Fixed in
2.4.9
Reported on
2.4.8
Categories
GraphQL
Components
magento/module-sales-graph-ql

Labels

Area
Promotion Rules
Component
GraphQL
Priority
P2
Severity
—
Reported on (labels)
2.4.8

Issue

Title and steps come from the upstream issue and pull request.

Description

There is an error: Internal server error Cannot return null for non-nullable field "Discount.applied_to"

Steps to reproduce

1. Create a simple product, customer, sales rule with a coupon, and place an order using this customer and applying coupon.
2. Get customer token via GraphQL query generateCustomerToken
3. Issue GraphQL request
query {
  customer {
    firstname
    lastname
    orders {
      items {
        order_number
        status
        items {
          product_sku
          discounts {
            applied_to
            amount {
              value
            }
          }
        }
      }
      page_info {
        current_page
        page_size
        total_pages
      }
      total_count
    } 
  }
}

Expected result

There is a correct response from GraphQl with orders and order items discount data

Actual result

There is an error: Internal server error Cannot return null for non-nullable field "Discount.applied_to"

exception.log:
[2025-06-05T10:18:09.817671+00:00] main.ERROR: Cannot return null for non-nullable field "Discount.applied_to".

GraphQL (25:13)
24:           discounts {
25:             applied_to
                ^
26:             amount {
 {"exception":"[object] (GraphQL\\Error\\Error(code: 0): Cannot return null for non-nullable field \"Discount.applied_to\". at /home/zak/sites/magento/test1/public/vendor/webonyx/graphql-php/src/Error/Error.php:171)
[previous exception] [object] (GraphQL\\Error\\InvariantViolation(code: 0): Cannot return null for non-nullable field \"Discount.applied_to\". at /home/zak/sites/magento/test1/public/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php:899)"} []

Taken from the upstream issue.

Error signatures

Code match per tag

Each tag was checked with git apply --check against that tag's files. A clean match means the change applies; it is not a test result. Tags that already contain the fix are marked.

LineCode match per tagTests
2.4.6
2.4.6 conflict 2.4.6-p1 conflict 2.4.6-p2 conflict 2.4.6-p3 conflict 2.4.6-p4 conflict 2.4.6-p5 conflict 2.4.6-p6 conflict 2.4.6-p7 conflict 2.4.6-p8 conflict 2.4.6-p9 conflict 2.4.6-p10 conflict 2.4.6-p11 conflict 2.4.6-p12 conflict 2.4.6-p13 conflict 2.4.6-p14 conflict 2.4.6-p15 conflict
2.4.6: no test data 2.4.6-p1: no test data 2.4.6-p2: no test data 2.4.6-p3: no test data 2.4.6-p4: no test data 2.4.6-p5: no test data 2.4.6-p6: no test data 2.4.6-p7: no test data 2.4.6-p8: no test data 2.4.6-p9: no test data 2.4.6-p10: no test data 2.4.6-p11: no test data 2.4.6-p12: no test data 2.4.6-p13: no test data 2.4.6-p14: no test data 2.4.6-p15: no test data
2.4.7
2.4.7 clean 2.4.7-p1 clean 2.4.7-p2 clean 2.4.7-p3 clean 2.4.7-p4 clean 2.4.7-p5 clean 2.4.7-p6 clean 2.4.7-p7 clean 2.4.7-p8 clean 2.4.7-p9 clean 2.4.7-p10 clean
2.4.7: no test data 2.4.7-p1: no test data 2.4.7-p2: no test data 2.4.7-p3: no test data 2.4.7-p4: no test data 2.4.7-p5: no test data 2.4.7-p6: no test data 2.4.7-p7: no test data 2.4.7-p8: no test data 2.4.7-p9: no test data 2.4.7-p10: no test data
2.4.8
2.4.8 clean 2.4.8-p1 clean 2.4.8-p2 clean 2.4.8-p3 clean 2.4.8-p4 clean 2.4.8-p5 clean
2.4.8: no test data 2.4.8-p1: no test data 2.4.8-p2: no test data 2.4.8-p3: no test data 2.4.8-p4: no test data 2.4.8-p5: no test data
2.4.9
2.4.9 conflictcontains the fix
2.4.9: no test data

Triage

Model @cf/cloudflare/clef. Probability this is a bug fix: 98.2%. Probability it is security relevant: 1.5%.

Show the model's answers and probabilities
QuestionAnswerProbabilitiesConfidence
Change kindbugfixbugfix 97.0%, refactor 1.1%, tests_only 0.5%, feature 0.5%, dependency 0.4%, docs_only 0.3%93.0%
Areagraphql_apigraphql_api 94.3%, customer 1.9%, checkout 1.8%87.4%
Reported version2.4.82.4.8 56.7%, 2.4.8-p2 3.7%, 2.4.8-p1 2.6%31.9%
Scope0.81 of 21 52.9%, 0 32.9%, 2 14.2%11.2%
Risk0.39 of 20 71.0%, 1 19.3%, 2 9.7%32.6%
Worth backporting1.71 of 22 76.2%, 1 19.2%, 0 4.7%42.8%

Download

For cweagans/composer-patches, choose a version below and download the bundle. Copy its magento2-39971/ folder into patches/composer/, merge composer.patches.json into composer.json, then run composer install. Test files are always removed; paths are relative to each package root, using the default -p1 level.

Packages (1): magento/module-sales-graph-ql
Bundle README (what the ZIP ships)
# magento2-39971

Community fix merged upstream into magento/magento2, adapted by magento.watch.
This is not a patch published by Adobe.

Pull request: https://github.com/magento/magento2/pull/39971
Issue: https://github.com/magento/magento2/issues/39963
Author: @rogerdz
Source commit: c2f257ee5569861b2d409471414e5b540e77338d
Modifications: test files and documentation removed, paths rewritten relative to each Composer package.
Licence: OSL-3.0 / AFL-3.0, as the original Magento Open Source code.
Maintainer: Łukasz Bajsarowicz (@lbajsarowicz)

Licence: Magento Open Source code under OSL-3.0 and AFL-3.0. The bundle carries the original author, source commit and the list of modifications.

Sources

Łukasz Bajsarowicz
Built by

Łukasz Bajsarowicz, e-commerce architect

Magento and Adobe Commerce architecture, upgrades, performance and audits for merchants and agencies since 2015; magento.watch is the tooling I use on those projects.

Open source, maintained on weekends.