0

Env: Sitecore 10.3 SXA Headless and using NextJS(react)

I am trying to retrieve the version of a pdf file from media lib using below GraphQL query and added to the Sitecore rendering (Json Rendering). I keep getting null value, but it's working on the playground.

enter image description here

Browser console:

enter image description here

Playground: graphql playground

FYI - I tried a different GraphQL query on this rendering with $contextitem and $datasource and it’s working. It’s not working when I directly pass the GUID/Path.

3
  • Is your endpoint url the same? In the playground do you use CM url? You can test other urls in the playground to test it out. Maybe this article can help you: linkedin.com/pulse/…
    – Danny Paul
    Commented Apr 27 at 4:41
  • @Danny Paul I am running locally with connected mode. All other graphQL queries are working (with datasource or contextitem), but if I pass the hardcoded value it’s not working.
    – Sun
    Commented Apr 27 at 11:29
  • How do you run the GraphQL query? To you parse it in a string. Do you escape the "{" and "}" so it doesn't try to use format?
    – Danny Paul
    Commented Apr 27 at 19:29

2 Answers 2

1

The issue you are facing probably is that the item itself is not present in the instance. The query returns the same result in the playground and browser console. Please see the attached screenshot below:

Playground:

enter image description here

Browser:

enter image description here

For better output using the integrated you can use query variables like: $contextItem, $datasource, $language

Probably you can use the below query:

  query($datasource: String!, $language: String!) {
  item(path: $datasource, language: $language) {
    name
    version
  }
}
5
  • @Biswas I am trying to get a specific pdf file version from the media lib, it’s not part of the datasource or the contextitem. That’s why I passed the GUID. But if I pass a different GraphQL query (with contextitem or datasource) it’s working. It’s just not returning data when I hardcode the value. Tried both GUID and item path.
    – Sun
    Commented Apr 27 at 11:27
  • So does your rendering has a data source attached?? And along with that you are trying to fetch the GraphQL query also?? Can you confirm your layout resolver field is empty in the rendering?? Commented Apr 27 at 17:57
  • Also if you want to pass a hard-coded item id, it will be better if you bind your query directly in the front-end application rather than using integrated GraphQL. Commented Apr 27 at 18:00
  • @Biswas - There is no data source attached, I am building a custom form and hardcoding it on react. Any suggestions on how to bind the query in the front end?
    – Sun
    Commented Apr 29 at 12:59
  • @Sun - This is not exactly the solution you are looking, but if you refer this URL, you might get a fair idea about how to use the GraphQl in FED application. debtanubiswasblog.wordpress.com/2024/04/06/… Commented Apr 29 at 14:39
1

The problem you have is the item that you are trying to query. You are trying to get PDF item, so its in the media library.

By default, the preview GraphQL API running on the CM or delivery API running on a CD sets its content root at /sitecore/content, you cannot query any content outside of that.

Update

Confirmed this with the product team, the limitation of the preview api only querying items under /sitecore/content is a known limitation and will be fixed in the future (current date: April 29th 2024)

Solution

To get around the issue for now, you will need to create a data template that you can link to the PDF item and also has fields for the information you need. Either that or because you are on 10.3, you could write a custom Rendering Content Resolver that gets the version of the media item in code and returns it out.

With Experience Edge

If you are using Experience Edge, then the media items are published and you can query them from Edge, so you would need to change your query from Integrated GraphQL to component based GraphQL.

6
  • You are correct, I am able to query non-media items, but not the media items from the default endpoint "sitecore/api/graph/edge". But I am getting the same results with Exp.Edge too. I have this entry "GRAPH_QL_ENDPOINT=https://edge.sitecorecloud.io/api/graphql/v1" in my .env file
    – Sun
    Commented Apr 29 at 18:13
  • Check your setting for : WorkerStorePublishableRoots - I have /sitecore/Content|/sitecore/media library on XM Cloud. That should specifiy the content roots for publishing to Edge
    – Richard Seal
    Commented Apr 29 at 18:15
  • Unfortunately, we are not on the XM Cloud. Our on-prem(CM) is currently being published to the Experience Edge. We have the above entry on the .env file
    – Sun
    Commented Apr 29 at 18:31
  • Yes, but check your showConfig.aspx - what is your WorkerStorePublishableRoots set too? That tells the Edge Connector what root paths can be published
    – Richard Seal
    Commented Apr 29 at 19:29
  • 1
    thank you for the update and I will try the alternative solution. For now I will accept this answer.
    – Sun
    Commented Apr 30 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.