1 Star 0 Fork 0

麦冬果果/graphql-go

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
union_interface_test.go 14.22 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
package graphql_test
import (
"context"
"reflect"
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
)
type testNamedType interface {
}
type testPet interface {
}
type testDog2 struct {
Name string `json:"name"`
Barks bool `json:"barks"`
}
type testCat2 struct {
Name string `json:"name"`
Meows bool `json:"meows"`
}
type testPerson struct {
Name string `json:"name"`
Pets []testPet `json:"pets"`
Friends []testNamedType `json:"friends"`
}
var namedType = graphql.NewInterface(graphql.InterfaceConfig{
Name: "Named",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
},
})
var dogType = graphql.NewObject(graphql.ObjectConfig{
Name: "Dog",
Interfaces: []*graphql.Interface{
namedType,
},
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
"barks": &graphql.Field{
Type: graphql.Boolean,
},
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*testDog2)
return ok
},
})
var catType = graphql.NewObject(graphql.ObjectConfig{
Name: "Cat",
Interfaces: []*graphql.Interface{
namedType,
},
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
"meows": &graphql.Field{
Type: graphql.Boolean,
},
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*testCat2)
return ok
},
})
var petType = graphql.NewUnion(graphql.UnionConfig{
Name: "Pet",
Types: []*graphql.Object{
dogType, catType,
},
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
if _, ok := p.Value.(*testCat2); ok {
return catType
}
if _, ok := p.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,
},
"pets": &graphql.Field{
Type: graphql.NewList(petType),
},
"friends": &graphql.Field{
Type: graphql.NewList(namedType),
},
},
IsTypeOf: func(p graphql.IsTypeOfParams) bool {
_, ok := p.Value.(*testPerson)
return ok
},
})
var unionInterfaceTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: personType,
Types: []graphql.Type{petType},
})
var garfield = &testCat2{"Garfield", false}
var odie = &testDog2{"Odie", true}
var liz = &testPerson{
Name: "Liz",
}
var john = &testPerson{
Name: "John",
Pets: []testPet{
garfield, odie,
},
Friends: []testNamedType{
liz, odie,
},
}
func TestUnionIntersectionTypes_CanIntrospectOnUnionAndIntersectionTypes(t *testing.T) {
doc := `
{
Named: __type(name: "Named") {
kind
name
fields { name }
interfaces { name }
possibleTypes { name }
enumValues { name }
inputFields { name }
}
Pet: __type(name: "Pet") {
kind
name
fields { name }
interfaces { name }
possibleTypes { name }
enumValues { name }
inputFields { name }
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"Named": map[string]interface{}{
"kind": "INTERFACE",
"name": "Named",
"fields": []interface{}{
map[string]interface{}{
"name": "name",
},
},
"interfaces": nil,
"possibleTypes": []interface{}{
map[string]interface{}{
"name": "Dog",
},
map[string]interface{}{
"name": "Cat",
},
map[string]interface{}{
"name": "Person",
},
},
"enumValues": nil,
"inputFields": nil,
},
"Pet": map[string]interface{}{
"kind": "UNION",
"name": "Pet",
"fields": nil,
"interfaces": nil,
"possibleTypes": []interface{}{
map[string]interface{}{
"name": "Dog",
},
map[string]interface{}{
"name": "Cat",
},
},
"enumValues": nil,
"inputFields": nil,
},
},
}
// parse query
ast := testutil.TestParse(t, doc)
// execute
ep := graphql.ExecuteParams{
Schema: unionInterfaceTestSchema,
AST: ast,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) != len(expected.Errors) {
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
if !testutil.ContainSubset(expected.Data.(map[string]interface{}), result.Data.(map[string]interface{})) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected.Data, result.Data))
}
}
func TestUnionIntersectionTypes_ExecutesUsingUnionTypes(t *testing.T) {
// NOTE: This is an *invalid* query, but it should be an *executable* query.
doc := `
{
__typename
name
pets {
__typename
name
barks
meows
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"__typename": "Person",
"name": "John",
"pets": []interface{}{
map[string]interface{}{
"__typename": "Cat",
"name": "Garfield",
"meows": false,
},
map[string]interface{}{
"__typename": "Dog",
"name": "Odie",
"barks": true,
},
},
},
}
// parse query
ast := testutil.TestParse(t, doc)
// execute
ep := graphql.ExecuteParams{
Schema: unionInterfaceTestSchema,
AST: ast,
Root: john,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) != len(expected.Errors) {
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestUnionIntersectionTypes_ExecutesUnionTypesWithInlineFragments(t *testing.T) {
// This is the valid version of the query in the above test.
doc := `
{
__typename
name
pets {
__typename
... on Dog {
name
barks
}
... on Cat {
name
meows
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"__typename": "Person",
"name": "John",
"pets": []interface{}{
map[string]interface{}{
"__typename": "Cat",
"name": "Garfield",
"meows": false,
},
map[string]interface{}{
"__typename": "Dog",
"name": "Odie",
"barks": true,
},
},
},
}
// parse query
ast := testutil.TestParse(t, doc)
// execute
ep := graphql.ExecuteParams{
Schema: unionInterfaceTestSchema,
AST: ast,
Root: john,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) != len(expected.Errors) {
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestUnionIntersectionTypes_ExecutesUsingInterfaceTypes(t *testing.T) {
// NOTE: This is an *invalid* query, but it should be an *executable* query.
doc := `
{
__typename
name
friends {
__typename
name
barks
meows
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"__typename": "Person",
"name": "John",
"friends": []interface{}{
map[string]interface{}{
"__typename": "Person",
"name": "Liz",
},
map[string]interface{}{
"__typename": "Dog",
"name": "Odie",
"barks": true,
},
},
},
}
// parse query
ast := testutil.TestParse(t, doc)
// execute
ep := graphql.ExecuteParams{
Schema: unionInterfaceTestSchema,
AST: ast,
Root: john,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) != len(expected.Errors) {
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestUnionIntersectionTypes_ExecutesInterfaceTypesWithInlineFragments(t *testing.T) {
// This is the valid version of the query in the above test.
doc := `
{
__typename
name
friends {
__typename
name
... on Dog {
barks
}
... on Cat {
meows
}
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"__typename": "Person",
"name": "John",
"friends": []interface{}{
map[string]interface{}{
"__typename": "Person",
"name": "Liz",
},
map[string]interface{}{
"__typename": "Dog",
"name": "Odie",
"barks": true,
},
},
},
}
// parse query
ast := testutil.TestParse(t, doc)
// execute
ep := graphql.ExecuteParams{
Schema: unionInterfaceTestSchema,
AST: ast,
Root: john,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) != len(expected.Errors) {
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestUnionIntersectionTypes_AllowsFragmentConditionsToBeAbstractTypes(t *testing.T) {
doc := `
{
__typename
name
pets { ...PetFields }
friends { ...FriendFields }
}
fragment PetFields on Pet {
__typename
... on Dog {
name
barks
}
... on Cat {
name
meows
}
}
fragment FriendFields on Named {
__typename
name
... on Dog {
barks
}
... on Cat {
meows
}
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"__typename": "Person",
"name": "John",
"friends": []interface{}{
map[string]interface{}{
"__typename": "Person",
"name": "Liz",
},
map[string]interface{}{
"__typename": "Dog",
"name": "Odie",
"barks": true,
},
},
"pets": []interface{}{
map[string]interface{}{
"__typename": "Cat",
"name": "Garfield",
"meows": false,
},
map[string]interface{}{
"__typename": "Dog",
"name": "Odie",
"barks": true,
},
},
},
}
// parse query
ast := testutil.TestParse(t, doc)
// execute
ep := graphql.ExecuteParams{
Schema: unionInterfaceTestSchema,
AST: ast,
Root: john,
}
result := testutil.TestExecute(t, ep)
if len(result.Errors) != len(expected.Errors) {
t.Fatalf("Unexpected errors, Diff: %v", testutil.Diff(expected.Errors, result.Errors))
}
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
func TestUnionIntersectionTypes_GetsExecutionInfoInResolver(t *testing.T) {
var encounteredContextValue string
var encounteredSchema graphql.Schema
var encounteredRootValue string
var personType2 *graphql.Object
namedType2 := graphql.NewInterface(graphql.InterfaceConfig{
Name: "Named",
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
},
ResolveType: func(p graphql.ResolveTypeParams) *graphql.Object {
encounteredSchema = p.Info.Schema
encounteredContextValue, _ = p.Context.Value("authToken").(string)
encounteredRootValue = p.Info.RootValue.(*testPerson).Name
return personType2
},
})
personType2 = graphql.NewObject(graphql.ObjectConfig{
Name: "Person",
Interfaces: []*graphql.Interface{
namedType2,
},
Fields: graphql.Fields{
"name": &graphql.Field{
Type: graphql.String,
},
"friends": &graphql.Field{
Type: graphql.NewList(namedType2),
},
},
})
schema2, _ := graphql.NewSchema(graphql.SchemaConfig{
Query: personType2,
})
john2 := &testPerson{
Name: "John",
Friends: []testNamedType{
liz,
},
}
doc := `{ name, friends { name } }`
expected := &graphql.Result{
Data: map[string]interface{}{
"name": "John",
"friends": []interface{}{
map[string]interface{}{
"name": "Liz",
},
},
},
}
// parse query
ast := testutil.TestParse(t, doc)
// create context
ctx := context.Background()
ctx = context.WithValue(ctx, "authToken", "contextStringValue123")
// execute
ep := graphql.ExecuteParams{
Schema: schema2,
AST: ast,
Root: john2,
Context: ctx,
}
result := testutil.TestExecute(t, ep)
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
if !reflect.DeepEqual("contextStringValue123", encounteredContextValue) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff("contextStringValue123", encounteredContextValue))
}
if !reflect.DeepEqual("John", encounteredRootValue) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff("John", encounteredRootValue))
}
if !reflect.DeepEqual(schema2, encounteredSchema) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(schema2, encounteredSchema))
}
}
func TestUnionIntersectionTypes_ValueMayBeNilPointer(t *testing.T) {
var unionInterfaceTestSchema, _ = graphql.NewSchema(graphql.SchemaConfig{
Query: graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"query": &graphql.Field{
Type: graphql.NewObject(graphql.ObjectConfig{
Name: "query",
Fields: graphql.Fields{
"pet": &graphql.Field{
Type: petType,
},
"named": &graphql.Field{
Type: namedType,
},
},
}),
Resolve: func(_ graphql.ResolveParams) (interface{}, error) {
return struct {
Pet *testCat2 `graphql:"pet"`
Named *testCat2 `graphql:"named"`
}{nil, nil}, nil
},
},
},
}),
})
query := `{
query {
pet {
__typename
}
named {
__typename
name
}
}
}`
expected := &graphql.Result{
Data: map[string]interface{}{
"query": map[string]interface{}{
"pet": map[string]interface{}{
"__typename": "Cat",
},
"named": map[string]interface{}{
"__typename": "Cat",
"name": nil,
},
}},
}
result := g(t, graphql.Params{
Schema: unionInterfaceTestSchema,
RequestString: query,
})
if !reflect.DeepEqual(expected, result) {
t.Fatalf("Unexpected result, Diff: %v", testutil.Diff(expected, result))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mdgg0816/graphql-go.git
git@gitee.com:mdgg0816/graphql-go.git
mdgg0816
graphql-go
graphql-go
v0.7.6

搜索帮助