Expand my Community achievements bar.

SOLVED

GraphQL fragment returning null

Avatar

Level 1

Hello, 

 

Whenever I try to include graphQL fragments in my query the values return null. First example shows results without graphQL fragment, second shows results with it. Note that the content fragments are the same for both instances, so the null should have something populate. 

 

 

Topics

Topics help categorize Community content and increase your ability to discover relevant content.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @jlepore30 

The AEM GraphQL API, while based on standard GraphQL, has areas that are still under development. Features such as mutations, subscriptions, and comprehensive support for fragments may not function as expected, leading to null values in query responses.

Solutions:

Avoid Using Fragments: To ensure consistent data retrieval, structure your GraphQL queries without using fragments. By explicitly specifying the required fields in each query, you can bypass the limitations associated with fragment usage in AEM's current GraphQL implementation.

Regards,

 

Shiv Prakash

View solution in original post

6 Replies

Avatar

Level 8

The query syntax is like this 

 

items {
  pageCtaReferences {
    ... on ButtonWithLogicModel {
         buttonId
         buttonLabel
    }
  }
}

Here is a similar example of my query that populates the reference fragment values

 

sarav_prakash_0-1737083046447.png

 

Avatar

Level 1

Thanks for the response @sarav_prakash . That is what I typically use on bigger pages as well. I am trying to use these graphql fragments so that I don't have to repeat '...on buttonWithLogicModel' three different times on the same query. 

Like this - https://hygraph.com/learn/graphql/fragments

Avatar

Level 8

understood. I too tried with fragment instead of spread operator and my query works again. Your fragment paths are all right, dont find anything wrong. 

Just to ruleout, my cfm modal path doesnt have too many hyphens like yours `/button---with-logic`. Can you try simple modal like `/buttonwithlogic` just to ruleout hyphens are not causing noise?

Avatar

Correct answer by
Community Advisor

Hi @jlepore30 

The AEM GraphQL API, while based on standard GraphQL, has areas that are still under development. Features such as mutations, subscriptions, and comprehensive support for fragments may not function as expected, leading to null values in query responses.

Solutions:

Avoid Using Fragments: To ensure consistent data retrieval, structure your GraphQL queries without using fragments. By explicitly specifying the required fields in each query, you can bypass the limitations associated with fragment usage in AEM's current GraphQL implementation.

Regards,

 

Shiv Prakash

Avatar

Level 1

Thank you @Shiv_Prakash_Patel 

Avatar

Level 7

In short, if your GraphQL fragment is returning null, try the following:

  1. Check Fragment Definition: Ensure the fragment is defined correctly with the necessary fields.
  2. Verify Query Syntax: Make sure you're including the fragment properly in the query using ...FragmentName.
  3. Check Data Availability: Ensure the backend data contains the fields your fragment is querying.
  4. Test Fragment Independently: Run a simplified query using just the fragment to isolate the issue.
  5. Clear Caches: Try clearing any caches in AEM or the GraphQL server.

These steps should help you pinpoint the issue with the fragment returning null.