From 6fd9a286d781a37a8c52997f3b37759039057b01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Sun, 17 May 2020 19:13:54 +0200 Subject: [PATCH] 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. --- app/controllers/graphql_controller.rb | 2 +- config/application.rb | 1 - config/initializers/graphql_api.rb | 11 ----------- lib/graph_ql/api_types_creator.rb | 6 +++++- spec/lib/graph_ql/query_type_creator_spec.rb | 14 ++++++++------ spec/lib/graphql_spec.rb | 2 +- 6 files changed, 15 insertions(+), 21 deletions(-) delete mode 100644 config/initializers/graphql_api.rb diff --git a/app/controllers/graphql_controller.rb b/app/controllers/graphql_controller.rb index d8233a685..5b1c27d16 100644 --- a/app/controllers/graphql_controller.rb +++ b/app/controllers/graphql_controller.rb @@ -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 diff --git a/config/application.rb b/config/application.rb index bb5a7580b..a44e143ca 100644 --- a/config/application.rb +++ b/config/application.rb @@ -60,7 +60,6 @@ module Consul config.after_initialize do Globalize.set_fallbacks_to_all_available_locales - GraphQLApi::Loader.setup end config.assets.paths << Rails.root.join("app", "assets", "fonts") diff --git a/config/initializers/graphql_api.rb b/config/initializers/graphql_api.rb deleted file mode 100644 index cf7b4bc03..000000000 --- a/config/initializers/graphql_api.rb +++ /dev/null @@ -1,11 +0,0 @@ -module GraphQLApi - class Loader - def self.setup - if ActiveRecord::Base.connection.tables.any? - api_config = YAML.load_file("./config/api.yml") - GraphqlController.const_set "API_TYPE_DEFINITIONS", - GraphQL::ApiTypesCreator.parse_api_config_file(api_config) - end - end - end -end diff --git a/lib/graph_ql/api_types_creator.rb b/lib/graph_ql/api_types_creator.rb index 823e430a8..3fbb3d704 100644 --- a/lib/graph_ql/api_types_creator.rb +++ b/lib/graph_ql/api_types_creator.rb @@ -10,7 +10,7 @@ module GraphQL string: GraphQL::STRING_TYPE }.freeze - def self.create(api_types_definitions) + def self.create created_types = {} api_types_definitions.each do |model, info| create_type(model, info[:fields], created_types) @@ -18,6 +18,10 @@ module GraphQL created_types end + def self.api_types_definitions + @api_types_definitions ||= parse_api_config_file(YAML.load_file(Rails.root.join("config/api.yml"))) + end + def self.type_kind(type) if SCALAR_TYPES[type] :scalar diff --git a/spec/lib/graph_ql/query_type_creator_spec.rb b/spec/lib/graph_ql/query_type_creator_spec.rb index 7a56666d0..36d671c5c 100644 --- a/spec/lib/graph_ql/query_type_creator_spec.rb +++ b/spec/lib/graph_ql/query_type_creator_spec.rb @@ -1,13 +1,15 @@ require "rails_helper" describe GraphQL::QueryTypeCreator do - let(:api_type_definitions) do - { - ProposalNotification => { fields: { title: :string }}, - Proposal => { fields: { id: :integer, title: :string }} - } + before do + allow(GraphQL::ApiTypesCreator).to receive(:api_types_definitions).and_return( + { + ProposalNotification => { fields: { title: :string }}, + Proposal => { fields: { id: :integer, title: :string }} + } + ) end - let(:api_types) { GraphQL::ApiTypesCreator.create(api_type_definitions) } + let(:api_types) { GraphQL::ApiTypesCreator.create } describe "::create" do let(:query_type) { GraphQL::QueryTypeCreator.create(api_types) } diff --git a/spec/lib/graphql_spec.rb b/spec/lib/graphql_spec.rb index 1a2efcce8..0675c0a66 100644 --- a/spec/lib/graphql_spec.rb +++ b/spec/lib/graphql_spec.rb @@ -1,6 +1,6 @@ require "rails_helper" -api_types = GraphQL::ApiTypesCreator.create(GraphqlController::API_TYPE_DEFINITIONS) +api_types = GraphQL::ApiTypesCreator.create query_type = GraphQL::QueryTypeCreator.create(api_types) ConsulSchema = GraphQL::Schema.define do query query_type