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

Created on 18 February 2025, about 2 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

Production build 0.71.5 2024