如何让AEM GraphQL返回jcr:lastModified日期作为内容片段元数据?
jcr:lastModified in AEM GraphQL Responses for Content Fragments Great question—this is a common gotcha with AEM's Content Fragment GraphQL integration. Let me break down why this is happening and walk you through two reliable solutions to get jcr:lastModified (or any other JCR property from jcr:content) into your GraphQL response.
Why jcr:lastModified Isn't Returned by Default
AEM's Content Fragment GraphQL provider is configured to:
- Expose all metadata stored under
jcr:content/metadata(the standard metadata node for assets/content fragments) - Automatically include a few special fields from
jcr:content:jcr:titleandjcr:description(these are hardcoded exceptions) - Ignore other properties directly under
jcr:content(likejcr:lastModified,jcr:created, etc.) unless explicitly configured to include them.
Solution 1: Add jcr:lastModified as a Custom Field in Your Content Fragment Model
This is the most straightforward no-code approach if you have access to the Content Fragment Model editor:
Navigate to your Content Fragment Model
- Go to
Tools > Assets > Content Fragment Modelsin AEM. - Find the model used by your
rahjang-benifitcontent fragment and open it for editing.
- Go to
Add the custom property to the model
- Switch to the GraphQL tab in the editor.
- Scroll to the Additional Properties section, then click Add Property.
- Fill in the details:
- Name:
jcrLastModified(or any name you prefer—GraphQL uses camelCase) - Type:
Calendar(sincejcr:lastModifiedis a date-time value) - Path:
./jcr:content/jcr:lastModified(this points directly to the JCR property)
- Name:
- Save the model.
Update your GraphQL query
Now you can query the field directly under theitemobject:{ benefitProgramByPath(_path: "/content/dam/rahjang-cf/rahjang-benifit") { item { _metadata { stringMetadata { name, value, __typename } # Keep your existing metadata fields } jcrLastModified # New field mapped to jcr:lastModified } } }
Solution 2: Configure the GraphQL Provider to Include Additional Metadata
If you want jcr:lastModified to appear within the _metadata structure (like the other metadata fields), you can adjust the OSGi configuration for the Content Fragment GraphQL provider:
Access the OSGi Configuration Console
- Go to
http://<your-aem-instance>:<port>/system/console/configMgr - Search for
org.apache.sling.graphql.content.fragments.impl.ContentFragmentGraphQLProvider
- Go to
Add
jcr:lastModifiedto Additional Properties- Find the Additional Properties field (a multi-value text input)
- Add an entry:
jcr:lastModified=calendarMetadata - This tells the provider to map the
jcr:lastModifiedproperty to thecalendarMetadataarray in your_metadataresponse.
Test your existing query
After saving the configuration, your original query will now includejcr:lastModifiedin thecalendarMetadataarray:{ "data": { "benefitProgramByPath": { "item": { "_metadata": { "stringMetadata": [...], "calendarMetadata": [ { "name": "jcr:lastModified", "value": "2024-05-20T14:30:00.000Z", "__typename": "CalendarMetadata" } ] } } } } }
Notes
- For
jcr:created, you can use the exact same steps—just replacejcr:lastModifiedwithjcr:createdin the path/configuration. - If you're using AEM as a Cloud Service, you'll need to deploy the OSGi configuration via your codebase (as a
.configfile inui.config/src/main/content/jcr_root/apps/<your-project>/config) instead of using the console directly.
内容的提问来源于stack exchange,提问作者rahul

