Sha256: 3985d0d337651b9383c3d165280fd23ae75bc57a479aa1cf657eb87379455cd4

Contents?: true

Size: 1.14 KB

Versions: 112

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true
module GraphQL
  module Language
    module DefinitionSlice
      extend self

      def slice(document, name)
        definitions = {}
        document.definitions.each { |d| definitions[d.name] = d }
        names = Set.new
        DependencyVisitor.find_definition_dependencies(definitions, name, names)
        definitions = document.definitions.select { |d| names.include?(d.name) }
        Nodes::Document.new(definitions: definitions)
      end

      private

      class DependencyVisitor < GraphQL::Language::Visitor
        def initialize(doc, definitions, names)
          @names = names
          @definitions = definitions
          super(doc)
        end

        def on_fragment_spread(node, parent)
          if fragment = @definitions[node.name]
            self.class.find_definition_dependencies(@definitions, fragment.name, @names)
          end
          super
        end

        def self.find_definition_dependencies(definitions, name, names)
          names.add(name)
          visitor = self.new(definitions[name], definitions, names)
          visitor.visit
          nil
        end
      end
    end
  end
end

Version data entries

112 entries across 112 versions & 2 rubygems

Version Path
graphql-2.0.17 lib/graphql/language/definition_slice.rb
graphql-1.13.19 lib/graphql/language/definition_slice.rb
graphql-1.13.18 lib/graphql/language/definition_slice.rb
graphql-2.0.16 lib/graphql/language/definition_slice.rb
graphql-1.13.17 lib/graphql/language/definition_slice.rb
graphql-2.0.15 lib/graphql/language/definition_slice.rb
graphql-2.0.14 lib/graphql/language/definition_slice.rb
graphql-1.13.16 lib/graphql/language/definition_slice.rb
graphql-2.0.13 lib/graphql/language/definition_slice.rb
graphql-2.0.12 lib/graphql/language/definition_slice.rb
graphql-1.13.15 lib/graphql/language/definition_slice.rb
graphql-2.0.11 lib/graphql/language/definition_slice.rb
graphql-1.13.14 lib/graphql/language/definition_slice.rb
graphql-1.13.13 lib/graphql/language/definition_slice.rb
graphql-2.0.9 lib/graphql/language/definition_slice.rb
graphql-2.0.8 lib/graphql/language/definition_slice.rb
graphql-2.0.7 lib/graphql/language/definition_slice.rb
graphql_cody-1.13.0 lib/graphql/language/definition_slice.rb
graphql-1.13.12 lib/graphql/language/definition_slice.rb
graphql-2.0.6 lib/graphql/language/definition_slice.rb