Sha256: 27db66f823e2c817b005180f7cf0a8169376c776d247902cd6768023559dfe9b

Contents?: true

Size: 1.66 KB

Versions: 73

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true
require "spec_helper"

describe GraphQL::NonNullType do
  describe "when a non-null field returns null" do
    it "nulls out the parent selection" do
      query_string = %|{ cow { name cantBeNullButIs } }|
      result = Dummy::Schema.execute(query_string)
      assert_equal({"cow" => nil }, result["data"])
      assert_equal([{"message"=>"Cannot return null for non-nullable field Cow.cantBeNullButIs"}], result["errors"])
    end

    it "propagates the null up to the next nullable field" do
      query_string = %|
      {
        nn1: deepNonNull {
          nni1: nonNullInt(returning: 1)
          nn2: deepNonNull {
            nni2: nonNullInt(returning: 2)
            nn3: deepNonNull {
              nni3: nonNullInt
            }
          }
        }
      }
      |
      result = Dummy::Schema.execute(query_string)
      assert_equal(nil, result["data"])
      assert_equal([{"message"=>"Cannot return null for non-nullable field DeepNonNull.nonNullInt"}], result["errors"])
    end

    describe "when type_error is configured to raise an error" do
      it "crashes query execution" do
        raise_schema = Dummy::Schema.redefine {
          type_error ->(type_err, ctx) {
            raise type_err
          }
        }
        query_string = %|{ cow { name cantBeNullButIs } }|
        err = assert_raises(GraphQL::InvalidNullError) { raise_schema.execute(query_string) }
        assert_equal("Cannot return null for non-nullable field Cow.cantBeNullButIs", err.message)
        assert_equal("Cow", err.parent_type.name)
        assert_equal("cantBeNullButIs", err.field.name)
        assert_equal(nil, err.value)
      end
    end
  end
end

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
graphql-1.8.18 spec/graphql/non_null_type_spec.rb
graphql-1.8.17 spec/graphql/non_null_type_spec.rb
graphql-1.8.16 spec/graphql/non_null_type_spec.rb
graphql-1.8.15 spec/graphql/non_null_type_spec.rb
graphql-1.8.14 spec/graphql/non_null_type_spec.rb
graphql-1.8.13 spec/graphql/non_null_type_spec.rb
graphql-1.8.12 spec/graphql/non_null_type_spec.rb
graphql-1.8.11 spec/graphql/non_null_type_spec.rb
graphql-1.8.10 spec/graphql/non_null_type_spec.rb
graphql-1.8.9 spec/graphql/non_null_type_spec.rb
graphql-1.8.8 spec/graphql/non_null_type_spec.rb
graphql-1.8.7 spec/graphql/non_null_type_spec.rb
graphql-1.8.6 spec/graphql/non_null_type_spec.rb
graphql-1.8.5 spec/graphql/non_null_type_spec.rb
graphql-1.8.4 spec/graphql/non_null_type_spec.rb
graphql-1.8.3 spec/graphql/non_null_type_spec.rb
graphql-1.8.2 spec/graphql/non_null_type_spec.rb
graphql-1.8.1 spec/graphql/non_null_type_spec.rb
graphql-1.8.0 spec/graphql/non_null_type_spec.rb
graphql-1.8.0.pre11 spec/graphql/non_null_type_spec.rb