Skip to content

Commit

Permalink
Support native go types for graphql operations. (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
schmidtw committed Sep 8, 2022
1 parent 93707b1 commit d2a5549
Show file tree
Hide file tree
Showing 10 changed files with 291 additions and 202 deletions.
50 changes: 25 additions & 25 deletions README.md
@@ -1,7 +1,7 @@
go-graphql-client
=======

[![Build Status](https://travis-ci.org/hasura/go-graphql-client.svg?branch=master)](https://travis-ci.org/hasura/go-graphql-client.svg?branch=master) [![GoDoc](https://godoc.org/github.com/hasura/go-graphql-client?status.svg)](https://pkg.go.dev/github.com/hasura/go-graphql-client)
[![Unit tests](https://github.com/hasura/go-graphql-client/actions/workflows/test.yml/badge.svg)](https://github.com/hasura/go-graphql-client/actions/workflows/test.yml)

**Preface:** This is a fork of `https://github.com/shurcooL/graphql` with extended features (subscription client, named operation)

Expand Down Expand Up @@ -99,7 +99,7 @@ You can define this variable:
```Go
var query struct {
Me struct {
Name graphql.String
Name string
}
}
```
Expand Down Expand Up @@ -136,8 +136,8 @@ You can define this variable:
```Go
var q struct {
Human struct {
Name graphql.String
Height graphql.Float `graphql:"height(unit: METER)"`
Name string
Height float64 `graphql:"height(unit: METER)"`
} `graphql:"human(id: \"1000\")"`
}
```
Expand All @@ -162,8 +162,8 @@ However, that'll only work if the arguments are constant and known in advance. O
```Go
var q struct {
Human struct {
Name graphql.String
Height graphql.Float `graphql:"height(unit: $unit)"`
Name string
Height float64 `graphql:"height(unit: $unit)"`
} `graphql:"human(id: $id)"`
}
```
Expand Down Expand Up @@ -264,12 +264,12 @@ You can define this variable:
```Go
var q struct {
Hero struct {
Name graphql.String
Name string
Droid struct {
PrimaryFunction graphql.String
PrimaryFunction string
} `graphql:"... on Droid"`
Human struct {
Height graphql.Float
Height float64
} `graphql:"... on Human"`
} `graphql:"hero(episode: \"JEDI\")"`
}
Expand All @@ -280,16 +280,16 @@ Alternatively, you can define the struct types corresponding to inline fragments
```Go
type (
DroidFragment struct {
PrimaryFunction graphql.String
PrimaryFunction string
}
HumanFragment struct {
Height graphql.Float
Height float64
}
)

var q struct {
Hero struct {
Name graphql.String
Name string
DroidFragment `graphql:"... on Droid"`
HumanFragment `graphql:"... on Human"`
} `graphql:"hero(episode: \"JEDI\")"`
Expand Down Expand Up @@ -319,8 +319,8 @@ The GraphQL type is automatically inferred from Go type by reflection. However,
```go
type UserReviewInput struct {
Review String
UserID String
Review string
UserID string
}

// type alias
Expand Down Expand Up @@ -365,15 +365,15 @@ You can define:
```Go
var m struct {
CreateReview struct {
Stars graphql.Int
Commentary graphql.String
Stars int
Commentary string
} `graphql:"createReview(episode: $ep, review: $review)"`
}
variables := map[string]interface{}{
"ep": starwars.Episode("JEDI"),
"review": starwars.ReviewInput{
Stars: graphql.Int(5),
Commentary: graphql.String("This is a great movie!"),
Stars: 5,
Commentary: "This is a great movie!",
},
}
```
Expand Down Expand Up @@ -419,8 +419,8 @@ var m struct {
variables := map[string]interface{}{
"ep": starwars.Episode("JEDI"),
"review": starwars.ReviewInput{
Stars: graphql.Int(5),
Commentary: graphql.String("This is a great movie!"),
Stars: 5,
Commentary: "This is a great movie!",
},
}
```
Expand Down Expand Up @@ -473,7 +473,7 @@ You can define this variable:
```Go
var subscription struct {
Me struct {
Name graphql.String
Name string
}
}
```
Expand Down Expand Up @@ -780,17 +780,17 @@ You can define:
```Go
type CreateUser struct {
Login graphql.String
Login string
}
m := [][2]interface{}{
{"createUser(login: $login1)", &CreateUser{}},
{"createUser(login: $login2)", &CreateUser{}},
{"createUser(login: $login3)", &CreateUser{}},
}
variables := map[string]interface{}{
"login1": graphql.String("grihabor"),
"login2": graphql.String("diman"),
"login3": graphql.String("indigo"),
"login1": "grihabor",
"login2": "diman",
"login3": "indigo",
}
```
Expand Down
10 changes: 5 additions & 5 deletions example/graphqldev/main.go
Expand Up @@ -59,15 +59,15 @@ func run() error {
var q struct {
Hero struct {
ID graphql.ID
Name graphql.String
Name string
}
Character struct {
Name graphql.String
Name string
Friends []struct {
Name graphql.String
Typename graphql.String `graphql:"__typename"`
Name string
Typename string `graphql:"__typename"`
}
AppearsIn []graphql.String
AppearsIn []string
} `graphql:"character(id: $characterID)"`
}
variables := map[string]interface{}{
Expand Down
2 changes: 1 addition & 1 deletion example/realworld/main.go
Expand Up @@ -36,7 +36,7 @@ func run() error {
*/
var q struct {
Character struct {
Name graphql.String
Name string
} `graphql:"character(id: $characterID)"`
}
variables := map[string]interface{}{
Expand Down
10 changes: 5 additions & 5 deletions example/subscription/client.go
Expand Up @@ -40,8 +40,8 @@ func startSubscription() error {
*/
var sub struct {
HelloSaid struct {
ID graphql.String
Message graphql.String `graphql:"msg"`
ID graphql.ID
Message string `graphql:"msg"`
} `graphql:"helloSaid"`
}

Expand Down Expand Up @@ -89,12 +89,12 @@ func startSendHello() {
*/
var q struct {
SayHello struct {
ID graphql.String
Msg graphql.String
ID graphql.ID
Msg string
} `graphql:"sayHello(msg: $msg)"`
}
variables := map[string]interface{}{
"msg": graphql.String(randomID()),
"msg": randomID(),
}
err := client.Mutate(context.Background(), &q, variables, graphql.OperationName("SayHello"))
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions graphql_test.go
Expand Up @@ -159,7 +159,7 @@ func TestClient_Query_noDataWithErrorResponse(t *testing.T) {

var q struct {
User struct {
Name graphql.String
Name string
}
}
err := client.Query(context.Background(), &q, nil)
Expand Down Expand Up @@ -209,7 +209,7 @@ func TestClient_Query_errorStatusCode(t *testing.T) {

var q struct {
User struct {
Name graphql.String
Name string
}
}
err := client.Query(context.Background(), &q, nil)
Expand Down
7 changes: 3 additions & 4 deletions internal/jsonutil/benchmark_test.go
Expand Up @@ -8,7 +8,6 @@ import (
"testing"
"time"

graphql "github.com/hasura/go-graphql-client"
"github.com/hasura/go-graphql-client/internal/jsonutil"
)

Expand All @@ -23,7 +22,7 @@ func TestUnmarshalGraphQL_benchmark(t *testing.T) {
*/
type query struct {
Viewer struct {
Login graphql.String
Login string
CreatedAt time.Time
}
}
Expand All @@ -48,7 +47,7 @@ func TestUnmarshalGraphQL_benchmark(t *testing.T) {
func BenchmarkUnmarshalGraphQL(b *testing.B) {
type query struct {
Viewer struct {
Login graphql.String
Login string
CreatedAt time.Time
}
}
Expand Down Expand Up @@ -76,7 +75,7 @@ func BenchmarkUnmarshalGraphQL(b *testing.B) {
func BenchmarkJSONUnmarshal(b *testing.B) {
type query struct {
Viewer struct {
Login graphql.String
Login string
CreatedAt time.Time
}
}
Expand Down

0 comments on commit d2a5549

Please sign in to comment.