なぜAPIにフィルタリング機能が必要になるのか
Webサービスを運営している時に、サーバサイドではAPIにフィルタリング機能を求められる事がしばしば起こります。このようなAPIの一例として、タスク管理ツールでのタスクの一覧を返す以下のようなAPIがあります。
openapi: 3.0.1 info: title: OpenAPI definition version: v0 servers: - url: http://localhost:8080 description: Generated server url paths: /tasks: get: tags: - tasks-controller summary: タスクをリストアップする operationId: listTasks responses: "200": description: OK content: application/json: schema: $ref: "#/components/schemas/ListTasksResponse" components: schemas: ListTasksResponse: type: object properties: results: type: array items: $ref: "#/components/schemas/TaskDto" Task: type: object properties: id: type: string description: ID format: uuid title: type: string description: タスクの名前です。 description: type: string description: タスクの説明です。 completed: type: boolean description: 完了したかどうか。 creatAt: type: string description: 作成日時です。 format: date-time updateAt: type: string description: 更新日時です。 format: date-time description: タスクです。
このAPIに対して、「タスクの名前でフィルタリングしたい」、「未完了のものだけ返して欲しい」とか、「未完了のもので1週間以上更新のないものをリストアップしたい」というような要望が出てくるのは一般的です。フィルタリングの処理そのものを実装するのは、RDBMSなどのデータベースへのクエリに条件を追加するだけなので難しくはないでしょう。
続きを読む
