Actix GraphQL Example

RestAPIに疲れてきたので、GraphQLでwebアプリを動かしてみたいと思います。

参考にしたのは本家actixのexampleのjuniper-advancedです。
https://github.com/actix/examples/tree/master/juniper-advanced

まずはmysqlのinstall

```ubuntu:コマンド
sudo apt install mysql-server mysql-client
```

rootのパスワードを設定（いらないかも）


```ubuntu:コマンド
sudo mysql_secure_installation
```

```ubuntu:出力
Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Please set the password for root here.

New password: 　＜＝10文字くらいのパスワードを入力しました

Re-enter new password: 

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!
```

rootで入って、webアプリで使うユーザを作成してsqlファイルを実行

```mysql:コマンド
sudo mysql -u root -p
CREATE USER 'user'@'localhost' IDENTIFIED BY 'password';
use dbname
SOURCE /home/ubuntu/examples/juniper-advanced/mysql-schema.sql
```

ユーザに権限を追加

```mysql:コマンド
grant create on *.* to user@localhost;
grant update on *.* to user@localhost;
grant delete on *.* to user@localhost;
show grants for user@localhost;
```

```mysql:出力
show grants for user@localhost;
```

.envをコピーしてcargoを実行

```rust:コマンド
cp .env.example .env
cargo run
```

こんな感じで出力されたら成功

```ubuntu:出力
Finished dev [unoptimized + debuginfo] target(s) in 0.13s
     Running `/home/ubuntu/examples/target/debug/juniper-advanced`
[2020-10-11T03:53:38Z INFO  actix_server::builder] Starting 12 workers
[2020-10-11T03:53:38Z INFO  actix_server::builder] Starting "actix-web-service-127.0.0.1:8080" service on 127.0.0.1:8080
[2020-10-11T03:54:10Z INFO  actix_web::middleware::logger] 127.0.0.1:56920 "GET /graphiql HTTP/1.1" 200 1684 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:81.0) Gecko/20100101 Firefox/81.0" 0.000352
```

graphiqlにアクセス
"http://127.0.0.1:8080/graphiql"
![スクリーンショット 2020-10-11 13-24-08.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/570009/27348393-2bd6-fc85-0db3-91ebe3941c2c.png)

画面の右側のエラーはqueryを投げて失敗しました。
まだまだGraphQLの使い方がわかっていないので、学習します。

以上