クエリの例

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

このトピックでは、一般的なクエリのニーズを満たすためにExperience Edge GraphQLスキーマをクエリする方法の例をいくつか説明します。

大事な

このトピックのクエリ例のほとんどは、Styleguideサンプル サイトから派生しています。アイテムIDとパスは、インポート/ソリューションによって異なります。

大事な

サイトでフォールバック言語をサポートしている場合は、開始する前にExperience Edge Connectorで 公開言語のフォールバックを有効にします

IDまたはパスでアイテムを取得する

コンテンツ ツリー内の任意の場所にあるアイテムをパスまたはIDでクエリして、フィールドやその他のプロパティを読み取ることができます。インラインフラグメントを使用して、アイテムを投影データテンプレートタイプにキャストし、フィールドをフィールドタイプにキャストできます。

クエリ

query {
  # path can be an item tree path or GUID-based id
  item(path: "/sitecore/content/Experience-Edge/home", language: "en") {
    # items can be cast to a Template type with inline fragments
    ... on AppRoute {
      pageTitle {
        value
      }
    }
    # fields can be cast to a Field type with inline fragments
    field(name: "pageTitle") {
      ... on TextField {
        value
      }
    }
  }
}

結果

{
  "data": {
    "item": {
      "pageTitle": {
        "value": "Welcome to Sitecore JSS"
      },
      "field": {
        "value": "Welcome to Sitecore JSS"
      }
    }
  }
}

複数のパスでフィルタリングされたアイテムを取得する

複数のパスに基づいてアイテムをフィルタリングできます。このクエリの例では、次のサイト構造を前提としています。

Content tree highlighting the items used in this example.

/home/graphqlアイテムとhome/styleguide/Page ComponentsアイテムのUUIDにそれぞれ0C6F4AAF-DD7E-5FAF-B9B0-61070D496055AB99BA2B-B606-51E3-BAD9-FD8358DE1333があるとします。/home/graphqlhome/styleguide/Page Componentsではない/homeアイテムと子孫、またはその子を取得したいと考えています。次のクエリでは、NCONTAINS演算子を使用して、これらの項目とその子のパスを結果から除外します。

クエリ

query {
  pageOne: search(
     where: {
       AND: [
         {
           name: "_path"
           value: "54C8E9B5-0B2C-5363-8FA6-D32A3A302F51"
           operator: CONTAINS
         }
         {
           AND: [
             {
               name: "_path"
               value: "0C6F4AAF-DD7E-5FAF-B9B0-61070D496055"
               operator: NCONTAINS
             }
             {
               name: "_path"
               value: "AB99BA2B-B606-51E3-BAD9-FD8358DE1333"
               operator: NCONTAINS
             }
           ]
         }
       ]
     }
     # defaults to 10
     first: 10
   ) {
     total
     pageInfo {
       endCursor
       hasNext
     }
     results {
       url {
         path
       }
     }
   }
 }

結果

検索クエリ結果には、パスが /home/graphqlhome/styleguide/Page Componentsのアイテム、またはその子孫は含まれませんが、/homeアイテムと残りの子アイテムが返されます。

{
  "data": {
    "pageOne": {
      "total": 5,
      "pageInfo": {
        "endCursor": "NQ==",
        "hasNext": false
      },
      "results": [
        {
          "url": {
            "path": "/styleguide/custom-route-type"
          }
        },
        {
          "url": {
            "path": "/styleguide"
          }
        },
        {
          "url": {
            "path": "/Page-Components/home-jss-main-ContentBlock-1"
          }
        },
        {
          "url": {
            "path": "/Page-Components"
          }
        },
        {
          "url": {
            "path": "/"
          }
        }
      ]
    }
  }
}

URLの項目レイアウトを取得する

ヘッドレスSDKで最も頻繁に使用され、サイト名とHTTP URLでアイテムを検索し、フレームワーク固有のSitecoreプレースホルダー実装でレンダリングするためのレイアウト サービス出力を取得できます。

クエリ

query {
  layout(site: "experienceedge", routePath: "/", language: "en") {
    item {
      rendered
    }
  }
}

結果

{
  "data": {
    "layout": {
      "item": {
        "rendered": {
          "sitecore": {
            "context": {
              "pageEditing": false,
              "site": {
                "name": "experienceedge"
              },
              "pageState": "normal",
              "language": "en",
              "itemPath": "/"
            },
            "route": {
              "name": "home",
              "displayName": "home",
              "fields": {
                "pageTitle": {
                  "value": "Welcome to Sitecore JSS"
                }
              },
              "databaseName": "web",
              "deviceId": "fe5d7fdf-89c0-4d99-9aa3-b5fbd009c9f3",
              "itemId": "2bde2083-5542-5569-ac5e-f1f42e29f75a",
              "itemLanguage": "en",
              "itemVersion": 1,
              "layoutId": "714096fc-3998-52cf-b87a-951fc7ce39d3",
              "templateId": "e34d8c4f-7e6c-560c-be24-e4d1fcdb16d8",
              "templateName": "App Route",
              "placeholders": {
                "jss-main": [
                  {
                    "uid": "2c4a53cc-9da8-5f51-9d79-6ee2fc671b2d",
                    "componentName": "ContentBlock",
                    "dataSource": "{CC3A044C-776B-5423-9BEA-CC5C2EDA8C9B}",
                    "fields": {
                      "heading": {
                        "value": "Welcome to Sitecore JSS"
                      },
                      "content": {
                        "value": "<p>Thanks for using JSS!! Here are some resources to get you started:</p>\n\n<h3><a href=\"https://jss.sitecore.net\" rel=\"noopener noreferrer\">Documentation</a></h3>\n<p>The official JSS documentation can help you with any JSS task from getting started to advanced techniques.</p>\n\n<h3><a href=\"/styleguide\">Styleguide</a></h3>\n<p>The JSS styleguide is a living example of how to use JSS, hosted right in this app.\nIt demonstrates most of the common patterns that JSS implementations may need to use,\nas well as useful architectural patterns.</p>\n\n<h3><a href=\"/graphql\">GraphQL</a></h3>\n<p>JSS features integration with the Sitecore GraphQL API to enable fetching non-route data from Sitecore - or from other internal backends as an API aggregator or proxy.\nThis route is a living example of how to use an integrate with GraphQL data in a JSS app.</p>\n\n<div class=\"alert alert-dark\">\n  <h4>This app is a boilerplate</h4>\n  <p>The JSS samples are a boilerplate, not a library. That means that any code in this app is meant for you to own and customize to your own requirements.</p>\n  <p>Want to change the lint settings? Do it. Want to read manifest data from a MongoDB database? Go for it. This app is yours.</p>\n</div>\n\n<div class=\"alert alert-dark\">\n  <h4>How to start with an empty app</h4>\n  <p>To remove all of the default sample content (the Styleguide and GraphQL routes) and start out with an empty JSS app:</p>\n  <ol>\n    <li>Delete <code>/src/components/Styleguide*</code> and <code>/src/components/GraphQL*</code></li>\n    <li>Delete <code>/sitecore/definitions/components/Styleguide*</code>, <code>/sitecore/definitions/templates/Styleguide*</code>, and <code>/sitecore/definitions/components/GraphQL*</code></li>\n    <li>Delete <code>/data/component-content/Styleguide</code></li>\n    <li>Delete <code>/data/content/Styleguide</code></li>\n    <li>Delete <code>/data/routes/styleguide</code> and <code>/data/routes/graphql</code></li>\n    <li>Delete <code>/data/dictionary/*.yml</code></li>\n  </ol>\n</div>\n"
                      }
                    }
                  }
                ]
              }
            }
          }
        }
      }
    }
  }
}

アイテムバケット内のアイテムをページ分割する

Sitecoreアイテム バケットを使用する場合、一般的な要件は、そのバケット内の特定のテンプレートから派生するすべてのアイテムをページ分割することです。次の検索クエリは、これらのユースケースに役立ちます。

クエリ

fragment bucketItemFields on Item {
  ... on AppRoute {
    pageTitle {
      value
    }
  }
  url {
    path
  }
}

query {
  search(
    where: {
      AND: [
        {
          name: "_templates"
          value: "0929f436c3f3500a9f8bd1c57a67a192"
          operator: CONTAINS
        }
        {
          name: "_path"
          value: "7ab00eca411249818420666fc9110faf"
          operator: CONTAINS
        }
      ]
    }
    first: 5
    orderBy: { name: "pageTitle", direction: ASC }
  ) {
    results {
      ...bucketItemFields
    }
    pageInfo {
      endCursor
      hasNext
    }
    total
  }
}

結果

{
  "data": {
    "search": {
      "results": [
        {
          "pageTitle": {
            "value": "Route A"
          },
          "url": {
            "path": "/bucket/2021/05/13/13/12/Route-A"
          }
        },
        {
          "pageTitle": {
            "value": "Route B"
          },
          "url": {
            "path": "/bucket/2021/05/13/13/12/Route-B"
          }
        },
        {
          "pageTitle": {
            "value": "Route C"
          },
          "url": {
            "path": "/bucket/2021/05/13/13/12/Route-C"
          }
        },
        {
          "pageTitle": {
            "value": "Route D"
          },
          "url": {
            "path": "/bucket/2021/05/13/13/12/Route-D"
          }
        },
        {
          "pageTitle": {
            "value": "Route E"
          },
          "url": {
            "path": "/bucket/2021/05/13/13/13/Route-E"
          }
        }
      ],
      "pageInfo": {
        "endCursor": "NQ==",
        "hasNext": true
      },
      "total": 26
    }
  }
}

サイトのルート項目を取得する

Experience Edgeスキーマでは、現在、サイトとそのルート項目のクエリは許可されていませんが、layoutクエリを使用してサイトのルート項目を検索することはできます。

クエリ

query {
  layout(site: "experienceedge", routePath: "/", language: "en") {
    item {
      homeItemPath: path
      contentRoot: parent {
        id
        path
      }
    }
  }
}

結果

{
  "data": {
    "layout": {
      "item": {
        "homeItemPath": "/sitecore/content/Experience-Edge/home",
        "contentRoot": {
          "id": "5AAA894246145088B7AD604B4D177B39",
          "path": "/sitecore/content/Experience-Edge"
        }
      }
    }
  }
}

すべてのサイトパス/ページを取得する

Experience Edgeスキーマのsearchクエリを使用して、特定のパスの下にあるレイアウトデータを持つすべてのアイテムを検索できます。これは、たとえば、静的サイト ジェネレータのパスを取得したり、サイト マップを作成したりする場合に便利です。検索クエリの結果はページ分割され、デフォルトでは10件の結果のみが返されます。 endCursorプロパティをfirstafterクエリ引数と共に使用して、結果をページ分割する必要があります。

クエリ

query {
  pageOne: search(
    where: {
      AND:[
        {
          name: "_path",
          value: "5AAA894246145088B7AD604B4D177B39",
          operator: CONTAINS
        },
        { name: "_language",  value: "en" },
        { name: "_hasLayout", value: "true" }
      ]
    }
    # defaults to 10
    first: 2
  ) {
    total
    pageInfo {
      endCursor
      hasNext
    }
    results {
      url {
        path
      }
    }
  },
  
  # this would be a subsequent query from your client
  pageTwo: search(
    where: {
      AND:[
        {
          name: "_path",
          value: "5AAA894246145088B7AD604B4D177B39",
          operator: CONTAINS
        },
        { name: "_language",  value: "en" },
        { name: "_hasLayout", value: "true" }
      ]
    }
    first: 2,
    # obtained the value from the "endCursor" field in "pageOne" query results.
    # you can do this by using either a query without an "after" parameter, or a query with an empty "after" parameter (after: "")
    after: "Mg=="
  ) {
    total
    pageInfo {
      endCursor
      hasNext
    }
    results {
      url {
        path
      }
    }
  }
}

結果

{
  "data": {
    "pageOne": {
      "total": 6,
      "pageInfo": {
        "endCursor": "Mg==",
        "hasNext": true
      },
      "results": [
        {
          "url": {
            "path": "/"
          }
        },
        {
          "url": {
            "path": "/graphql"
          }
        }
      ]
    },
    "pageTwo": {
      "total": 6,
      "pageInfo": {
        "endCursor": "NA==",
        "hasNext": true
      },
      "results": [
        {
          "url": {
            "path": "/graphql/sample-1"
          }
        },
        {
          "url": {
            "path": "/graphql/sample-2"
          }
        }
      ]
    }
  }
}

ビルド サイト ナビゲーション

このサンプル クエリは 、サイト ルートの検索 と、項目の子を走査する機能を組み合わせたものです。 hasLayout引数を使用してレイアウト(ナビゲート可能ページ)の項目のみを検索し、includeTemplateIds引数を使用して(ベース)データテンプレートでフィルタリングできます。

クエリ

fragment navigationFields on Item {
  ... on AppRoute {
    pageTitle {
      value
    }
  }
  url {
    path
  }
}

query {
  layout(site: "experienceedge", routePath: "/", language: "en") {
    item {
      ...navigationFields
      children(hasLayout: true, includeTemplateIDs: "e34d8c4f7e6c560cbe24e4d1fcdb16d8") {
        results {
          ...navigationFields
          children(hasLayout: true, includeTemplateIDs: "e34d8c4f7e6c560cbe24e4d1fcdb16d8") {
            results {
            	...navigationFields
            }
          }
        }
      }
    }
  }
}

結果

{
  "data": {
    "layout": {
      "item": {
        "pageTitle": {
          "value": "Welcome to Sitecore JSS"
        },
        "url": {
          "path": "/"
        },
        "children": {
          "results": [
            {
              "pageTitle": {
                "value": "GraphQL | Sitecore JSS"
              },
              "url": {
                "path": "/graphql"
              },
              "children": {
                "results": [
                  {
                    "pageTitle": {
                      "value": "Sample 1 Page Title"
                    },
                    "url": {
                      "path": "/graphql/sample-1"
                    }
                  },
                  {
                    "pageTitle": {
                      "value": "Sample 2 Page Title"
                    },
                    "url": {
                      "path": "/graphql/sample-2"
                    }
                  }
                ]
              }
            },
            {
              "pageTitle": {
                "value": "Styleguide | Sitecore JSS"
              },
              "url": {
                "path": "/styleguide"
              },
              "children": {
                "results": [
                  {
                    "pageTitle": {
                      "value": "Custom Route Type | Sitecore JSS"
                    },
                    "url": {
                      "path": "/styleguide/custom-route-type"
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

ページ階層リンクを作成する

項目グラフ タイプのancestorsフィールドを使用して、階層リンク ナビゲーションを作成できます。 hasLayout引数を使用して、レイアウト (ナビゲート可能なページ) を持つ項目のみを検索し、includeTemplateIdsを使用して (基本) データ テンプレートでフィルター処理します。

クエリ

fragment breadcrumbFields on Item {
  ... on AppRoute {
    pageTitle {
      value
    }
  }
  url {
    path
  }
}

query {
  # Assume item id is available on page
  item(path: "e2c5c62f42a95b17bc0e71bc9193db9a", language: "en") {
    ...breadcrumbFields
    ancestors(
      hasLayout: true
      includeTemplateIDs: "e34d8c4f7e6c560cbe24e4d1fcdb16d8"
    ) {
      ...breadcrumbFields
    }
  }
}

結果

{
  "data": {
    "item": {
      "pageTitle": {
        "value": "Custom Route Type | Sitecore JSS"
      },
      "url": {
        "path": "/styleguide/custom-route-type"
      },
      "ancestors": [
        {
          "pageTitle": {
            "value": "Styleguide | Sitecore JSS"
          },
          "url": {
            "path": "/styleguide"
          }
        },
        {
          "pageTitle": {
            "value": "Welcome to Sitecore JSS"
          },
          "url": {
            "path": "/"
          }
        }
      ]
    }
  }
}

SitecoreヘッドレスSDKでアイテム フィールドを使用する

さまざまなSitecoreヘッドレスSDKにはすべて、フィールド値をレンダリングするためのヘルパーが含まれています。これらのヘルパーは、インライン編集のコンテキストで複雑なフィールド (画像やリンクなど) と編集可能な値をレンダリングするのに役立ちます。Experience Edgeスキーマの項目フィールドには、これらのフィールドヘルパーと互換性のある形式でフィールドを出力するjsonValueが含まれます。

たとえば、次のリンクフィールドは、ReactまたはLinkフィールドヘルパーを使用してNext.jsでレンダリングできます。

<Link field={data.linkFieldExample?.externalLink?.jsonValue} />

クエリ

query {
  richTextFieldExample: item(path: "d979794f76fd51829f3ece48d16dc36c", language: "en") {
    displayName
    ... on StyleguideFieldUsageRichText {
      sample {
        jsonValue
      }
    }
  }
  imageFieldExample: item(path: "c05be0b464885343b5670145b6815d03", language: "en") {
    displayName
    ... on StyleguideFieldUsageImage {
      sample1 {
        jsonValue
      }
    }
  }
  linkFieldExample: item(path: "d299e65e1f6c5ea3b71381c8e2f85baf", language: "en") {
    displayName
    ... on StyleguideFieldUsageLink {
      externalLink {
        jsonValue
      }
    }
  }
}

結果

{
  "data": {
    "richTextFieldExample": {
      "displayName": "Styleguide-FieldUsage-RichText-3",
      "sample": {
        "jsonValue": {
          "value": "<p>This is a sample rich text field. <mark>HTML is always supported.</mark> In Sitecore, editors will see a WYSIWYG editor for these fields.</p>"
        }
      }
    },
    "imageFieldExample": {
      "displayName": "Styleguide-FieldUsage-Image-4",
      "sample1": {
        "jsonValue": {
          "value": {
            "src": "https://cm.experience_edge.localhost/-/media/experienceedge/data/media/img/sc_logo.png?iar=0&hash=F4F969FF95009AE0A80FA094C497ED25",
            "alt": "Sitecore Logo"
          }
        }
      }
    },
    "linkFieldExample": {
      "displayName": "Styleguide-FieldUsage-Link-9",
      "externalLink": {
        "jsonValue": {
          "value": {
            "href": "https://www.sitecore.com",
            "text": "Link to Sitecore",
            "url": "https://www.sitecore.com",
            "linktype": "external"
          }
        }
      }
    }
  }
}
この記事を改善するための提案がある場合は、 お知らせください!