Sha256: 7488596ad8ffa4c0d300272584d389a3e289e25f1cf67216de7f5abe26ee0f59

Contents?: true

Size: 844 Bytes

Versions: 10

Compression:

Stored size: 844 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

10 entries across 10 versions & 1 rubygems

Version Path
graphql-1.2.6 lib/graphql/language/definition_slice.rb
graphql-1.2.5 lib/graphql/language/definition_slice.rb
graphql-1.2.4 lib/graphql/language/definition_slice.rb
graphql-1.2.3 lib/graphql/language/definition_slice.rb
graphql-1.2.2 lib/graphql/language/definition_slice.rb
graphql-1.2.1 lib/graphql/language/definition_slice.rb
graphql-1.2.0 lib/graphql/language/definition_slice.rb
graphql-1.1.0 lib/graphql/language/definition_slice.rb
graphql-1.0.0 lib/graphql/language/definition_slice.rb
graphql-0.19.4 lib/graphql/language/definition_slice.rb