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.31 lib/graphql/language/definition_slice.rb
graphql-1.13.23 lib/graphql/language/definition_slice.rb
graphql-2.0.29 lib/graphql/language/definition_slice.rb
graphql-1.13.22 lib/graphql/language/definition_slice.rb
graphql-2.0.28 lib/graphql/language/definition_slice.rb
graphql-1.13.21 lib/graphql/language/definition_slice.rb
graphql-1.13.20 lib/graphql/language/definition_slice.rb
graphql-2.1.0 lib/graphql/language/definition_slice.rb
graphql-2.0.27 lib/graphql/language/definition_slice.rb
graphql-2.0.26 lib/graphql/language/definition_slice.rb
graphql-2.0.25 lib/graphql/language/definition_slice.rb
graphql-2.0.24 lib/graphql/language/definition_slice.rb
graphql-2.0.23 lib/graphql/language/definition_slice.rb
graphql-2.0.22 lib/graphql/language/definition_slice.rb
graphql-2.0.21 lib/graphql/language/definition_slice.rb
graphql-2.0.20 lib/graphql/language/definition_slice.rb
graphql-2.0.17.2 lib/graphql/language/definition_slice.rb
graphql-2.0.17.1 lib/graphql/language/definition_slice.rb
graphql-2.0.19 lib/graphql/language/definition_slice.rb
graphql-2.0.18 lib/graphql/language/definition_slice.rb