GraphQL 実装の応答の設定

logo cloud IDE Cloud IDE

logo desktop IDE Desktop IDE

これは進行中のベータリリースです。ベータ状態での Anypoint Code Builder の使用には、該当するベータサービス契約条件が適用されます。

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

始める前に

次の手順を実行します。

ロジックをフローに追加する

  1. Anypoint Code Builder を起動します。

  2. [Books Implementation (ブック実装)]​ プロジェクトを開きます。

  3. flows.xml​ ファイルを開きます。

  4. Query.bookById​ フローの ​<graphql-router:data-fetcher/>​ 要素の後に改行を追加します。

  5. 「set」​の入力を開始して、可能なコンポーネントのリストから ​[Set-Payload]​ を選択します。​Query.bookById​ フローは次のようになります。

    <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="#[]" doc:name="Set payload" doc:id="mtehoh" />
      <graphql-router:serialize xmlns:graphql-router="http://www.mulesoft.org/schema/mule/graphql-router" config-ref="api-router-config" objectTypeName="Query" fieldName="bookById"/>
    </flow>
  6. <set-payload>​ コンポーネントを次の JSON 応答で置き換えます。

    <set-payload value='{"id": 1, "name": "My Book", "pageCount": 28, "author": "author-1"}' mimeType="application/json" />
  7. アクティビティバーの ​[Run and Debug (実行およびデバッグ)]​ をクリックし、​[Start Debugging (F5) (デバッグを開始 (F5))]​ をクリックします。

  8. curl​ を使用してローカルアドレスへの ​POST​ 要求を作成し、​bookById​ クエリを送信します。

    例については、​「GraphQL 実装のテスト」​を参照してください。

  9. curl​ 要求を送信します。

    Anypoint Code Builder で設定済みの JSON 応答が返されます。

    {
        "data": {
            "bookById": {
                "id": "1",
                "name": "My Book",
                "pageCount": 28,
                "author": null
            }
        }
    }

bookById​ エンドポイントをテストしたら、カスタム応答を追加して残りのフローをテストします。

<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>