Sha256: 67f17909287af17347cfc3d6a20cd201654f872a5cf2c4d92a7644260b270655

Contents?: true

Size: 1.68 KB

Versions: 59

Compression:

Stored size: 1.68 KB

Contents

# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Language::Visitor do
  let(:document) { GraphQL.parse("
    query cheese {
      cheese(id: 1) {
        flavor,
        source,
        producers(first: 3) {
          name
        }
        ... cheeseFields
      }
    }

    fragment cheeseFields on Cheese { flavor }
    ")}
  let(:counts) { {fields_entered: 0, arguments_entered: 0, arguments_left: 0, argument_names: []} }

  let(:visitor) do
    v = GraphQL::Language::Visitor.new(document)
    v[GraphQL::Language::Nodes::Field] << ->(node, parent) { counts[:fields_entered] += 1 }
    # two ways to set up enter hooks:
    v[GraphQL::Language::Nodes::Argument] <<       ->(node, parent) { counts[:argument_names] << node.name }
    v[GraphQL::Language::Nodes::Argument].enter << ->(node, parent) { counts[:arguments_entered] += 1}
    v[GraphQL::Language::Nodes::Argument].leave << ->(node, parent) { counts[:arguments_left] += 1 }

    v[GraphQL::Language::Nodes::Document].leave << ->(node, parent) { counts[:finished] = true }
    v
  end

  it "calls hooks during a depth-first tree traversal" do
    assert_equal(2, visitor[GraphQL::Language::Nodes::Argument].enter.length)
    visitor.visit
    assert_equal(6, counts[:fields_entered])
    assert_equal(2, counts[:arguments_entered])
    assert_equal(2, counts[:arguments_left])
    assert_equal(["id", "first"], counts[:argument_names])
    assert(counts[:finished])
  end

  describe "Visitor::SKIP" do
    it "skips the rest of the node" do
      visitor[GraphQL::Language::Nodes::Document] << ->(node, parent) { GraphQL::Language::Visitor::SKIP }
      visitor.visit
      assert_equal(0, counts[:fields_entered])
    end
  end
end

Version data entries

59 entries across 59 versions & 1 rubygems

Version Path
graphql-1.8.2 spec/graphql/language/visitor_spec.rb
graphql-1.8.1 spec/graphql/language/visitor_spec.rb
graphql-1.8.0 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre11 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre10 spec/graphql/language/visitor_spec.rb
graphql-1.7.14 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre9 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre8 spec/graphql/language/visitor_spec.rb
graphql-1.7.13 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre7 spec/graphql/language/visitor_spec.rb
graphql-1.7.12 spec/graphql/language/visitor_spec.rb
graphql-1.7.11 spec/graphql/language/visitor_spec.rb
graphql-1.7.10 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre6 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre5 spec/graphql/language/visitor_spec.rb
graphql-1.7.9 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre4 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre3 spec/graphql/language/visitor_spec.rb
graphql-1.7.8 spec/graphql/language/visitor_spec.rb
graphql-1.8.0.pre2 spec/graphql/language/visitor_spec.rb