Created
February 5, 2020 01:56
-
-
Save chris-ramon/6c98e31259d1c7837fc3f240ee4f988d to your computer and use it in GitHub Desktop.
github.com/graphql-go ordered serially execution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"github.com/graphql-go/graphql" | |
) | |
type Birthday struct { | |
Month int8 `json:"month"` | |
} | |
var BirthdayType = graphql.NewObject(graphql.ObjectConfig{ | |
Name: "BirthdayType", | |
Fields: graphql.Fields{ | |
"month": &graphql.Field{ | |
Type: graphql.Int, | |
}, | |
}, | |
}) | |
type Address struct { | |
Street string `json:"street"` | |
} | |
var AddressType = graphql.NewObject(graphql.ObjectConfig{ | |
Name: "AddressType", | |
Fields: graphql.Fields{ | |
"street": &graphql.Field{ | |
Type: graphql.String, | |
}, | |
}, | |
}) | |
var RootQuery = graphql.NewObject(graphql.ObjectConfig{ | |
Name: "RootQuery", | |
Fields: graphql.Fields{ | |
"hello": &graphql.Field{ | |
Type: graphql.String, | |
Resolve: func(p graphql.ResolveParams) (interface{}, error) { | |
return "ok", nil | |
}, | |
}, | |
}, | |
}) | |
var RootMutation = graphql.NewObject(graphql.ObjectConfig{ | |
Name: "RootMutation", | |
Fields: graphql.Fields{ | |
"birthday": &graphql.Field{ | |
Type: BirthdayType, | |
Resolve: func(p graphql.ResolveParams) (interface{}, error) { | |
birthday := Birthday{ | |
Month: 2, | |
} | |
log.Printf("[birthday] resolve executed") | |
return birthday, nil | |
}, | |
}, | |
"address": &graphql.Field{ | |
Type: AddressType, | |
Resolve: func(p graphql.ResolveParams) (interface{}, error) { | |
address := Address{ | |
Street: "gopher avenue", | |
} | |
log.Printf("[address] resolve executed") | |
return address, nil | |
}, | |
}, | |
}, | |
}) | |
func main() { | |
schema, err := graphql.NewSchema(graphql.SchemaConfig{ | |
Query: RootQuery, | |
Mutation: RootMutation, | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
query := ` | |
mutation { | |
first: birthday { | |
month | |
} | |
second: address { | |
street | |
} | |
third: birthday { | |
month | |
} | |
fourth: address { | |
street | |
} | |
fifth: address { | |
street | |
} | |
} | |
` | |
result := graphql.Do(graphql.Params{ | |
Schema: schema, | |
RequestString: query, | |
}) | |
resultStr, err := json.Marshal(result) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(resultStr)) | |
} | |
/* | |
(demo)-> go run main.go | |
2020/02/04 20:47:49 added, lowest: 23, responseName: first | |
2020/02/04 20:47:49 added, lowest: 72, responseName: second | |
2020/02/04 20:47:49 added, lowest: 119, responseName: third | |
2020/02/04 20:47:49 added, lowest: 165, responseName: fourth | |
2020/02/04 20:47:49 added, lowest: 212, responseName: fifth | |
2020/02/04 20:47:49 [birthday] resolve executed | |
2020/02/04 20:47:49 [address] resolve executed | |
2020/02/04 20:47:49 [birthday] resolve executed | |
2020/02/04 20:47:49 [address] resolve executed | |
2020/02/04 20:47:49 [address] resolve executed | |
{"data":{"fifth":{"street":"gopher avenue"},"first":{"month":2},"fourth":{"street":"gopher avenue"},"second":{"street":"gopher avenue"},"third":{"month":2}}} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment