Rails API × GraphQL プロジェクトを作成する際にハマったこと

## 概要

以下の記事を参考にプロジェクトを作ろうとしたところ私のmac環境で次のような問題でハマったためまとめます。

- [【Rails】graphql-rubyでAPIを作成 - @k-penguin-sato](https://qiita.com/k-penguin-sato/items/07fef2f26fd6339e0e69)
- [Rails APIモードで始めるGraphQL - @dkawabata](https://qiita.com/dkawabata/items/4fd965ee6d7295386a8b)


## 環境

- macOS Mojave v10.14.6
- Rails 5.0.7.2

## `make: *** [mysql2.bundle] Error 1`

これはGraphQL関係なく、mysql2の `$ bundle install` エラーでした。

```
$ bundle install
...
Fetching mysql2 0.5.3
Installing mysql2 0.5.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
    current directory: /Users/gremito/.gem/gems/mysql2-0.5.3/ext/mysql2
/Users/gremito/.rbenv/versions/2.7.1/bin/ruby -I /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0 -r ./siteconf20201125-31525-3obuae.rb
extconf.rb
checking for rb_absint_size()... yes
checking for rb_absint_singlebit_p()... yes
checking for rb_wait_for_single_fd()... yes
-----
Using mysql_config at /usr/local/opt/mysql@5.7/bin/mysql_config
-----
checking for mysql.h... yes
checking for errmsg.h... yes
checking for SSL_MODE_DISABLED in mysql.h... yes
checking for SSL_MODE_PREFERRED in mysql.h... yes
checking for SSL_MODE_REQUIRED in mysql.h... yes
checking for SSL_MODE_VERIFY_CA in mysql.h... yes
checking for SSL_MODE_VERIFY_IDENTITY in mysql.h... yes
checking for MYSQL.net.vio in mysql.h... yes
checking for MYSQL.net.pvio in mysql.h... no
checking for MYSQL_ENABLE_CLEARTEXT_PLUGIN in mysql.h... yes
checking for SERVER_QUERY_NO_GOOD_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_NO_INDEX_USED in mysql.h... yes
checking for SERVER_QUERY_WAS_SLOW in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_ON in mysql.h... yes
checking for MYSQL_OPTION_MULTI_STATEMENTS_OFF in mysql.h... yes
checking for my_bool in mysql.h... yes
-----
Don't know how to set rpath on your system, if MySQL libraries are not in path mysql2 may not load
-----
-----
Setting libpath to /usr/local/opt/mysql@5.7/lib
-----
creating Makefile
current directory: /Users/gremito/.gem/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR=" clean
current directory: /Users/gremito/.gem/gems/mysql2-0.5.3/ext/mysql2
make "DESTDIR="
compiling client.c
client.c:787:14: warning: incompatible pointer types passing 'VALUE (void *)' (aka 'unsigned long (void *)') to parameter of type 'VALUE
(*)(VALUE)' (aka 'unsigned long (*)(unsigned long)') [-Wincompatible-pointer-types]
  rb_rescue2(do_send_query, (VALUE)&args, disconnect_and_raise, self, rb_eException, (VALUE)0);
             ^~~~~~~~~~~~~
/Users/gremito/.rbenv/versions/2.7.1/include/ruby-2.7.0/ruby/ruby.h:1988:25: note: passing argument to parameter here
VALUE rb_rescue2(VALUE(*)(VALUE),VALUE,VALUE(*)(VALUE,VALUE),VALUE,...);
                        ^
client.c:795:16: warning: incompatible pointer types passing 'VALUE (void *)' (aka 'unsigned long (void *)') to parameter of type 'VALUE
(*)(VALUE)' (aka 'unsigned long (*)(unsigned long)') [-Wincompatible-pointer-types]
    rb_rescue2(do_query, (VALUE)&async_args, disconnect_and_raise, self, rb_eException, (VALUE)0);
               ^~~~~~~~
/Users/gremito/.rbenv/versions/2.7.1/include/ruby-2.7.0/ruby/ruby.h:1988:25: note: passing argument to parameter here
VALUE rb_rescue2(VALUE(*)(VALUE),VALUE,VALUE(*)(VALUE,VALUE),VALUE,...);
                        ^
2 warnings generated.
compiling infile.c
compiling mysql2_ext.c
compiling result.c
compiling statement.c
linking shared-object mysql2/mysql2.bundle
ld: library not found for -lssl
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [mysql2.bundle] Error 1
make failed, exit code 2
Gem files will remain installed in /Users/gremito/.gem/gems/mysql2-0.5.3 for inspection.
Results logged to /Users/gremito/.gem/extensions/x86_64-darwin-18/2.7.0/mysql2-0.5.3/gem_make.out
An error occurred while installing mysql2 (0.5.3), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.5.3' --source 'https://rubygems.org/'` succeeds before bundling.
In Gemfile:
  mysql2
```

### 解決方法

以下のコマンドを実行した後に再度 `$ bundle install` すると正常にインストールできました。
原因は、以下の参考にした記事で解説されてあります。

- [mysql2 version 0.5.2 Installation Failed [duplicate]](https://stackoverflow.com/questions/53755775/mysql2-version-0-5-2-installation-failed)
- [Install Ruby On Rails on
macOS 10.14 Mojave](https://gorails.com/setup/osx/10.14-mojave)



```
$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Password:
installer: Package name is macOS_SDK_headers_for_macOS_10.14
installer: Installing at base path /

installer: The install was successful.
```

## `Sprockets::Railtie::ManifestNeededError`

標準ライブラリの `sprockets`によって起きたエラーでした。

```
$ rails generate graphql:install
...
But did not, please create this file and use it to link any assets that need
to be rendered by your app:

Example:
  //= link_tree ../images
  //= link_directory ../javascripts .js
  //= link_directory ../stylesheets .css
and restart your server

For more information see: https://github.com/rails/sprockets/blob/070fc01947c111d35bb4c836e9bb71962a8e0595/UPGRADING.md#manifestjs (Sprockets::Railtie::ManifestNeededError)
        from /Users/gremito/.gem/gems/railties-5.0.7.2/lib/rails/initializable.rb:30:in `instance_exec'
        from /Users/gremito/.gem/gems/railties-5.0.7.2/lib/rails/initializable.rb:30:in `run'
        from /Users/gremito/.gem/gems/railties-5.0.7.2/lib/rails/initializable.rb:55:in `block in run_initializers'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/tsort.rb:228:in `block in tsort_each'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/tsort.rb:431:in `each_strongly_connected_component_from'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/tsort.rb:349:in `block in each_strongly_connected_component'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/tsort.rb:347:in `each'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/tsort.rb:347:in `call'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/tsort.rb:347:in `each_strongly_connected_component'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/tsort.rb:226:in `tsort_each'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/tsort.rb:205:in `tsort_each'
        from /Users/gremito/.gem/gems/railties-5.0.7.2/lib/rails/initializable.rb:54:in `run_initializers'
        from /Users/gremito/.gem/gems/railties-5.0.7.2/lib/rails/application.rb:352:in `initialize!'
        from /Users/gremito/rails/graphql-sample/config/environment.rb:5:in `<top (required)>'
        from /Users/gremito/.gem/gems/spring-2.1.1/lib/spring/application.rb:106:in `require'
        from /Users/gremito/.gem/gems/spring-2.1.1/lib/spring/application.rb:106:in `preload'
        from /Users/gremito/.gem/gems/spring-2.1.1/lib/spring/application.rb:157:in `serve'
        from /Users/gremito/.gem/gems/spring-2.1.1/lib/spring/application.rb:145:in `block in run'
        from /Users/gremito/.gem/gems/spring-2.1.1/lib/spring/application.rb:139:in `loop'
        from /Users/gremito/.gem/gems/spring-2.1.1/lib/spring/application.rb:139:in `run'
        from /Users/gremito/.gem/gems/spring-2.1.1/lib/spring/application/boot.rb:19:in `<top (required)>'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
        from /Users/gremito/.rbenv/versions/2.7.1/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:72:in `require'
        from -e:1:in `<main>'
```

### 解決方法

バージョンを落とすしか方法がありませんでした・・・

- [\`manifest.js\` and dynamic assets to link #654](https://github.com/rails/sprockets/issues/654)
- [[Rails6][sprockets4.0.0]Sprocketsを無効にするためにapp/assetsフォルダ以下を削除すると、Sprockets::Railtie::ManifestNeededErrorが出た - @sasakura_870](https://qiita.com/sasakura_870/items/106484f88c857bd9563e)

```
$ bundle exec gem uninstall sprockets
...

Select gem to uninstall:
 1. sprockets-3.7.1
 2. sprockets-3.7.2
 3. sprockets-4.0.2
 4. All versions
> 4
Successfully uninstalled sprockets-3.7.1
Successfully uninstalled sprockets-3.7.2
NOTE: Gem::Specification#rubyforge_project= is deprecated with no replacement. It will be removed on or after 2019-12-01.
Gem::Specification#rubyforge_project= called from /Users/gremito/.gem/specifications/erubis-2.7.0.gemspec:17.
NOTE: Gem::Specification#rubyforge_project= is deprecated with no replacement. It will be removed on or after 2019-12-01.
Gem::Specification#rubyforge_project= called from /Users/gremito/.gem/specifications/rack-test-0.6.3.gemspec:17.

You have requested to uninstall the gem:
        sprockets-4.0.2

sprockets-rails-3.2.2 depends on sprockets (>= 3.0.0)
If you remove this gem, these dependencies will not be met.
Continue with Uninstall? [yN]  y
Remove executables:
        sprockets

in addition to the gem? [Yn]  y
Removing sprockets
Successfully uninstalled sprockets-4.0.2

$ rm -fr Gemfile.lock
```

`Gemfile`に

> gem 'sprockets', '< 4'

を追記して再度 `$ bundle install`して正常にダウングレードできたら `$ rails generate graphql:install` を再度実行して終了。