Flex Gateway新着情報
Governance新着情報
Monitoring API Manager
これは進行中のベータリリースです。ベータ状態での Anypoint Code Builder の使用には、該当するベータサービス契約条件が適用されます。 |
フローごとに特定の応答を返すように GraphQL 実装を設定します。
次の手順を実行します。
MuleSoft 環境をセットアップします。
詳細は、Anypoint Code Builder の使用開始を参照してください。
ブック API GraphQL スキーマを実装します。
GraphQL API の実装を参照してください。
ブック API GraphQL スキーマをテストします。
GraphQL 実装のテストを参照してください。
Anypoint Code Builder を起動します。
[Books Implementation (ブック実装)] プロジェクトを開きます。
flows.xml
ファイルを開きます。
Query.bookById フローの <graphql-router:data-fetcher/>
要素の後に改行を追加します。
「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>
<set-payload>
コンポーネントを次の JSON 応答で置き換えます。
<set-payload value='{"id": 1, "name": "My Book", "pageCount": 28, "author": "author-1"}' mimeType="application/json" />
アクティビティバーの [Run and Debug (実行およびデバッグ)] をクリックし、[Start Debugging (F5) (デバッグを開始 (F5))] をクリックします。
curl
を使用してローカルアドレスへの POST
要求を作成し、bookById
クエリを送信します。
例については、「GraphQL 実装のテスト」を参照してください。
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>