示例应用程序是探索Adobe Experience Manager(AEM)无头功能的绝佳方式。 此服务器到服务器应用程序演示了如何使用AEM GraphQL API来使用持久查询查询查询内容,并在终端上打印该内容。
查看 GitHub上的源代码
应在本地安装以下工具:
Node.js应用程序可与以下AEM部署选项配合使用。 所有部署都需要 WKND Site v2.0.0+ 安装。
此Node.js应用程序可以根据命令行参数连接到AEM作者或AEM发布。
克隆 adobe/aem-guides-wknd-graphql
存储库:
$ git clone git@github.com:adobe/aem-guides-wknd-graphql.git
打开终端并运行命令:
$ cd aem-guides-wknd-graphql/server-to-server-app
$ npm install
可使用以下命令运行应用程序:
$ node index.js <AEM_HOST> <OPTIONAL_SERVICE_CONFIG_FILE_PATH>
例如,要在未授权的情况下针对AEM发布运行应用程序,请执行以下操作:
$ node index.js https://publish-p123-e789.adobeaemcloud.com
要通过授权针对AEM作者运行应用程序,请执行以下操作:
$ node index.js https://author-p123-e456.adobeaemcloud.com ./service-config.json
来自WKND引用站点的历险JSON列表应打印在终端中。
以下是如何构建服务器到服务器Node.js应用程序、如何连接到AEM Headless以使用GraphQL持久查询检索内容,以及如何显示该数据的摘要。 完整代码可在 GitHub.
服务器到服务器AEM无头应用程序的常见用例是将内容片段数据从AEM同步到其他系统,但此应用程序特意很简单,会打印保留查询的JSON结果。
遵循AEM无头最佳实践,应用程序使用AEM GraphQL持久查询来查询冒险数据。 应用程序使用两个持久查询:
wknd/adventures-all
持久查询,该查询会返回具有一组简略属性的AEM中的所有冒险。 此持久查询驱动初始视图的探险列表。# Retrieves a list of all adventures
{
adventureList {
items {
_path
slug
title
price
tripLength
primaryImage {
... on ImageRef {
_path
mimeType
width
height
}
}
}
}
}
const { AEMHeadless, getToken } = require('@adobe/aem-headless-client-nodejs');
async function run() {
// Parse the AEM host, and optional service credentials from the command line arguments
const args = process.argv.slice(2);
const aemHost = args.length > 0 ? args[0] : null; // Example: https://author-p123-e456.adobeaemcloud.com
const serviceCredentialsFile = args.length > 1 ? args[1] : null; // Example: ./service-config.json
// If service credentials are provided via command line argument,
// use `getToken(..)` to exchange them with Adobe IMS for an AEM access token
let accessToken;
if (serviceCredentialsFile) {
accessToken = (await getToken(serviceCredentialsFile)).accessToken;
}
// Instantiate withe AEM Headless client to query AEM GraphQL APIs
// The endpoint is left blank since only persisted queries should be used to query AEM's GraphQL APIs
const aemHeadlessClient = new AEMHeadless({
serviceURL: aemHost,
endpoint: '', // Avoid non-persisted queries
auth: accessToken // accessToken only set if the 2nd command line parameter is set
})
...
}
AEM持久查询是通过HTTPGET执行的,因此, AEM Headless客户端(用于Node.js) 用于 执行持久GraphQL查询 ,并检索冒险内容。
通过调用 aemHeadlessClient.runPersistedQuery(...)
,并传递保留的GraphQL查询名称。 GraphQL返回数据后,将其传递到简化的 doSomethingWithDataFromAEM(..)
函数,该函数可打印结果 — 但通常会将数据发送到其他系统,或根据检索到的数据生成一些输出。
// index.js
async function run() {
...
try {
// Retrieve the data from AEM GraphQL APIs
data = await aemHeadlessClient.runPersistedQuery('wknd-shared/adventures-all')
// Do something with the data from AEM.
// A common use case is sending the data to another system.
await doSomethingWithDataFromAEM(data);
} catch (e) {
console.error(e.toJSON())
}
}