---
schema_version: "1"
id: 1e7013923d581ca454e5e0de454622c82f727ebfe64e522e9e0b34e87dadc278
title: GraphQL 実装の応答の設定
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/anypoint-code-builder/tut-graphql-configure-flow-responses.md
canonical_url: https://docs.mulesoft.com/jp/anypoint-code-builder/tut-graphql-configure-flow-responses
source_site: docs.mulesoft.com
content_type: documentation
content_hash: a00eb9cc7475781a3dd9c7ceb6f9cb536b458f594a1b814c628decb7be97167d
description: MuleSoft Documentation Site
latest: true
---
# GraphQL 実装の応答の設定

![logo cloud IDE](/mulesoft/current/ja-jp/_/img/logos/deployment-cloud-ide.png "Cloud IDE") Cloud IDE

![logo desktop IDE](/mulesoft/current/ja-jp/_/img/logos/deployment-desktop-ide.png "Desktop IDE") Desktop IDE

**進行中のベータリリース**​: **クラウド IDE は進行中のベータリリースです**​。ベータ状態での Anypoint Code Builder の使用には、IDE で入手できる、該当するベータサービス契約条件が適用されます。

フローごとに特定の応答を返すように GraphQL 実装を設定します。

## [](#始める前に)始める前に

次の手順を実行します。

- [GraphQL API の実装](https://docs.mulesoft.com/jp/anypoint-code-builder/tut-graphql-implement-api)。
- [GraphQL プロジェクトをテストする](https://docs.mulesoft.com/jp/anypoint-code-builder/tut-graphql-test-first-implementation)

## [](#ロジックをフローに追加する)ロジックをフローに追加する

1. Anypoint Code Builder で ​**\[Books Implementation (ブック実装)\]**​ プロジェクトを開きます。
2. \[Explorer\] のプロジェクトディレクトリから ​`flows.xml`​ を開きます。
3. `Query.bookById`​ フロー内で、​`<graphql-router:data-fetcher/>`​ 要素の後の ​**Set Payload**​ コンポーネントを次の Set Payload 設定で​_置き換え_​ます。
    
    ```XML
    <set-payload value='{"id": 1, "name": "My Book", "pageCount": 28, "author": "author-1"}' mimeType="application/json" />
    ```
    
    これで、​`Query.bookById`​ フローの XML に次の設定が割り当てられます。
    
    ```XML
    <flow name="Query.bookById">
      <graphql-router:data-fetcher xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Query" fieldName="bookById"/>
      <set-payload value='{"id": 1, "name": "My Book", "pageCount": 28, "author": "author-1"}' mimeType="application/json" />
      <graphql-router:serialize xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Query" fieldName="bookById"/>
    </flow>
    ```
4. アクティビティバーの ​![](/mulesoft/current/ja-jp/anypoint-code-builder/_images/icon-run-debug.png)​ (​**\[Run and Debug (実行およびデバッグ)\]**​) アイコンをクリックし、​**\[Start Debugging (F5) (デバッグを開始 (F5))\]**​ アイコンをクリックして、アプリケーションを再ビルドします。
5. IDE から、コンソールのターミナルウィンドウを開きます。
    
    - デスクトップ IDE で、​**\[View (表示)\]**​ > ​**\[Terminal (ターミナル)\]**​ を選択する。
    - クラウド IDE で、​![](/mulesoft/current/ja-jp/anypoint-code-builder/_images/icon-menu.png)​ (メニュー) アイコンをクリックし、​**\[Terminal (ターミナル)\]**​ > ​**\[New Terminal (新規ターミナル)\]**​ を選択する。
    - いずれかの IDE で、Ctrl キーを押してからバッククォートキー (\`) を押す。
        
        アプリケーションがすでに実行されている場合、​**\[Start New (新たに開始)\]**​ をクリックして、デバッグモードでアプリケーションを再実行します。
6. コマンドプロンプトから次の ​`curl`​ コマンドを実行します。
    
    ```curl
    curl --request POST \
    --location 'http://localhost:8081/graphql' \
    --header 'Content-Type: application/json' \
    --data '{
        "query": "query bookById($id: ID) {bookById(id: $id){ id name pageCount author{ id firstName lastName}}}",
        "variables": {
            "id": 1
        }
    }'
    ```
    
    Anypoint Code Builder からターミナルに JSON 応答が返されます (読みやすくするために手動で再書式設定)。
    
    ```JSON
    { "data": {
                "bookById": {
                              "id":"1",
                              "name":"My Book",
                              "pageCount":28,
                              "author": {
                                          "id":"urn:uuid:123e4567-e89b-12d3-a456-426655440000",
                                          "firstName":"This is some example data",
                                          "lastName":"Hello World!"
                                        }
                            }
              }
    }
    ```

`bookById`​ エンドポイントをテストしたら、残りのフローをテストするために Set Payload を使用してモック応答を追加できます。次に例を示します。

```XML
<flow name="api-main-flow">
  <http:listener xmlns:http="http://www.mulesoft.org/schema/mule/http" config-ref="http-listener-config" path="${http.listener.path}"/>
  <graphql-router:route xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config"/>
</flow>
<flow name="Query.bookById">
  <graphql-router:data-fetcher xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Query" fieldName="bookById"/>
  <set-payload value='{"id": 1, "name": "My Book", "pageCount": 28, "author": "author-1"}' mimeType="application/json" />
  <graphql-router:serialize xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Query" fieldName="bookById"/>
</flow>
<flow name="Query.books">
  <graphql-router:data-fetcher xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Query" fieldName="books"/>
  <set-payload value='[{"id": 1, "name": "My Book", "pageCount": 28, "author": "author-1"},{"id": 2, "name": "His Book", "pageCount": 12, "author": "author-2"},{"id": 3, "name": "Her Book", "pageCount": 41, "author": "author-1"}]' mimeType="application/json"/>
  <graphql-router:serialize xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Query" fieldName="books"/>
</flow>
<flow name="Query.bestsellers">
  <graphql-router:data-fetcher xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Query" fieldName="bestsellers"/>
  <set-payload value='{"books": ["1,2"], "authors":["author-1", "author-2"]}' mimeType="application/json" />
  <graphql-router:serialize xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Query" fieldName="bestsellers"/>
</flow>
<flow name="Book.author">
  <graphql-router:data-fetcher xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Book" fieldName="author"/>
  <set-payload value='{"id": "author-1", "firstName": "John", "lastName": "Doe"}' mimeType="application/json" />
  <graphql-router:serialize xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Book" fieldName="author"/>
</flow>
<flow name="Bestsellers.books">
  <graphql-router:data-fetcher xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Bestsellers" fieldName="books"/>
  <set-payload value='[{"id": 1, "name": "My Book", "pageCount": 28, "author": "author-1"},{"id": 2, "name": "His Book", "pageCount": 12, "author": "author-2"},{"id": 2, "name": "Her Book", "pageCount": 41, "author": "author-1"}]' mimeType="application/json"/>
  <graphql-router:serialize xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Bestsellers" fieldName="books"/>
</flow>
<flow name="Bestsellers.authors">
  <graphql-router:data-fetcher xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Bestsellers" fieldName="authors"/>
  <set-payload value='[{"id": "author-1", "firstName": "John", "lastName": "Doe"},{"id": "author-2", "firstName": "Anie", "lastName": "Eberts"}]' mimeType="application/json" />
  <graphql-router:serialize xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Bestsellers" fieldName="authors"/>
</flow>
```