原文

What are views?

View を用いると、独自に定義した query の結果を(訳注:この query は GraphQL の query ではなく、SQL の query のこと。)あたかもそのテーブルが存在するかのように扱うことができます。View を用いることで仮想的に生じるテーブルは、物理的には保存されていません。つまり、View で定義した query が、View を参照するたびに実行され、その結果が仮想的なテーブルとして提供されます。

Views can be used to expose the results of a custom query as a virtual table. Views are not persisted physically i.e. the query defining a view is executed whenever data is requested from the view.

Hasura GraphQL engine は、作成した view を GraphQL API として使用できる状態に自動的にしてくれます。つまり view に対して、通常の table と同じく(訳注:GraphQLの)query や subscription を用いて、データにアクセスすることができます。

Hasura GraphQL engine lets you expose views over the GraphQL API to allow querying them using both queries and subscriptions just like regular tables.

Creating & exposing views

View は SQL を Hasura console から実行することで生成できます。

Views can be created using SQL which can be run in the Hasura console:

  • Hasura console で Data => SQL セクションに移動する。
  • View を記述する。
  • Track this チェックボックスを選択した状態にする。こうすることで作成した View が GraphQL API からも認識されるようになります。
  • Run ボタンを押して生成します。
  • Head to the Data -> SQL section of the Hasura console
  • Enter your create view SQL statement
  • Select the Track this checkbox to expose the new view over the GraphQL API
  • Hit the Run button

Use cases

View は、なんらかの独自に定義したロジックに基づいてデータを扱いたい場合に、一番適切な手法です。ですが、もしそのロジックがユーザーからの情報の入力を必要としている場合には custom SQL functions を使用してください。(訳注:つまりその View を使うための GraphQL クエリが input を必要とする場合には、custom SQL functions を使用する必要があります。例えば GraphQL query で userId を受け取っておいて、その userId を変数としてみなし、SQL の中の where で絞り込みたい、といった場合です。)

Views are ideal solutions for retrieving some derived data based on some custom business logic. If your custom logic requires any user input, you should use custom SQL functions instead.

ではいくつかの実例をみていきましょう。

Let’s see a few example use cases for views:

Example: Group by and then aggregate

authorarticle という schema ある状況で、各 author ごとの「記事評価の平均」をどのように算出すればいいかみてみましょう。

Let’s see how to fetch the average article rating for each author in our author/article schema.

各 author ごとの「記事評価の平均」を算出する View を生成する SQL query は以下のようになります。

A view that averages the rating of articles for each author can be created using the following SQL query:

viewをつくる
CREATE VIEW author_average_rating AS
  SELECT author_id, avg(rating)
    FROM article
    GROUP BY author_id

Example: Hide certain fields of a table

公開してはいけない情報がテーブルに含まれていたとしましょう。そういう場合に view を用いることで、公開可能な情報だけを持ったテーブルを作ることができます。

Say, we have some sensitive information in a table which we wouldn’t want to expose. We can create a view that only exposes the non-sensitive fields.

author table が例えば id, name, city, email, phone, address という項目をもっているとして、このなかの email, phone and address を公開しないようにしたいとしましょう。その場合は、以下の SQL query で条件を満たす View を生成することができます。

Let’s say our author table has the fields id, name, city, email, phone, address and we want to hide the email, phone and address fields. We can create the following view to achieve this:

viewをつくる
CREATE VIEW author_public AS
  SELECT id, name, city
    FROM author