Skip to content

Commit d2a5549

Browse files
authored
Support native go types for graphql operations. (#45)
1 parent 93707b1 commit d2a5549

File tree

10 files changed

+291
-202
lines changed

10 files changed

+291
-202
lines changed

README.md

+25-25
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
go-graphql-client
22
=======
33

4-
[![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)
4+
[![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)
55

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

@@ -99,7 +99,7 @@ You can define this variable:
9999
```Go
100100
var query struct {
101101
Me struct {
102-
Name graphql.String
102+
Name string
103103
}
104104
}
105105
```
@@ -136,8 +136,8 @@ You can define this variable:
136136
```Go
137137
var q struct {
138138
Human struct {
139-
Name graphql.String
140-
Height graphql.Float `graphql:"height(unit: METER)"`
139+
Name string
140+
Height float64 `graphql:"height(unit: METER)"`
141141
} `graphql:"human(id: \"1000\")"`
142142
}
143143
```
@@ -162,8 +162,8 @@ However, that'll only work if the arguments are constant and known in advance. O
162162
```Go
163163
var q struct {
164164
Human struct {
165-
Name graphql.String
166-
Height graphql.Float `graphql:"height(unit: $unit)"`
165+
Name string
166+
Height float64 `graphql:"height(unit: $unit)"`
167167
} `graphql:"human(id: $id)"`
168168
}
169169
```
@@ -264,12 +264,12 @@ You can define this variable:
264264
```Go
265265
var q struct {
266266
Hero struct {
267-
Name graphql.String
267+
Name string
268268
Droid struct {
269-
PrimaryFunction graphql.String
269+
PrimaryFunction string
270270
} `graphql:"... on Droid"`
271271
Human struct {
272-
Height graphql.Float
272+
Height float64
273273
} `graphql:"... on Human"`
274274
} `graphql:"hero(episode: \"JEDI\")"`
275275
}
@@ -280,16 +280,16 @@ Alternatively, you can define the struct types corresponding to inline fragments
280280
```Go
281281
type (
282282
DroidFragment struct {
283-
PrimaryFunction graphql.String
283+
PrimaryFunction string
284284
}
285285
HumanFragment struct {
286-
Height graphql.Float
286+
Height float64
287287
}
288288
)
289289

290290
var q struct {
291291
Hero struct {
292-
Name graphql.String
292+
Name string
293293
DroidFragment `graphql:"... on Droid"`
294294
HumanFragment `graphql:"... on Human"`
295295
} `graphql:"hero(episode: \"JEDI\")"`
@@ -319,8 +319,8 @@ The GraphQL type is automatically inferred from Go type by reflection. However,
319319
320320
```go
321321
type UserReviewInput struct {
322-
Review String
323-
UserID String
322+
Review string
323+
UserID string
324324
}
325325

326326
// type alias
@@ -365,15 +365,15 @@ You can define:
365365
```Go
366366
var m struct {
367367
CreateReview struct {
368-
Stars graphql.Int
369-
Commentary graphql.String
368+
Stars int
369+
Commentary string
370370
} `graphql:"createReview(episode: $ep, review: $review)"`
371371
}
372372
variables := map[string]interface{}{
373373
"ep": starwars.Episode("JEDI"),
374374
"review": starwars.ReviewInput{
375-
Stars: graphql.Int(5),
376-
Commentary: graphql.String("This is a great movie!"),
375+
Stars: 5,
376+
Commentary: "This is a great movie!",
377377
},
378378
}
379379
```
@@ -419,8 +419,8 @@ var m struct {
419419
variables := map[string]interface{}{
420420
"ep": starwars.Episode("JEDI"),
421421
"review": starwars.ReviewInput{
422-
Stars: graphql.Int(5),
423-
Commentary: graphql.String("This is a great movie!"),
422+
Stars: 5,
423+
Commentary: "This is a great movie!",
424424
},
425425
}
426426
```
@@ -473,7 +473,7 @@ You can define this variable:
473473
```Go
474474
var subscription struct {
475475
Me struct {
476-
Name graphql.String
476+
Name string
477477
}
478478
}
479479
```
@@ -780,17 +780,17 @@ You can define:
780780
781781
```Go
782782
type CreateUser struct {
783-
Login graphql.String
783+
Login string
784784
}
785785
m := [][2]interface{}{
786786
{"createUser(login: $login1)", &CreateUser{}},
787787
{"createUser(login: $login2)", &CreateUser{}},
788788
{"createUser(login: $login3)", &CreateUser{}},
789789
}
790790
variables := map[string]interface{}{
791-
"login1": graphql.String("grihabor"),
792-
"login2": graphql.String("diman"),
793-
"login3": graphql.String("indigo"),
791+
"login1": "grihabor",
792+
"login2": "diman",
793+
"login3": "indigo",
794794
}
795795
```
796796

example/graphqldev/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ func run() error {
5959
var q struct {
6060
Hero struct {
6161
ID graphql.ID
62-
Name graphql.String
62+
Name string
6363
}
6464
Character struct {
65-
Name graphql.String
65+
Name string
6666
Friends []struct {
67-
Name graphql.String
68-
Typename graphql.String `graphql:"__typename"`
67+
Name string
68+
Typename string `graphql:"__typename"`
6969
}
70-
AppearsIn []graphql.String
70+
AppearsIn []string
7171
} `graphql:"character(id: $characterID)"`
7272
}
7373
variables := map[string]interface{}{

example/realworld/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func run() error {
3636
*/
3737
var q struct {
3838
Character struct {
39-
Name graphql.String
39+
Name string
4040
} `graphql:"character(id: $characterID)"`
4141
}
4242
variables := map[string]interface{}{

example/subscription/client.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func startSubscription() error {
4040
*/
4141
var sub struct {
4242
HelloSaid struct {
43-
ID graphql.String
44-
Message graphql.String `graphql:"msg"`
43+
ID graphql.ID
44+
Message string `graphql:"msg"`
4545
} `graphql:"helloSaid"`
4646
}
4747

@@ -89,12 +89,12 @@ func startSendHello() {
8989
*/
9090
var q struct {
9191
SayHello struct {
92-
ID graphql.String
93-
Msg graphql.String
92+
ID graphql.ID
93+
Msg string
9494
} `graphql:"sayHello(msg: $msg)"`
9595
}
9696
variables := map[string]interface{}{
97-
"msg": graphql.String(randomID()),
97+
"msg": randomID(),
9898
}
9999
err := client.Mutate(context.Background(), &q, variables, graphql.OperationName("SayHello"))
100100
if err != nil {

graphql_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func TestClient_Query_noDataWithErrorResponse(t *testing.T) {
159159

160160
var q struct {
161161
User struct {
162-
Name graphql.String
162+
Name string
163163
}
164164
}
165165
err := client.Query(context.Background(), &q, nil)
@@ -209,7 +209,7 @@ func TestClient_Query_errorStatusCode(t *testing.T) {
209209

210210
var q struct {
211211
User struct {
212-
Name graphql.String
212+
Name string
213213
}
214214
}
215215
err := client.Query(context.Background(), &q, nil)

internal/jsonutil/benchmark_test.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"testing"
99
"time"
1010

11-
graphql "github.com/hasura/go-graphql-client"
1211
"github.com/hasura/go-graphql-client/internal/jsonutil"
1312
)
1413

@@ -23,7 +22,7 @@ func TestUnmarshalGraphQL_benchmark(t *testing.T) {
2322
*/
2423
type query struct {
2524
Viewer struct {
26-
Login graphql.String
25+
Login string
2726
CreatedAt time.Time
2827
}
2928
}
@@ -48,7 +47,7 @@ func TestUnmarshalGraphQL_benchmark(t *testing.T) {
4847
func BenchmarkUnmarshalGraphQL(b *testing.B) {
4948
type query struct {
5049
Viewer struct {
51-
Login graphql.String
50+
Login string
5251
CreatedAt time.Time
5352
}
5453
}
@@ -76,7 +75,7 @@ func BenchmarkUnmarshalGraphQL(b *testing.B) {
7675
func BenchmarkJSONUnmarshal(b *testing.B) {
7776
type query struct {
7877
Viewer struct {
79-
Login graphql.String
78+
Login string
8079
CreatedAt time.Time
8180
}
8281
}

0 commit comments

Comments
 (0)