Don't access the database in after_initialize

Rails 5.2 crashes in the `db:create` task because it tries to run the
`after_initialize` block before the database is created.

The easiest way to solve it is to move the code out of the initializer
and calculate the API type definitions on demand. Note results are still
cached using a class instance variable (not to be confused with a class
variable), and so once definitions are obtained, they will remain
constant until the application is restarted, even in the development
environment.
This commit is contained in:
Javi Martín
2020-05-17 19:13:54 +02:00
parent 9837b1ab74
commit 6fd9a286d7
6 changed files with 15 additions and 21 deletions

View File

@@ -29,7 +29,7 @@ class GraphqlController < ApplicationController
private
def consul_schema
api_types = GraphQL::ApiTypesCreator.create(API_TYPE_DEFINITIONS)
api_types = GraphQL::ApiTypesCreator.create
query_type = GraphQL::QueryTypeCreator.create(api_types)
GraphQL::Schema.define do