Sha256: ee2c536e2d0c204886ea64de42a762140dcfa451587e25413dcc19a8b5c8f6d5

Contents?: true

Size: 845 Bytes

Versions: 9

Compression:

Stored size: 845 Bytes

Contents

module GraphQL
  module Language
    module DefinitionSlice
      extend self

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

      private

      def find_definition_dependencies(definitions, name)
        names = Set.new([name])
        visitor = Visitor.new(definitions[name])
        visitor[Nodes::FragmentSpread] << -> (node, parent) {
          if fragment = definitions[node.name]
            names.merge(find_definition_dependencies(definitions, fragment.name))
          end
        }
        visitor.visit
        names
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
graphql-0.19.3 lib/graphql/language/definition_slice.rb
graphql-0.19.2 lib/graphql/language/definition_slice.rb
graphql-0.19.1 lib/graphql/language/definition_slice.rb
graphql-0.19.0 lib/graphql/language/definition_slice.rb
graphql-0.18.15 lib/graphql/language/definition_slice.rb
graphql-0.18.14 lib/graphql/language/definition_slice.rb
graphql-0.18.13 lib/graphql/language/definition_slice.rb
graphql-0.18.12 lib/graphql/language/definition_slice.rb
graphql-0.18.11 lib/graphql/language/definition_slice.rb