graphql-go / graphql

An implementation of GraphQL for Go / Golang
MIT License
9.91k stars 839 forks source link

deserialize of graphql.SchemaConfig is failing #490

Open continuum-dipanjan-mazumder opened 5 years ago

continuum-dipanjan-mazumder commented 5 years ago

While trying to deserialize graphql.SchemaConfig object its failing:

failed to create new schema, error: QueryType fields must be an object with field names as keys or a function which return such an object.

Code below:

package main

import ( "encoding/json" "errors" "fmt" "io/ioutil" "log" "net/http"

"github.com/hprose/hprose-go"

"github.com/graphql-go/graphql"
gqlhandler "github.com/graphql-go/graphql-go-handler"

)

// Post is a post type Post struct { UserID int json:"userId" ID int json:"id" Title string json:"title" Body string json:"body" }

// Comment is a comment type Comment struct { PostID int json:"postId" ID int json:"id" Name string json:"name" Email string json:"email" Body string json:"body" }

func main() {

schemaCfg := graphql.SchemaConfig{
    Query: graphql.NewObject(
        createQueryType(
            createPostType(
                createCommentType(),
            ),
        ),
    ),
}
Serialized, err := hprose.Serialize(schemaCfg, true)
if err != nil {
    panic(err)
}

fmt.Println("seialize", string(Serialized))
var NewBook graphql.SchemaConfig
err = hprose.Unserialize(Serialized, &NewBook, true)
if err != nil {
    panic(err)
}

schema, err := graphql.NewSchema(NewBook)

if err != nil {
    log.Fatalf("failed to create new schema, error: %v", err)
}
handler := gqlhandler.New(&gqlhandler.Config{
    Schema: &schema,
})

fs := http.FileServer(http.Dir("static"))
http.Handle("/graphql", handler)
http.Handle("/", fs)
log.Println("Server started at http://localhost:3000/graphql")
log.Fatal(http.ListenAndServe(":3000", nil))

}

func createQueryType(postType *graphql.Object) graphql.ObjectConfig { return graphql.ObjectConfig{Name: "QueryType", Fields: graphql.Fields{ "post": &graphql.Field{ Type: postType, Args: graphql.FieldConfigArgument{ "id": &graphql.ArgumentConfig{ Type: graphql.NewNonNull(graphql.Int), }, }, Resolve: func(p graphql.ResolveParams) (interface{}, error) { id := p.Args["id"] v, _ := id.(int) log.Printf("fetching post with id: %d", v) return fetchPostByiD(v) }, }, }, } }

func createPostType(commentType graphql.Object) graphql.Object { return graphql.NewObject(graphql.ObjectConfig{ Name: "Post", Fields: graphql.Fields{ "userId": &graphql.Field{ Type: graphql.NewNonNull(graphql.Int), }, "id": &graphql.Field{ Type: graphql.NewNonNull(graphql.Int), }, "title": &graphql.Field{ Type: graphql.String, }, "body": &graphql.Field{ Type: graphql.String, }, "comments": &graphql.Field{ Type: graphql.NewList(commentType), Resolve: func(p graphql.ResolveParams) (interface{}, error) { post, _ := p.Source.(*Post) log.Printf("fetching comments of post with id: %d", post.ID) return fetchCommentsByPostID(post.ID) }, }, }, }) }

func createCommentType() *graphql.Object { return graphql.NewObject(graphql.ObjectConfig{ Name: "Comment", Fields: graphql.Fields{ "postId": &graphql.Field{ Type: graphql.NewNonNull(graphql.Int), }, "id": &graphql.Field{ Type: graphql.NewNonNull(graphql.Int), }, "name": &graphql.Field{ Type: graphql.String, }, "email": &graphql.Field{ Type: graphql.String, }, "body": &graphql.Field{ Type: graphql.String, }, }, }) }

func fetchPostByiD(id int) (*Post, error) { resp, err := http.Get(fmt.Sprintf("http://jsonplaceholder.typicode.com/posts/%d", id)) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != 200 { return nil, fmt.Errorf("%s: %s", "could not fetch data", resp.Status) } b, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, errors.New("could not read data") } result := Post{} err = json.Unmarshal(b, &result) if err != nil { return nil, errors.New("could not unmarshal data") } return &result, nil }

func fetchCommentsByPostID(id int) ([]Comment, error) { resp, err := http.Get(fmt.Sprintf("http://jsonplaceholder.typicode.com/posts/%d/comments", id)) if err != nil { return nil, err } defer resp.Body.Close() if resp.StatusCode != 200 { return nil, fmt.Errorf("%s: %s", "could not fetch data", resp.Status) } b, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, errors.New("could not read data") } result := []Comment{} err = json.Unmarshal(b, &result) if err != nil { return nil, errors.New("could not unmarshal data") } return result, nil }