1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
|
# gqlclient
[](https://godocs.io/git.sr.ht/~emersion/gqlclient)
[](https://builds.sr.ht/~emersion/gqlclient/commits?)
A GraphQL client and code generator for Go.
## Usage
gqlclient can be used as a thin GraphQL client, and can be augmented with code
generation. See the [GoDoc examples] for direct usage.
### GraphQL schema code generation
The code generator can parse a GraphQL schema and generate Go types. For
instance, the following schema:
```graphqls
type Train {
name: String!
maxSpeed: Int!
weight: Int!
linesServed: [String!]!
}
```
and the following `gqlclientgen` invocation:
```sh
gqlclientgen -s schema.graphqls -o gql.go -n rail
```
will generate the following Go type:
```go
type Train struct {
Name string
MaxSpeed int32
Weight int32
LinesServed []string
}
```
which can then be used in a GraphQL query:
```go
op := gqlclient.NewOperation(`query {
train(name: "Shinkansen E5") {
name
maxSpeed
linesServed
}
}`)
var data struct {
Train rail.Train
}
if err := c.Execute(ctx, op, &data); err != nil {
log.Fatal(err)
}
log.Print(data.Train)
```
### GraphQL query code generation
The code generator can also parse a GraphQL query document and generate Go
functions. For instance, the following query document:
```graphql
query fetchTrain($name: String!) {
train(name: $name) {
maxSpeed
linesServed
}
}
```
and the following `gqlclientgen` invocation:
```sh
gqlclientgen -s schema.graphqls -q queries.graphql -o gql.go -n rail
```
will generate the following function:
```go
func FetchTrain(client *gqlclient.Client, ctx context.Context, name string) (Train, error)
```
which can then be used to execute the query:
```go
train, err := rail.FetchTrain(c, ctx, "Shinkansen E5")
if err != nil {
log.Fatal(err)
}
log.Print(train)
```
### GraphQL schema introspection
gqlclient also supports fetching GraphQL schemas through GraphQL introspection.
For instance, the following `gqlintrospect` invocation will fetch the GraphQL
schema of the `https://example.com/query` GraphQL endpoint:
```sh
gqlintrospect https://example.com/query > schema.graphqls
```
## Contributing
Send patches on the [mailing list]. Discuss in [#emersion on Libera Chat][IRC channel].
## License
MIT
[GoDoc examples]: https://godocs.io/git.sr.ht/~emersion/gqlclient#example-Client-Execute
[mailing list]: https://lists.sr.ht/~emersion/gqlclient-dev
[IRC channel]: ircs://irc.libera.chat/#emersion
|