Skip to content

poohvpn/gqlgo

Repository files navigation

GQLGo

An open and well design GraphQL Client for Gophers, it's going to be great.
And welcome issue and PR by the way.

Features

  • GraphQL
    • basis request
    • batch requests
    • error handling
    • subscriptions
    • file upload
  • Custom HTTP Header

Usage

You can check example faster to make the program run.

Install

go get -u github.com/poohvpn/gqlgo

Create Client

client := gqlgo.NewClient(`https://some_endpoint`)
// or
client := gqlgo.NewClient(`https://some_endpoint`, gqlgo.Option{
	HTTPClient: myHttpClient,
	BearerAuth: "token",
	Headers: ...,
	WebSocketEndpoint: "wss://some_endpoint",
})

Do a GraphQL Request

res := struct {
	User struct {
		ID   string `json:"id"`
		Name string `json:"name"`
		Email string `json:"email"`
	} `json:"User"`
}{}
err := client.Do(context.Background(), &res, gqlgo.Request{
	Query: `
query ($id:ID){
	User (id: $id) {
		id
		name
		email
	}
}
`,
	Variables: map[string]interface{}{
		"id": "...",
	},
})

GraphQL error handling

detailErr := &gqlgo.DetailError{}
gqlErr := gqlgo.GraphQLErrors{}
switch {
case err == nil:
case errors.As(err, &detailErr):
	fmt.Println("detail error:", detailErr.Response.StatusCode, "\n", detailErr.Content)
	return
case errors.As(err, &gqlErr):
	gqlOneErr := gqlErr[0]
	fmt.Println("graphql server error:",
		gqlOneErr.Message,
		"on line",
		gqlOneErr.Locations[0].Line,
		"column",
		gqlOneErr.Locations[0].Column,
	)
	return
default:
	fmt.Println("graphql client error:", err.Error())
	return
}

Batch Requests

req1 := gqlgo.Request{...}
req2 := gqlgo.Request{...}
data := []interface{}{&data1, &data2}
err := client.Do(context.Background(), data, req1, req2)

Subscription

req1 := gqlgo.Request{...}
subId,err := client.Subscribe(req, func(data json.RawMessage, errors gqlgo.GraphQLErrors, completed bool) error {
	...
})

Credits

GraphQL Spec
GraphQL MultiPart Request Spec
Thanks to machinebox/graphql, learning a lot from it.
apollographql/subscriptions-transport-ws

License

Apache License 2.0
Copyright 2020 PoohVPN