Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/steebchen/gqlclient
š A simple generic GraphQL client for Go
https://github.com/steebchen/gqlclient
api go golang gql graphql graphql-client
Last synced: about 1 month ago
JSON representation
š A simple generic GraphQL client for Go
- Host: GitHub
- URL: https://github.com/steebchen/gqlclient
- Owner: steebchen
- License: mit
- Created: 2019-06-03T23:36:24.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2019-11-04T17:39:30.000Z (about 5 years ago)
- Last Synced: 2024-06-20T01:56:38.009Z (5 months ago)
- Topics: api, go, golang, gql, graphql, graphql-client
- Language: Go
- Homepage:
- Size: 46.9 KB
- Stars: 8
- Watchers: 4
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# gqlclient [![GoDoc](https://godoc.org/github.com/steebchen/gqlclient?status.png)](http://godoc.org/github.com/steebchen/gqlclient) [![Go Report Card](https://goreportcard.com/badge/github.com/steebchen/gqlclient)](https://goreportcard.com/report/github.com/steebchen/gqlclient) [![Actions Status](https://github.com/steebchen/gqlclient/workflows/test%20all/badge.svg)](https://github.com/steebchen/gqlclient/actions)
The package gqlclient provides a GraphQL client implementation.
Reasons to use gqlclient:
- Simple, familiar API
- Use strong Go types for variables and response data
- Receive a full GraphQL response with data, errors and extensions
- Respects `context.Context` cancellations and timeouts
- Supports GraphQL Errors with Extensions*Note*: This package already works quite well, but it is under heavy development to work towards a v1.0 release. Before that, the API may have breaking changes even with minor versions.
Coming soon:
- Uploads
- Subscriptions
- More options (e.g. http headers)## Installation
Make sure you have a working Go environment, preferably with Go modules.
To install graphql, simply run:
```
$ go get github.com/steebchen/gqlclient
```## Quickstart
The recommended way is to use structs depending on your schema for best type-safety.
```go
package mainimport (
"log"
"context"
"github.com/steebchen/gqlclient"
)func main() {
client := gqlclient.New("https://metaphysics-production.artsy.net")
var data struct {
Article struct {
ID string
Title string
}
}
type variables struct{
ID string `json:"id"`
}query := `
query Article($id: String!) {
article(id: $id) {
id
title
}
}
`_, err := client.Send(context.Background(), &data, query, variables{
ID: "55bfed9275de7b060098b9bc",
})if err != nil {
panic(err)
}log.Printf("data: %+v", data)
// Output:
// Article: {
// ID: 55bfed9275de7b060098b9bc
// Title: How the 1960sā Most Iconic Artists Made Art Contemporary
// }
}
```If you don't want to use structs, you use `Raw()` to use maps for both input (variables) and output (response data).
```go
resp, err := client.Raw(context.Background(), query, map[string]interface{}{
"id": "55bfed9275de7b060098b9bc",
})if err != nil {
panic(err)
}log.Printf("data: %+v", resp.Data)
// Output:
// data: map[
// article: map[
// id: 55bfed9275de7b060098b9bc
// title: How the 1960sā Most Iconic Artists Made Art Contemporary
// ]
// ]
```Both `Send()` and `Raw()` always return a GraphQL [`Response`](https://godoc.org/github.com/steebchen/gqlclient#Response), so you can access GraphQL Errors and Extensions.