func TestTypeSystem_DefinitionExample_ProhibitsNilTypeInUnions(t *testing.T) { ttype := graphql.NewUnion(graphql.UnionConfig{ Name: "BadUnion", Types: []*graphql.Object{nil}, }) expected := `BadUnion may only contain Object types, it cannot contain: <nil>.` if ttype.Error().Error() != expected { t.Fatalf(`expected %v , got: %v`, expected, ttype.Error()) } }
func TestTypeSystem_UnionTypesMustBeResolvable_AcceptsAUnionOfObjectTypesDefiningIsTypeOf(t *testing.T) { _, err := schemaWithFieldType(graphql.NewUnion(graphql.UnionConfig{ Name: "SomeUnion", Types: []*graphql.Object{objectWithIsTypeOf}, })) if err != nil { t.Fatalf("unexpected error: %v", err) } }
func TestTypeSystem_UnionTypesMustBeArray_RejectsAUnionTypeWithoutTypes(t *testing.T) { _, err := schemaWithFieldType(graphql.NewUnion(graphql.UnionConfig{ Name: "SomeUnion", ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object { 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(graphql.NewUnion(graphql.UnionConfig{ Name: "SomeUnion", Types: []*graphql.Object{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(graphql.NewUnion(graphql.UnionConfig{ Name: "SomeUnion", Types: []*graphql.Object{objectWithIsTypeOf}, ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object { return nil }, })) if err != nil { t.Fatalf("unexpected error: %v", err) } }
func TestTypeSystem_UnionTypesMustBeArray_AcceptsAUnionTypeWithArrayTypes(t *testing.T) { _, err := schemaWithFieldType(graphql.NewUnion(graphql.UnionConfig{ Name: "SomeUnion", ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object { return nil }, Types: []*graphql.Object{ someObjectType, }, })) if err != nil { t.Fatalf("unexpected error: %v", err) } }
func schemaWithUnionOfType(ttype *graphql.Object) (graphql.Schema, error) { badObjectType := graphql.NewUnion(graphql.UnionConfig{ Name: "BadUnion", ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object { return nil }, Types: []*graphql.Object{ttype}, }) return graphql.NewSchema(graphql.SchemaConfig{ Query: graphql.NewObject(graphql.ObjectConfig{ Name: "Query", Fields: graphql.Fields{ "f": &graphql.Field{ Type: badObjectType, }, }, }), }) }
func TestResolveTypeOnUnionYieldsUsefulError(t *testing.T) { humanType := graphql.NewObject(graphql.ObjectConfig{ Name: "Human", Fields: graphql.Fields{ "name": &graphql.Field{ Type: graphql.String, Resolve: func(p graphql.ResolveParams) interface{} { if human, ok := p.Source.(*testHuman); ok { return human.Name } return nil }, }, }, }) dogType := graphql.NewObject(graphql.ObjectConfig{ Name: "Dog", IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool { _, ok := value.(*testDog) return ok }, Fields: graphql.Fields{ "name": &graphql.Field{ Type: graphql.String, Resolve: func(p graphql.ResolveParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Name } return nil }, }, "woofs": &graphql.Field{ Type: graphql.Boolean, Resolve: func(p graphql.ResolveParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Woofs } return nil }, }, }, }) catType := graphql.NewObject(graphql.ObjectConfig{ Name: "Cat", IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool { _, ok := value.(*testCat) return ok }, Fields: graphql.Fields{ "name": &graphql.Field{ Type: graphql.String, Resolve: func(p graphql.ResolveParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Name } return nil }, }, "meows": &graphql.Field{ Type: graphql.Boolean, Resolve: func(p graphql.ResolveParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Meows } return nil }, }, }, }) petType := graphql.NewUnion(graphql.UnionConfig{ Name: "Pet", Types: []*graphql.Object{ dogType, catType, }, ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object { 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 := graphql.NewSchema(graphql.SchemaConfig{ Query: graphql.NewObject(graphql.ObjectConfig{ Name: "Query", Fields: graphql.Fields{ "pets": &graphql.Field{ Type: graphql.NewList(petType), Resolve: func(p graphql.ResolveParams) 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 := &graphql.Result{ Data: map[string]interface{}{ "pets": []interface{}{ map[string]interface{}{ "name": "Odie", "woofs": bool(true), }, map[string]interface{}{ "name": "Garfield", "meows": bool(false), }, nil, }, }, Errors: []gqlerrors.FormattedError{ gqlerrors.FormattedError{ Message: `Runtime Object type "Human" is not a possible type for "Pet".`, Locations: []location.SourceLocation{}, }, }, } result := graphql.Do(graphql.Params{ Schema: schema, RequestString: query, }) 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 := graphql.NewObject(graphql.ObjectConfig{ Name: "Dog", IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool { _, ok := value.(*testDog) return ok }, Fields: graphql.Fields{ "name": &graphql.Field{ Type: graphql.String, Resolve: func(p graphql.ResolveParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Name } return nil }, }, "woofs": &graphql.Field{ Type: graphql.Boolean, Resolve: func(p graphql.ResolveParams) interface{} { if dog, ok := p.Source.(*testDog); ok { return dog.Woofs } return nil }, }, }, }) catType := graphql.NewObject(graphql.ObjectConfig{ Name: "Cat", IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool { _, ok := value.(*testCat) return ok }, Fields: graphql.Fields{ "name": &graphql.Field{ Type: graphql.String, Resolve: func(p graphql.ResolveParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Name } return nil }, }, "meows": &graphql.Field{ Type: graphql.Boolean, Resolve: func(p graphql.ResolveParams) interface{} { if cat, ok := p.Source.(*testCat); ok { return cat.Meows } return nil }, }, }, }) // ie declare Pet has Dot and Cat object types petType := graphql.NewUnion(graphql.UnionConfig{ Name: "Pet", Types: []*graphql.Object{ dogType, catType, }, ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object { if _, ok := value.(*testCat); ok { return catType } if _, ok := value.(*testDog); ok { return dogType } return nil }, }) schema, err := graphql.NewSchema(graphql.SchemaConfig{ Query: graphql.NewObject(graphql.ObjectConfig{ Name: "Query", Fields: graphql.Fields{ "pets": &graphql.Field{ Type: graphql.NewList(petType), Resolve: func(p graphql.ResolveParams) 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 := &graphql.Result{ Data: map[string]interface{}{ "pets": []interface{}{ map[string]interface{}{ "name": "Odie", "woofs": bool(true), }, map[string]interface{}{ "name": "Garfield", "meows": bool(false), }, }, }, Errors: nil, } result := graphql.Do(graphql.Params{ Schema: schema, RequestString: query, }) 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 objectType = graphql.NewObject(graphql.ObjectConfig{ Name: "Object", IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool { return true }, }) var interfaceType = graphql.NewInterface(graphql.InterfaceConfig{ Name: "Interface", }) var unionType = graphql.NewUnion(graphql.UnionConfig{ Name: "Union", Types: []*graphql.Object{ objectType, }, }) var enumType = graphql.NewEnum(graphql.EnumConfig{ Name: "Enum", Values: graphql.EnumValueConfigMap{ "foo": &graphql.EnumValueConfig{}, }, }) var inputObjectType = graphql.NewInputObject(graphql.InputObjectConfig{ Name: "InputObject", }) func init() { blogAuthor.AddFieldConfig("recentArticle", &graphql.Field{ Type: blogArticle,
var objectWithIsTypeOf = graphql.NewObject(graphql.ObjectConfig{ Name: "ObjectWithIsTypeOf", IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool { return true }, Fields: graphql.Fields{ "f": &graphql.Field{ Type: graphql.String, }, }, }) var someUnionType = graphql.NewUnion(graphql.UnionConfig{ Name: "SomeUnion", ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object { return nil }, Types: []*graphql.Object{ someObjectType, }, }) var someInterfaceType = graphql.NewInterface(graphql.InterfaceConfig{ Name: "SomeInterface", ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object { return nil }, Fields: graphql.Fields{ "f": &graphql.Field{ Type: graphql.String, }, }, })
Type: graphql.Boolean, }, }, IsTypeOf: func(value interface{}, info graphql.ResolveInfo) bool { _, ok := value.(*testCat2) return ok }, }) var petType = graphql.NewUnion(graphql.UnionConfig{ Name: "Pet", Types: []*graphql.Object{ dogType, catType, }, ResolveType: func(value interface{}, info graphql.ResolveInfo) *graphql.Object { if _, ok := value.(*testCat2); ok { return catType } if _, ok := value.(*testDog2); ok { return dogType } return nil }, }) var personType = graphql.NewObject(graphql.ObjectConfig{ Name: "Person", Interfaces: []*graphql.Interface{ namedType, }, Fields: graphql.Fields{ "name": &graphql.Field{ Type: graphql.String,