lib/rails/graphql/source.rb in rails-graphql-1.0.0.beta vs lib/rails/graphql/source.rb in rails-graphql-1.0.0.rc1

- old
+ new

@@ -15,17 +15,10 @@ extend Helpers::WithEvents extend Helpers::WithCallbacks include Helpers::Instantiable - ATTACH_FIELDS_STEP = -> do - if fields? - attach_fields!(type, fields) - attach_scoped_arguments_to(fields.values) - end - end - autoload :Base autoload :Builder autoload :ScopedArguments autoload :ActiveRecordSource @@ -37,11 +30,10 @@ def safe_field(name, *args, **xargs, &block) return if receiver.send(:skip_field?, name, on: type) self_object.safe_field(name, *args, **xargs, &block) end - # skip_field?(item.name, on: holder.kind) def respond_to_missing?(method_name, include_private = false) self_object.respond_to?(method_name, include_private) || receiver.respond_to?(method_name, include_private) end @@ -59,11 +51,11 @@ # List of hook names used while describing a new source. This basically # set the order of the execution of the hooks while validating the hooks # callbacks using the +on+ method class_attribute :hook_names, instance_accessor: false, - default: %i[start object input query mutation subscription].to_set + default: %i[start object input query mutation subscription].to_set.freeze # The list of hooks defined in order to describe a source inherited_collection :hooks, instance_reader: false, type: :hash_array # A list of fields to skip when performing shared methods @@ -111,10 +103,26 @@ def find_for(object) object = object.constantize if object.is_a?(String) base_sources.reverse_each.find { |source| object <= source.assigned_class } end + # Add a new description hook. You can use +throw :skip+ and skip + # parent hooks. If the class is already built, then execute the hook. + # Use the +unshift: true+ to add the hook at the beginning of the + # list, which will then be the last to run + def step(hook_name, unshift: false, &block) + raise ArgumentError, (+<<~MSG).squish unless hook_names.include?(hook_name.to_sym) + The #{hook_name.inspect} is not a valid hook method. + MSG + + if built?(hook_name) + hook_scope_for(hook_name).instance_exec(&block) + else + hooks[hook_name.to_sym].public_send(unshift ? :unshift : :push, block) + end + end + # Attach all defined schema fields into the schemas using the namespaces # configured for the source def attach_fields!(type = :all, from = self) schemas.each { |schema| schema.import_into(type, from) } end @@ -147,26 +155,10 @@ # Add fields to be skipped on the given +source+ as the segment def skip_from(source, *fields) segmented_skip_fields[source] += fields.flatten.compact.map(&:to_sym).to_set end - # Add a new description hook. You can use +throw :skip+ and skip - # parent hooks. If the class is already built, then execute the hook. - # Use the +unshift: true+ to add the hook at the beginning of the - # list, which will then be the last to run - def step(hook_name, unshift: false, &block) - raise ArgumentError, (+<<~MSG).squish unless hook_names.include?(hook_name.to_sym) - The #{hook_name.inspect} is not a valid hook method. - MSG - - if built?(hook_name) - hook_scope_for(hook_name).instance_exec(&block) - else - hooks[hook_name.to_sym].public_send(unshift ? :unshift : :push, block) - end - end - # Creates a hook that throws a done action, preventing any parent hooks def skip(*names) names.each do |hook_name| hook_name = hook_name.to_s.singularize.to_sym step(hook_name) { throw :skip } @@ -181,26 +173,25 @@ end # It's an alternative to +self.hook_names -= %i[*names]+ which # disables a specific hook def disable(*names) - self.hook_names -= names.flatten.map do |hook_name| + list = names.flatten.map do |hook_name| hook_name.to_s.singularize.to_sym end + + self.hook_names = (hook_names - list).freeze end # It's an alternative to +self.hook_names += %i[*names]+ which # enables additional hooks def enable(*names) - self.hook_names += names.flatten.map do |hook_name| + list = names.flatten.map do |hook_name| hook_name.to_s.singularize.to_sym end - end - # Return the module where the GraphQL types should be created at - def gql_module - name.start_with?('GraphQL::') ? module_parent : ::GraphQL + self.hook_names = (hook_names + list).freeze end # Add one or more fields to the list of fields that needs to be # ignored in all places. It converts strings to underscore def skip_fields!(*list) @@ -234,19 +225,14 @@ def inherited(subclass) subclass.abstract = false super if defined? super end - # Find all classes that inherits from source that are abstract, - # meaning that they are a base sources + # Constantize all the base sources that were defined in the settings def base_sources @@base_sources ||= GraphQL.config.sources.map(&:constantize).to_set end end - - step(:query, &ATTACH_FIELDS_STEP) - step(:mutation, &ATTACH_FIELDS_STEP) - step(:subscription, &ATTACH_FIELDS_STEP) end end end