spec/graphql/schema/warden_spec.rb in graphql-1.4.0 vs spec/graphql/schema/warden_spec.rb in graphql-1.4.1

- old
+ new

@@ -94,12 +94,21 @@ description "Find an emic unit by its name" argument :name, types.String.to_non_null_type end end + MutationType = GraphQL::ObjectType.define do + name "Mutation" + field :add_phoneme, PhonemeType do + argument :symbol, types.String + end + end + Schema = GraphQL::Schema.define do query QueryType + mutation MutationType + subscription MutationType resolve_type -> (obj, ctx) { PhonemeType } end module Data UVULAR_TRILL = OpenStruct.new({name: "Uvular Trill", symbol: "ʀ", manner: "TRILL"}) @@ -149,10 +158,56 @@ def error_messages(query_result) query_result["errors"].map { |err| err["message"] } end + describe "hiding root types" do + let(:mask) { ->(m, ctx) { m == MaskHelpers::MutationType } } + + it "acts as if the root doesn't exist" do + query_string = %|mutation { add_phoneme(symbol: "ϕ") { name } }| + res = MaskHelpers.query_with_mask(query_string, mask) + assert MaskHelpers::Schema.mutation # it _does_ exist + assert_equal 1, res["errors"].length + assert_equal "Schema is not configured for mutations", res["errors"][0]["message"] + + query_string = %|subscription { add_phoneme(symbol: "ϕ") { name } }| + res = MaskHelpers.query_with_mask(query_string, mask) + assert MaskHelpers::Schema.subscription # it _does_ exist + assert_equal 1, res["errors"].length + assert_equal "Schema is not configured for subscriptions", res["errors"][0]["message"] + end + + it "doesn't show in introspection" do + query_string = <<-GRAPHQL + { + __schema { + queryType { + name + } + mutationType { + name + } + subscriptionType { + name + } + types { + name + } + } + } + GRAPHQL + res = MaskHelpers.query_with_mask(query_string, mask) + assert_equal "Query", res["data"]["__schema"]["queryType"]["name"] + assert_equal nil, res["data"]["__schema"]["mutationType"] + assert_equal nil, res["data"]["__schema"]["subscriptionType"] + type_names = res["data"]["__schema"]["types"].map { |t| t["name"] } + refute type_names.include?("Mutation") + refute type_names.include?("Subscription") + end + end + describe "hiding fields" do let(:mask) { -> (member, ctx) { member.metadata[:hidden_field] || member.metadata[:hidden_type] } } @@ -508,8 +563,32 @@ } | assert_raises(GraphQL::EnumType::UnresolvedValueError) { MaskHelpers.query_with_mask(query_string, mask) } + end + end + + describe "default_mask" do + let(:default_mask) { + -> (member, ctx) { member.metadata[:hidden_enum_value] } + } + let(:schema) { + MaskHelpers::Schema.redefine(default_mask: default_mask) + } + let(:query_str) { <<-GRAPHQL + { + enum: __type(name: "Manner") { enumValues { name } } + input: __type(name: "WithinInput") { name } + } + GRAPHQL + } + + it "is additive with query filters" do + query_except = -> (member, ctx) { member.metadata[:hidden_input_object_type] } + res = schema.execute(query_str, except: query_except) + assert_equal nil, res["data"]["input"] + enum_values = res["data"]["enum"]["enumValues"].map { |v| v["name"] } + refute_includes enum_values, "TRILL" end end end