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