Sha256: 9c210203395f1c331919173b0f8fb10b6fb5753b52ef3e666048e5276d3024a1

Contents?: true

Size: 1.53 KB

Versions: 15

Compression:

Stored size: 1.53 KB

Contents

require 'spec_helper'

describe GraphQL::StaticValidation::VariablesAreUsedAndDefined do
  let(:document) { GraphQL.parse('
    query getCheese(
      $usedVar: Int,
      $usedInnerVar: String,
      $usedInlineFragmentVar: Boolean,
      $usedFragmentVar: Int,
      $notUsedVar: Float,
    ) {
      cheese(id: $usedVar) {
        source(str: $usedInnerVar)
        whatever(undefined: $undefinedVar)
        ... on Cheese {
          something(bool: $usedInlineFragmentVar)
        }
        ... outerCheeseFields
      }
    }

    fragment outerCheeseFields on Cheese {
      ... innerCheeseFields
    }

    fragment innerCheeseFields on Cheese {
      source(notDefined: $undefinedFragmentVar)
      someField(someArg: $usedFragmentVar)
    }
  ')}

  let(:validator) { GraphQL::StaticValidation::Validator.new(schema: DummySchema, rules: [GraphQL::StaticValidation::VariablesAreUsedAndDefined]) }
  let(:errors) { validator.validate(document) }

  it "finds variables which are used-but-not-defined or defined-but-not-used" do
    expected = [
      {
        "message"=>"Variable $notUsedVar is declared by getCheese but not used",
        "locations"=>[{"line"=>2, "column"=>5}]
      },
      {
        "message"=>"Variable $undefinedVar is used by getCheese but not declared",
        "locations"=>[{"line"=>11, "column"=>30}]
      },
      {
        "message"=>"Variable $undefinedFragmentVar is used by innerCheeseFields but not declared",
        "locations"=>[{"line"=>24, "column"=>27}]
      },
    ]
    assert_equal(expected, errors)
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
graphql-0.11.1 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.11.0 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.9 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.8 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.7 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.6 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.5 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.4 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.3 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.2 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.1 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.10.0 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.9.5 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.9.4 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb
graphql-0.9.3 spec/graphql/static_validation/rules/variables_are_used_and_defined_spec.rb