Using GraphQL with Ruby on Rails: 作業ログ

https://www.apollographql.com/blog/community/backend/using-graphql-with-ruby-on-rails/

apollo blogが一番わかりやすかったので見ながら作成

まずはpostgresqlをインストール。

sudo port install postgresql14

  postgresql14 has the following notes:
    To use the postgresql server, install the postgresql14-server port

sudo port install postgresql14-server

--->  Some of the ports you installed have notes:
  postgresql14-server has the following notes:
    To create a database instance, after install do
     sudo mkdir -p /opt/local/var/db/postgresql14/defaultdb
     sudo chown postgres:postgres /opt/local/var/db/postgresql14/defaultdb
     sudo su postgres -c 'cd /opt/local/var/db/postgresql14 && /opt/local/lib/postgresql14/bin/initdb -D /opt/local/var/db/postgresql14/defaultdb'

    A startup item has been generated that will aid in starting postgresql14-server with launchd. It is disabled by default. Execute the following
    command to start it, and to cause it to launch at startup:
    
        sudo port load postgresql14-server

上記のコマンドを上から流す。そうすると最後のコマンドのログはこんな感じ

$ sudo su postgres -c 'cd /opt/local/var/db/postgresql14 && /opt/local/lib/postgresql14/bin/initdb -D /opt/local/var/db/postgresql14/defaultdb'
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.

The database cluster will be initialized with locale "ja_JP.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "ja_JP.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

fixing permissions on existing directory /opt/local/var/db/postgresql14/defaultdb ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Tokyo
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    /opt/local/lib/postgresql14/bin/pg_ctl -D /opt/local/var/db/postgresql14/defaultdb -l logfile start

最後のコマンドを実行してみる

$ /opt/local/lib/postgresql14/bin/pg_ctl -D /opt/local/var/db/postgresql14/defaultdb -l logfile start
pg_ctl: could not open PID file "/opt/local/var/db/postgresql14/defaultdb/postmaster.pid": Permission denied
$ sudo /opt/local/lib/postgresql14/bin/pg_ctl -D /opt/local/var/db/postgresql14/defaultdb -l logfile start
Password:
pg_ctl: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.

Permission deniedなのにsudoをつけるとエラーになる。

さっきやってなかったコマンド

~ takayuki$ sudo port load postgresql14-server
Password:
--->  Loading startupitem 'postgresql14-server' for postgresql14-server
$ sudo ls -l /opt/local/var/db/postgresql14/defaultdb/postmaster.pid
-rw-------  1 postgres  postgres  107 11 30 22:19 /opt/local/var/db/postgresql14/defaultdb/postmaster.pid

pidファイルはpostgresユーザなのは当たり前

~ takayuki$ /opt/local/lib/postgresql14/bin/psql -U postgres
psql (14.0)
Type "help" for help.

postgres=# 

接続はできるので、とりあえず、先に進む。

やっとここでrails new。とりあえず今回はかなり単純に実行 rails new rails_graphql_practice -d postgresql

こんなエラーが出て失敗

checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
 --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header

https://stackoverflow.com/questions/6040583/cant-find-the-libpq-fe-h-header-when-trying-to-install-pg-gem

古い記事ですが、これのMacPortsを参考にするとこんな感じ

$ sudo gem install pg -- --with-pg-config=/opt/local/lib/postgresql14/bin/pg_config
Password:
Building native extensions with: '--with-pg-config=/opt/local/lib/postgresql14/bin/pg_config'
This could take a while...
Successfully installed pg-1.2.3
Parsing documentation for pg-1.2.3
Installing ri documentation for pg-1.2.3
Done installing documentation for pg after 0 seconds
1 gem installed

成功

bundle installも成功

ここから先は先ほどの記事の内容を上からなぞっていきます。

models作成

rails g model Artist first_name last_name email
rails g model Item title description:text image_url artist:references

そして、seedsの追記やdb:migrateなど、なんやかんやありまして、 rails g graphql:object itemするところでエラー。

bundle add graphqlでGemfileにgraphiql-railsも追加されるが、別途bundle installしないとgemが追加されてなかったです。

また、rails g graphql:object -hすると最後の行に

Create a GraphQL::ObjectType with the given name and fields.If the given type name matches an existing ActiveRecord model, the generated type will automatically include fields for the models database columns.

と表示されるので、databaseの内容を直接読み出してくれてるみたいです。

item_type.rbをgenerateした後でartist fieldを追加する

item_type.rb: add field :artist · na8esin/rails_graphql_practice@843f9fa · GitHub

こんな感じです。ここは流石に手動で追加しないといけないみたいです。

rails webpacker:installはやっぱり必要

artist_type.rbも生成した後で、rails sを実行したところエラー。

rails new の時に--apiつければいらないのだろうか?

何はともあれ、この時点でrails sで起動して、 http://localhost:3000/graphiql にアクセスするとクエリを実行することができます。

f:id:ta_watanabe:20211223104439p:plain

ログはこんな感じで出力されます。

この時点だと、全てのItemのArtistがtaylorなので同じArtistがn回呼び出されてます。 キャッシュが使われているので要件によってはこれでいいのかもしれないですが、 できれば

https://graphql-ruby.org/dataloader/overview.html

のあたりが使いたいです。

自分の描いたソース

GitHub - na8esin/rails_graphql_practice: railsでgraphqlやってみる