Cannot return null for non-nullable field for Link Field in Paragraph

Created on 18 February 2025, 5 months ago

Problem/Motivation

Getting below error while trying to fetch data from Decoupled application for a link field with limited cardinality like 4 and only add 3 links to the field. This field should be included in a paragraph and that paragraph should be linked to a content.
GraphQL\Error\InvariantViolation: Cannot return null for non-nullable field "ParagraphDirectLinksBlock.links". in GraphQL\Executor\ReferenceExecutor->completeValue() (line 748 of /var/www/html/vendor/webonyx/graphql-php/src/Executor/ReferenceExecutor.php).

Steps to reproduce

Create a paragraph.
Add Link field with cardinality 4.
Add this paragraph to a node. For example Article.
Now add an Article content.
Add paragraph and in paragraph only add 2 or 3 links.
Now try to fetch the data from FE application.

Proposed resolution

Update below code in given file docroot/modules/contrib/graphql_compose/src/Plugin/GraphQL/DataProducer/FieldProducerPlugin.php

  public function resolveFieldItems(FieldItemListInterface $field, FieldContext $context): array {
    $plugin = $this->getContextValue('plugin');

    if ($plugin instanceof FieldProducerItemsInterface) {
      return $plugin->resolveFieldItems($field, $context);
    }

    $results = [];
    foreach ($field as $item) {
      if (!empty($result = $this->resolveFieldItem($item, $context))) {
        $results[] = $result;
      }
    }

    return $results;
  }
  public function resolveFieldItems(FieldItemListInterface $field, FieldContext $context): array {
    $plugin = $this->getContextValue('plugin');

    if ($plugin instanceof FieldProducerItemsInterface) {
      return $plugin->resolveFieldItems($field, $context);
    }

    $results = [];
    foreach ($field as $item) {
       $results[] = $this->resolveFieldItem($item, $context);
    }

    return $results;
  }
🐛 Bug report
Status

Active

Version

2.3

Component

Code

Created by

🇮🇳India vikas shishodia

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @vikas shishodia
  • Pipeline finished with Failed
    5 months ago
    Total: 258s
    #427358
  • 🇦🇺Australia almunnings Melbourne, 🇦🇺

    I suppose instead of

    if (!empty($result = $this->resolveFieldItem($item, $context))) {

    Could you use is_null ?

  • Pipeline finished with Failed
    3 months ago
    Total: 430s
    #453517
  • 🇦🇺Australia almunnings Melbourne, 🇦🇺

    Hmm, nah not passing tests.
    I'll try take a look at this next

  • Status changed to Postponed: needs info about 2 months ago
  • 🇦🇺Australia almunnings Melbourne, 🇦🇺

    I'm unable to reproduce this error.

    query MyQuery {
      node(id: "777f15be-c72f-4578-a9cb-c297559c9e89") {
        ... on NodePage {
          paras {
            __typename
            ... on ParagraphLinks {
              links {
                title
                url
              }
            }
          }
        }
      }
    }
    
    {
      "data": {
        "node": {
          "paras": [
            {
              "__typename": "ParagraphText"
            },
            {
              "__typename": "ParagraphLinks",
              "links": [
                {
                  "title": "Para text",
                  "url": "https://www.para.com"
                },
                {
                  "title": "sdadsasd",
                  "url": "https://www.pasra.com"
                }
              ]
            }
          ]
        }
      }
    }
    

    Are you able to re-check and revise on how to reproduce?

  • 🇺🇸United States apmsooner

    Al, I'm not on that project anymore and I could also never reproduce the issue on any other projects so I'm of the opinion of just closing the ticket unless you want to wait on anyone else to respond.

  • 🇦🇺Australia almunnings Melbourne, 🇦🇺

    Nah thats good enough for now, happy for this to re-open if someone finds a way.

Production build 0.71.5 2024