wire up example by way of testing
This commit is contained in:
@@ -0,0 +1,16 @@
|
|||||||
|
package example
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Main() {
|
||||||
|
resp, err := getViewer(context.Background())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
fmt.Println("you are:", resp.Viewer.Name)
|
||||||
|
}
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
example "github.com/Khan/genql/example"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
example.Main()
|
||||||
|
}
|
||||||
@@ -3,6 +3,7 @@ package example
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -10,19 +11,19 @@ import (
|
|||||||
"github.com/vektah/gqlparser/gqlerror"
|
"github.com/vektah/gqlparser/gqlerror"
|
||||||
)
|
)
|
||||||
|
|
||||||
type GetViewerResponse = struct {
|
type getViewerResponse = struct {
|
||||||
Viewer struct {
|
Viewer struct {
|
||||||
Name *string
|
Name *string
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
func GetViewer(ctx context.Context) (*GetViewerResponse, error) {
|
func getViewer(ctx context.Context) (*getViewerResponse, error) {
|
||||||
req, err := http.NewRequest(
|
req, err := http.NewRequest(
|
||||||
http.MethodPost,
|
http.MethodPost,
|
||||||
`https://api.github.com/graphql`,
|
`https://api.github.com/graphql`,
|
||||||
strings.NewReader(`
|
strings.NewReader(`
|
||||||
query GetViewer {
|
query getViewer {
|
||||||
Viewer: viewer {
|
Viewer: viewer {
|
||||||
Name: name
|
Name: name
|
||||||
}
|
}
|
||||||
@@ -44,8 +45,12 @@ query GetViewer {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("returned error %v: %v", resp.Status, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
var retval struct {
|
var retval struct {
|
||||||
Data GetViewerResponse `json:"data"`
|
Data getViewerResponse `json:"data"`
|
||||||
Errors gqlerror.List `json:"errors"`
|
Errors gqlerror.List `json:"errors"`
|
||||||
}
|
}
|
||||||
err = json.Unmarshal(body, &retval)
|
err = json.Unmarshal(body, &retval)
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# GetViewer gets the current user's name.
|
# getViewer gets the current user's name.
|
||||||
query GetViewer {
|
query getViewer {
|
||||||
Viewer: viewer {
|
Viewer: viewer {
|
||||||
Name: name
|
Name: name
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package {{.PackageName}}
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -35,6 +36,10 @@ func {{.OperationName}}(ctx context.Context) (*{{.ResponseName}}, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, fmt.Errorf("returned error %v: %v", resp.Status, string(body))
|
||||||
|
}
|
||||||
|
|
||||||
var retval struct {
|
var retval struct {
|
||||||
Data {{.ResponseName}} `json:"data"`
|
Data {{.ResponseName}} `json:"data"`
|
||||||
Errors gqlerror.List `json:"errors"`
|
Errors gqlerror.List `json:"errors"`
|
||||||
|
|||||||
Reference in New Issue
Block a user