GraphQL fragment returning null | Community
Skip to main content
January 16, 2025
Solved

GraphQL fragment returning null

  • January 16, 2025
  • 4 replies
  • 991 views

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. 

 

 

Best answer by Shiv_Prakash_Patel

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,

 

4 replies

sarav_prakash
Community Advisor
Community Advisor
January 17, 2025

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

 

 

jlepore30Author
January 17, 2025

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

sarav_prakash
Community Advisor
Community Advisor
January 17, 2025

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?

Shiv_Prakash_Patel
Community Advisor
Shiv_Prakash_PatelCommunity AdvisorAccepted solution
Community Advisor
January 17, 2025

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
jlepore30Author
January 21, 2025

Thank you @shiv_prakash_patel 

AmitVishwakarma
Community Advisor
Community Advisor
January 19, 2025

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.