---
title: "イベントサブスクリプションのクエリ例"
slug: "query-examples-for-events-subscription"
abstract: "GraphQLを使ってイベントサブスクリプションを作成、更新、削除できます"
---
# イベントサブスクリプションのクエリ例

以下のGraphQLタイプのサブスクリプション操作でイベントサブスクリプションを行うことができます:

-   アイテム
-   職務
-   出版

利用可能な種類や操作の完全なリストについては、GraphQL IDEの組み込みドキュメントを参照してください。

## IISでWebSocketを有効にする

Graphqlのサブスクリプションは、2つの通信手段にWebSocketが必要です。

Windows Internet Information ServicesでWebSocket機能を有効にし、アクセストークンで認証するには:

1.  コントロールパネルで**Windows機能** を開き、WebSocketプロトコル機能を有効にしてください。
    
    ![The Windows Features dialog](https://doc.sitecore.com/api/markdown-image/xp/ja/developers/104/sitecore-experience-manager/images/windowson.png)
    
2.  機能が有効になったら、WindowsのIISにアクセスし、ルートサイトをクリックします。
    
3.  **管理**下で「**設定エディター**」をクリックします。
    
4.  セクション **のドロップダウンボックス** で「system.webServer/webSocket」と入力し、右側の「 **Unlock Section**」をクリックしてください。
    
    ![The Configuration Editor](https://doc.sitecore.com/api/markdown-image/xp/ja/developers/104/sitecore-experience-manager/images/websocket.png)
    
5.  **SitecoreCMInstance web.config**に追加してください:
    
    <system.webServer>
     <webSocket enabled="true" />
    </system.webServer>
    
6.  [アクセストークン](https://doc.sitecore.com/xp/ja/developers/104/sitecore-experience-manager/walkthrough--enabling-and-authorizing-requests-to-the-authoring-and-management-api.html#UUID-0d300dda-55bf-01f0-6762-33ec87516f7a_section-idm1403337571915802 "Obtain an access token")を作成しましょう。
    
7.  NodeJSでWebSocket GraphQLクライアントを作成する際は、graphql-wsを使ってAuthoringエンドポイントに接続する際、以下の例のようにアクセストークンを使用してください。
    
    import { createClient } from "graphql-ws";
    
    export const wsClient = createClient({
     url: "wss://{cm\_instance\_url}/sitecore/api/authoring/graphql/v1",
     connectionParams: () => ({
     access\_token: \`{access\_token}\`
     }),
     on: {
     connecting: () => console.log(" Connecting WS..."),
     connected: () => console.log(" WebSocket connected"),
     closed: (e) => console.log(" WebSocket closed:", e),
     error: (e) => console.log(" Full keys:", e)
     }
    });
    

## アイテムのクエリ例

以下のセクションでは、イベントサブスクリプションに関する例のクエリを掲載しています。

### アイテム追加時に

onItemAdaddedのサブスクリプションで「on item added」イベントデータを受け取ることができます。

#### クエリ { .notintoc }

subscription {
 onItemAdded(input: { database: "master"}) {
 name
 displayName
 path
 }
 }

#### 結果 { .notintoc }

Data:
 onItemAdded:
 displayName: "test item"
 itemId: "5f18db1b7afa43e99b6c84f7a0cb3bd8"
 name: "test item"
 path: "/sitecore/content/Home/test item"

### オンジョブ開始 { .notintoc }

onJobStartedのサブスクリプションで「オンジョブスタート」イベントデータを受け取ることができます。

#### クエリ { .notintoc }

subscription {
 onJobStarted(input: { jobName: "Sitecore.ListManagement.Operations.UpdateListOperationsAgent" }) {
 name
 jobState
 expiry
 done
 category
 queueTime
 handle
 siteName
 displayName
 }
 }

#### 結果 { .notintoc }

Data:
 onJobStarted:
 category: "schedule"
 displayName: "Sitecore.ListManagement.Operations.UpdateListOperationsAgent"
 done: true expiry: "2025-12-15T11:\`35:29\`.216Z"
 handle: "cef968ae-ba25-46ae-b06e-4020c2b84ba1"
 jobState: "Finished"
 name: "Sitecore.ListManagement.Operations.UpdateListOperationsAgent"
 queueTime: "2025-12-15T11:\`34:29\`.215Z"
 siteName: "scheduler"

### 出版状況更新 { .notintoc }

publishingStatusUpdatedのサブスクリプションで「publishingStatusUpdated」イベントデータを受け取ることができます。

#### クエリ { .notintoc }

subscription {
 publishingStatusUpdated(input: { operationId: "1bac04b7-8b97-4756-b0c9-38dcacc89b5b " }) {
 isDone
 isFailed
 state
 processed
 }
 }

#### 結果 { .notintoc }

Data:
 publishingStatusUpdated:
 isDone: true
 isFailed: false
 processed: 104
 state: "Finished"