Sha256: f199d65bf04fed3c5ea4f5e8e8f83b560d7ba944959755b32cd5d218bf9424fb

Contents?: true

Size: 1.67 KB

Versions: 12

Compression:

Stored size: 1.67 KB

Contents

module GraphitiGql
  class Schema
    class Query
      def initialize(resources, existing_query: nil)
        @resources = resources
        @query_class = Class.new(existing_query || Schema.base_object)
        @query_class.graphql_name "Query"
      end

      def build
        @resources.each { |resource| ResourceType.new(resource).build }
        define_entrypoints
        add_relationships
        @query_class
      end

      private

      def registry
        Registry.instance
      end

      def define_entrypoints
        registry.resource_types.each do |registered|
          if GraphitiGql.entrypoint?(registered[:resource])
            Fields::Index.new(registered).apply(@query_class)
            Fields::Show.new(registered).apply(@query_class)
          end
        end
      end

      def add_relationships
        each_relationship do |type, sideload_type, sideload|
          if [:has_many, :many_to_many, :has_one].include?(sideload.type)
            Fields::ToMany.new(sideload, sideload_type).apply(type)
          else
            Fields::ToOne.new(sideload, sideload_type).apply(type)
          end
        end
      end

      def each_relationship
        registry.resource_types.each do |registered|
          registered[:resource].sideloads.each do |name, sl|
            next unless sl.readable?

            registered_sl = if sl.type == :polymorphic_belongs_to
              PolymorphicBelongsToInterface
                .new(registered[:resource], sl)
                .build
            else
              registry.get(sl.resource.class)
            end

            yield registered[:type], registered_sl[:type], sl
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
graphiti_gql-0.2.15 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.14 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.12 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.11 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.10 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.9 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.8 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.7 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.6 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.5 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.4 lib/graphiti_gql/schema/query.rb
graphiti_gql-0.2.3 lib/graphiti_gql/schema/query.rb