Sha256: 4c7c46548cb0a520e4aea975b786394fa3db5733861b7aced52295782cddf354

Contents?: true

Size: 1.53 KB

Versions: 12

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true
require "spec_helper"

describe GraphQL::Schema::Union do
  let(:union) { Jazz::PerformingAct }
  describe "type info" do
    it "has some" do
      assert_equal 2, union.possible_types.size
    end
  end

  describe ".to_graphql" do
    it "creates a UnionType" do
      union = Class.new(GraphQL::Schema::Union) do
        possible_types Jazz::Musician, Jazz::Ensemble

        def self.name
          "MyUnion"
        end
      end
      union_type = union.to_graphql
      assert_equal "MyUnion", union_type.name
      assert_equal [Jazz::Musician.to_graphql, Jazz::Ensemble.to_graphql], union_type.possible_types
      assert_nil union_type.resolve_type_proc
    end

    it "can specify a resolve_type method" do
      union = Class.new(GraphQL::Schema::Union) do
        def self.resolve_type(_object, _context)
          "MyType"
        end

        def self.name
          "MyUnion"
        end
      end
      union_type = union.to_graphql
      assert_equal "MyType", union_type.resolve_type_proc.call(nil, nil)
    end
  end

  describe "in queries" do
    it "works" do
      query_str = <<-GRAPHQL
      {
        nowPlaying {
          ... on Musician {
            name
            instrument {
              family
            }
          }
          ... on Ensemble {
            name
          }
        }
      }
      GRAPHQL

      res = Jazz::Schema.execute(query_str)
      expected_data = { "name" => "Bela Fleck and the Flecktones" }
      assert_equal expected_data, res["data"]["nowPlaying"]
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
graphql-1.8.7 spec/graphql/schema/union_spec.rb
graphql-1.8.6 spec/graphql/schema/union_spec.rb
graphql-1.8.5 spec/graphql/schema/union_spec.rb
graphql-1.8.4 spec/graphql/schema/union_spec.rb
graphql-1.8.3 spec/graphql/schema/union_spec.rb
graphql-1.8.2 spec/graphql/schema/union_spec.rb
graphql-1.8.1 spec/graphql/schema/union_spec.rb
graphql-1.8.0 spec/graphql/schema/union_spec.rb
graphql-1.8.0.pre11 spec/graphql/schema/union_spec.rb
graphql-1.8.0.pre10 spec/graphql/schema/union_spec.rb
graphql-1.8.0.pre9 spec/graphql/schema/union_spec.rb
graphql-1.8.0.pre8 spec/graphql/schema/union_spec.rb