代码拉取完成,页面将自动刷新
package graphql_test
import (
"fmt"
"reflect"
"testing"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
)
// TODO: have a separate package for other tests for eg `parser`
// maybe for:
// - tests that supposed to be black-boxed (no reason to access private identifiers)
// - tests that create internal tests structs, we might not want to pollute the package with too many test structs
type testPic struct {
Url string `json:"url"`
Width string `json:"width"`
Height string `json:"height"`
}
type testPicFn func(width, height string) *testPic
type testAuthor struct {
Id int `json:"id"`
Name string `json:"name"`
Pic testPicFn `json:"pic"`
RecentArticle *testArticle `json:"recentArticle"`
}
type testArticle struct {
Id string `json:"id"`
IsPublished string `json:"isPublished"`
Author *testAuthor `json:"author"`
Title string `json:"title"`
Body string `json:"body"`
Hidden string `json:"hidden"`
Keywords []interface{} `json:"keywords"`
}
func getPic(id int, width, height string) *testPic {
return &testPic{
Url: fmt.Sprintf("cdn://%v", id),
Width: width,
Height: height,
}
}
var johnSmith *testAuthor
func article(id interface{}) *testArticle {
return &testArticle{
Id: fmt.Sprintf("%v", id),
IsPublished: "true",
Author: johnSmith,
Title: fmt.Sprintf("My Article %v", id),
Body: "This is a post",
Hidden: "This data is not exposed in the schema",
Keywords: []interface{}{
"foo", "bar", 1, true, nil,
},
}
}
func TestExecutesUsingAComplexSchema(t *testing.T) {
johnSmith = &testAuthor{
Id: 123,
Name: "John Smith",
Pic: func(width string, height string) *testPic {
return getPic(123, width, height)
},
RecentArticle: article("1"),
}
blogImage := graphql.NewObject(graphql.ObjectConfig{
Name: "Image",
Fields: graphql.Fields{
"url": &graphql.Field{
Type: graphql.String,
},
"width": &graphql.Field{
Type: graphql.Int,
},
"height": &graphql.Field{
Type: graphql.Int,
},
},
})
blogAuthor := graphql.NewObject(graphql.ObjectConfig{
Name: "Author",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.String,
},
"name": &graphql.Field{
Type: graphql.String,
},
"pic": &graphql.Field{
Type: blogImage,
Args: graphql.FieldConfigArgument{
"width": &graphql.ArgumentConfig{
Type: graphql.Int,
},
"height": &graphql.ArgumentConfig{
Type: graphql.Int,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if author, ok := p.Source.(*testAuthor); ok {
width := fmt.Sprintf("%v", p.Args["width"])
height := fmt.Sprintf("%v", p.Args["height"])
return author.Pic(width, height), nil
}
return nil, nil
},
},
"recentArticle": &graphql.Field{},
},
})
blogArticle := graphql.NewObject(graphql.ObjectConfig{
Name: "Article",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
},
"isPublished": &graphql.Field{
Type: graphql.Boolean,
},
"author": &graphql.Field{
Type: blogAuthor,
},
"title": &graphql.Field{
Type: graphql.String,
},
"body": &graphql.Field{
Type: graphql.String,
},
"keywords": &graphql.Field{
Type: graphql.NewList(graphql.String),
},
},
})
blogAuthor.AddFieldConfig("recentArticle", &graphql.Field{
Type: blogArticle,
})
blogQuery := graphql.NewObject(graphql.ObjectConfig{
Name: "Query",
Fields: graphql.Fields{
"article": &graphql.Field{
Type: blogArticle,
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.ID,
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
id := p.Args["id"]
return article(id), nil
},
},
"feed": &graphql.Field{
Type: graphql.NewList(blogArticle),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return []*testArticle{
article(1),
article(2),
article(3),
article(4),
article(5),
article(6),
article(7),
article(8),
article(9),
article(10),
}, nil
},
},
},
})
blogSchema, err := graphql.NewSchema(graphql.SchemaConfig{
Query: blogQuery,
})
if err != nil {
t.Fatalf("Error in schema %v", err.Error())
}
request := `
{
feed {
id,
title
},
article(id: "1") {
...articleFields,
author {
id,
name,
pic(width: 640, height: 480) {
url,
width,
height
},
recentArticle {
...articleFields,
keywords
}
}
}
}
fragment articleFields on Article {
id,
isPublished,
title,
body,
hidden,
notdefined
}
`
expected := &graphql.Result{
Data: map[string]interface{}{
"article": map[string]interface{}{
"title": "My Article 1",
"body": "This is a post",
"author": map[string]interface{}{
"id": "123",
"name": "John Smith",
"pic": map[string]interface{}{
"url": "cdn://123",
"width": 640,
"height": 480,
},
"recentArticle": map[string]interface{}{
"id": "1",
"isPublished": bool(true),
"title": "My Article 1",
"body": "This is a post",
"keywords": []interface{}{
"foo",
"bar",
"1",
"true",
nil,
},
},
},
"id": "1",
"isPublished": bool(true),
},
"feed": []interface{}{
map[string]interface{}{
"id": "1",
"title": "My Article 1",
},
map[string]interface{}{
"id": "2",
"title": "My Article 2",
},
map[string]interface{}{
"id": "3",
"title": "My Article 3",
},
map[string]interface{}{
"id": "4",
"title": "My Article 4",
},
map[string]interface{}{
"id": "5",
"title": "My Article 5",
},
map[string]interface{}{
"id": "6",
"title": "My Article 6",
},
map[string]interface{}{
"id": "7",
"title": "My Article 7",
},
map[string]interface{}{
"id": "8",
"title": "My Article 8",
},
map[string]interface{}{
"id": "9",
"title": "My Article 9",
},
map[string]interface{}{
"id": "10",
"title": "My Article 10",
},
},
},
}
// parse query
ast := testutil.TestParse(t, request)
// execute
ep := graphql.ExecuteParams{
Schema: blogSchema,
AST: ast,
}
result := testutil.TestExecute(t, ep)
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))
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。