func TestTypeSystem_DefinitionExample_ProhibitsNilTypeInUnions(t *testing.T) {
	ttype := types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
		Name:  "BadUnion",
		Types: []*types.GraphQLObjectType{nil},
	})
	expected := `BadUnion may only contain Object types, it cannot contain: <nil>.`
	if ttype.GetError().Error() != expected {
		t.Fatalf(`expected %v , got: %v`, expected, ttype.GetError())
	}
}
func TestTypeSystem_UnionTypesMustBeResolvable_AcceptsAUnionOfObjectTypesDefiningIsTypeOf(t *testing.T) {

	_, err := schemaWithFieldType(types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
		Name:  "SomeUnion",
		Types: []*types.GraphQLObjectType{objectWithIsTypeOf},
	}))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}
func TestTypeSystem_UnionTypesMustBeArray_RejectsAUnionTypeWithoutTypes(t *testing.T) {
	_, err := schemaWithFieldType(types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
		Name: "SomeUnion",
		ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
			return nil
		},
	}))
	expectedError := "Must provide Array of types for Union SomeUnion."
	if err == nil || err.Error() != expectedError {
		t.Fatalf("Expected error: %v, got %v", expectedError, err)
	}
}
func TestTypeSystem_UnionTypesMustBeResolvable_RejectsAUnionTypeNotDefiningResolveTypeOfObjectTypesNotDefiningIsTypeOf(t *testing.T) {

	_, err := schemaWithFieldType(types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
		Name:  "SomeUnion",
		Types: []*types.GraphQLObjectType{someObjectType},
	}))
	expectedError := `Union Type SomeUnion does not provide a "resolveType" function and ` +
		`possible Type SomeObject does not provide a "isTypeOf" function. ` +
		`There is no way to resolve this possible type during execution.`
	if err == nil || err.Error() != expectedError {
		t.Fatalf("Expected error: %v, got %v", expectedError, err)
	}
}
func TestTypeSystem_UnionTypesMustBeResolvable_AcceptsAUnionTypeDefiningResolveTypeOfObjectTypesDefiningIsTypeOf(t *testing.T) {

	_, err := schemaWithFieldType(types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
		Name:  "SomeUnion",
		Types: []*types.GraphQLObjectType{objectWithIsTypeOf},
		ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
			return nil
		},
	}))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}
func TestTypeSystem_UnionTypesMustBeArray_AcceptsAUnionTypeWithArrayTypes(t *testing.T) {
	_, err := schemaWithFieldType(types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
		Name: "SomeUnion",
		ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
			return nil
		},
		Types: []*types.GraphQLObjectType{
			someObjectType,
		},
	}))
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}
func schemaWithUnionOfType(ttype *types.GraphQLObjectType) (types.GraphQLSchema, error) {

	badObjectType := types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
		Name: "BadUnion",
		ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
			return nil
		},
		Types: []*types.GraphQLObjectType{ttype},
	})
	return types.NewGraphQLSchema(types.GraphQLSchemaConfig{
		Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
			Name: "Query",
			Fields: types.GraphQLFieldConfigMap{
				"f": &types.GraphQLFieldConfig{
					Type: badObjectType,
				},
			},
		}),
	})
}
		},
	},
})

var objectType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
	Name: "Object",
	IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
		return true
	},
})
var interfaceType = types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
	Name: "Interface",
})
var unionType = types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
	Name: "Union",
	Types: []*types.GraphQLObjectType{
		objectType,
	},
})
var enumType = types.NewGraphQLEnumType(types.GraphQLEnumTypeConfig{
	Name: "Enum",
	Values: types.GraphQLEnumValueConfigMap{
		"foo": &types.GraphQLEnumValueConfig{},
	},
})
var inputObjectType = types.NewGraphQLInputObjectType(types.InputObjectConfig{
	Name: "InputObject",
})

func init() {
	blogAuthor.AddFieldConfig("recentArticle", &types.GraphQLFieldConfig{
		Type: blogArticle,
			Type: types.GraphQLBoolean,
		},
	},
	IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
		_, ok := value.(*testCat2)
		return ok
	},
})
var petType = types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
	Name: "Pet",
	Types: []*types.GraphQLObjectType{
		dogType, catType,
	},
	ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
		if _, ok := value.(*testCat2); ok {
			return catType
		}
		if _, ok := value.(*testDog2); ok {
			return dogType
		}
		return nil
	},
})
var personType = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
	Name: "Person",
	Interfaces: []*types.GraphQLInterfaceType{
		namedType,
	},
	Fields: types.GraphQLFieldConfigMap{
		"name": &types.GraphQLFieldConfig{
			Type: types.GraphQLString,
func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) {

	humanType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
		Name: "Human",
		Fields: types.GraphQLFieldConfigMap{
			"name": &types.GraphQLFieldConfig{
				Type: types.GraphQLString,
				Resolve: func(p types.GQLFRParams) interface{} {
					if human, ok := p.Source.(*testHuman); ok {
						return human.Name
					}
					return nil
				},
			},
		},
	})
	dogType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
		Name: "Dog",
		IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
			_, ok := value.(*testDog)
			return ok
		},
		Fields: types.GraphQLFieldConfigMap{
			"name": &types.GraphQLFieldConfig{
				Type: types.GraphQLString,
				Resolve: func(p types.GQLFRParams) interface{} {
					if dog, ok := p.Source.(*testDog); ok {
						return dog.Name
					}
					return nil
				},
			},
			"woofs": &types.GraphQLFieldConfig{
				Type: types.GraphQLBoolean,
				Resolve: func(p types.GQLFRParams) interface{} {
					if dog, ok := p.Source.(*testDog); ok {
						return dog.Woofs
					}
					return nil
				},
			},
		},
	})
	catType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
		Name: "Cat",
		IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
			_, ok := value.(*testCat)
			return ok
		},
		Fields: types.GraphQLFieldConfigMap{
			"name": &types.GraphQLFieldConfig{
				Type: types.GraphQLString,
				Resolve: func(p types.GQLFRParams) interface{} {
					if cat, ok := p.Source.(*testCat); ok {
						return cat.Name
					}
					return nil
				},
			},
			"meows": &types.GraphQLFieldConfig{
				Type: types.GraphQLBoolean,
				Resolve: func(p types.GQLFRParams) interface{} {
					if cat, ok := p.Source.(*testCat); ok {
						return cat.Meows
					}
					return nil
				},
			},
		},
	})
	petType := types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
		Name: "Pet",
		Types: []*types.GraphQLObjectType{
			dogType, catType,
		},
		ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
			if _, ok := value.(*testCat); ok {
				return catType
			}
			if _, ok := value.(*testDog); ok {
				return dogType
			}
			if _, ok := value.(*testHuman); ok {
				return humanType
			}
			return nil
		},
	})
	schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
		Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
			Name: "Query",
			Fields: types.GraphQLFieldConfigMap{
				"pets": &types.GraphQLFieldConfig{
					Type: types.NewGraphQLList(petType),
					Resolve: func(p types.GQLFRParams) interface{} {
						return []interface{}{
							&testDog{"Odie", true},
							&testCat{"Garfield", false},
							&testHuman{"Jon"},
						}
					},
				},
			},
		}),
	})
	if err != nil {
		t.Fatalf("Error in schema %v", err.Error())
	}

	query := `{
      pets {
        name
        ... on Dog {
          woofs
        }
        ... on Cat {
          meows
        }
      }
    }`

	expected := &types.GraphQLResult{
		Data: map[string]interface{}{
			"pets": []interface{}{
				map[string]interface{}{
					"name":  "Odie",
					"woofs": bool(true),
				},
				map[string]interface{}{
					"name":  "Garfield",
					"meows": bool(false),
				},
				nil,
			},
		},
		Errors: []graphqlerrors.GraphQLFormattedError{
			graphqlerrors.GraphQLFormattedError{
				Message:   `Runtime Object type "Human" is not a possible type for "Pet".`,
				Locations: []location.SourceLocation{},
			},
		},
	}

	resultChannel := make(chan *types.GraphQLResult)

	go gql.Graphql(gql.GraphqlParams{
		Schema:        schema,
		RequestString: query,
	}, resultChannel)
	result := <-resultChannel
	if len(result.Errors) == 0 {
		t.Fatalf("wrong result, expected errors: %v, got: %v", len(expected.Errors), len(result.Errors))
	}
	if !reflect.DeepEqual(expected, result) {
		t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
	}
}
func TestIsTypeOfUsedToResolveRuntimeTypeForUnion(t *testing.T) {

	dogType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
		Name: "Dog",
		IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
			_, ok := value.(*testDog)
			return ok
		},
		Fields: types.GraphQLFieldConfigMap{
			"name": &types.GraphQLFieldConfig{
				Type: types.GraphQLString,
				Resolve: func(p types.GQLFRParams) interface{} {
					if dog, ok := p.Source.(*testDog); ok {
						return dog.Name
					}
					return nil
				},
			},
			"woofs": &types.GraphQLFieldConfig{
				Type: types.GraphQLBoolean,
				Resolve: func(p types.GQLFRParams) interface{} {
					if dog, ok := p.Source.(*testDog); ok {
						return dog.Woofs
					}
					return nil
				},
			},
		},
	})
	catType := types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
		Name: "Cat",
		IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
			_, ok := value.(*testCat)
			return ok
		},
		Fields: types.GraphQLFieldConfigMap{
			"name": &types.GraphQLFieldConfig{
				Type: types.GraphQLString,
				Resolve: func(p types.GQLFRParams) interface{} {
					if cat, ok := p.Source.(*testCat); ok {
						return cat.Name
					}
					return nil
				},
			},
			"meows": &types.GraphQLFieldConfig{
				Type: types.GraphQLBoolean,
				Resolve: func(p types.GQLFRParams) interface{} {
					if cat, ok := p.Source.(*testCat); ok {
						return cat.Meows
					}
					return nil
				},
			},
		},
	})
	// ie declare Pet has Dot and Cat object types
	petType := types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
		Name: "Pet",
		Types: []*types.GraphQLObjectType{
			dogType, catType,
		},
		ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
			if _, ok := value.(*testCat); ok {
				return catType
			}
			if _, ok := value.(*testDog); ok {
				return dogType
			}
			return nil
		},
	})
	schema, err := types.NewGraphQLSchema(types.GraphQLSchemaConfig{
		Query: types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
			Name: "Query",
			Fields: types.GraphQLFieldConfigMap{
				"pets": &types.GraphQLFieldConfig{
					Type: types.NewGraphQLList(petType),
					Resolve: func(p types.GQLFRParams) interface{} {
						return []interface{}{
							&testDog{"Odie", true},
							&testCat{"Garfield", false},
						}
					},
				},
			},
		}),
	})
	if err != nil {
		t.Fatalf("Error in schema %v", err.Error())
	}

	query := `{
      pets {
        name
        ... on Dog {
          woofs
        }
        ... on Cat {
          meows
        }
      }
    }`

	expected := &types.GraphQLResult{
		Data: map[string]interface{}{
			"pets": []interface{}{
				map[string]interface{}{
					"name":  "Odie",
					"woofs": bool(true),
				},
				map[string]interface{}{
					"name":  "Garfield",
					"meows": bool(false),
				},
			},
		},
		Errors: nil,
	}

	resultChannel := make(chan *types.GraphQLResult)

	go gql.Graphql(gql.GraphqlParams{
		Schema:        schema,
		RequestString: query,
	}, resultChannel)
	result := <-resultChannel

	if len(result.Errors) != 0 {
		t.Fatalf("wrong result, unexpected errors: %v", result.Errors)
	}
	if !reflect.DeepEqual(expected, result) {
		t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
	}
}
var objectWithIsTypeOf = types.NewGraphQLObjectType(types.GraphQLObjectTypeConfig{
	Name: "ObjectWithIsTypeOf",
	IsTypeOf: func(value interface{}, info types.GraphQLResolveInfo) bool {
		return true
	},
	Fields: types.GraphQLFieldConfigMap{
		"f": &types.GraphQLFieldConfig{
			Type: types.GraphQLString,
		},
	},
})
var someUnionType = types.NewGraphQLUnionType(types.GraphQLUnionTypeConfig{
	Name: "SomeUnion",
	ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
		return nil
	},
	Types: []*types.GraphQLObjectType{
		someObjectType,
	},
})
var someInterfaceType = types.NewGraphQLInterfaceType(types.GraphQLInterfaceTypeConfig{
	Name: "SomeInterface",
	ResolveType: func(value interface{}, info types.GraphQLResolveInfo) *types.GraphQLObjectType {
		return nil
	},
	Fields: types.GraphQLFieldConfigMap{
		"f": &types.GraphQLFieldConfig{
			Type: types.GraphQLString,
		},
	},
})