https://github.com/solid-wang/graphqlc
simple go graphql client
https://github.com/solid-wang/graphqlc
client go graphql graphql-client
Last synced: 5 months ago
JSON representation
simple go graphql client
- Host: GitHub
- URL: https://github.com/solid-wang/graphqlc
- Owner: solid-wang
- License: mit
- Created: 2023-07-01T08:12:09.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2023-07-03T01:42:04.000Z (almost 3 years ago)
- Last Synced: 2025-06-07T14:43:20.116Z (about 1 year ago)
- Topics: client, go, graphql, graphql-client
- Language: Go
- Homepage:
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
graphqlc
=======
Package `graphqlc` provides a GraphQL client implementation.
- [graphqlc](#graphqlc)
- [Installation](#installation)
- [Usage](#usage)
- [Query](#query)
- [Simple Query](#simple-query)
- [Arguments and Variables](#arguments-and-variables)
- [Mutations](#mutations)
- [Subscription](#subscription)
## Installation
```bash
go get -u github.com/solid-wang/graphqlc
```
## Usage
Construct a GraphQL client, specifying the GraphQL server URL. Then, you can use it to make GraphQL queries、 mutations and subscription.
```Go
client, _ := graphqlc.NewClient("https://example.com/graphql")
// Use client...
```
### Query
#### Simple Query
For example, to make the following GraphQL query:
```GraphQL
query {
me {
name
}
}
```
You can define this graphql request:
```Go
req := NewGraphRequest(`
query Me {
me {
name
}
}
`, nil)
```
You also need to define the struct:
```Go
type Me struct {
Name string
}
type Response struct {
Me Me
}
```
Then do request, you need passing a pointer to resp:
```Go
var resp Response
err := client.Body(req).QueryOrMutate().Do(context.Background()).Decode(&resp)
if err != nil {
// Handle error.
}
fmt.Println(query.Me.Name)
// Output: Luke Skywalker
```
#### Arguments and Variables
Often, you'll want to specify arguments on some fields.
For example, to make the following GraphQL query:
```GraphQL
{
human(id: "1000") {
name
height(unit: METER)
}
}
```
You can define this graphql request:
```Go
req := NewGraphRequest(`
{
human(id: "1000") {
name
height(unit: METER)
}
}
`, nil)
```
Usually, you might want to use:
```Go
req := NewGraphRequest(`
query Human($id: ID!, $unit: LengthUnit) {
human(id: $id) {
name
height(unit: $unit)
}
}
`, map[string]any{"id": "1000", "unit": "METER"})
```
You also need to define the struct:
```Go
type Human struct {
Name string
height string
}
type Response struct {
Human Human
}
```
Then do request:
```Go
var resp Response
err := client.Body(req).QueryOrMutate().Do(context.Background()).Decode(&resp)
if err != nil {
// Handle error.
}
fmt.Println(q.Human.Name)
fmt.Println(q.Human.Height)
// Output:
// Luke Skywalker
// 1.72
```
### Mutations
Mutations often require information that you can only find out by performing a query first. Let's suppose you've already done that.
For example, to make the following GraphQL mutation:
```GraphQL
mutation($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}
variables {
"ep": "JEDI",
"review": {
"stars": 5,
"commentary": "This is a great movie!"
}
}
```
You can define:
```Go
req := NewGraphRequest(`
mutation($ep: Episode!, $review: ReviewInput!) {
createReview(episode: $ep, review: $review) {
stars
commentary
}
}
`, map[string]any{"ep": "JEDI", "review": map[string]any{"start": 5, "commentary": "This is a great movie!"}})
```
You also need to define the struct:
```Go
type CreateReview struct {
stars int
commentary string
}
type Response struct {
CreateReview CreateReview
}
```
Then do request:
```Go
var resp Response
err = client.Body(req).QueryOrMutate().Do(context.Background()).Decode(&resp)
if err != nil {
// Handle error.
}
fmt.Printf("Created a %v star review: %v\n", m.CreateReview.Stars, m.CreateReview.Commentary)
// Output:
// Created a 5 star review: This is a great movie!
```
### Subscription
For example, to make the following GraphQL query:
```GraphQL
subscription {
me {
name
}
}
```
You can define this graphql request:
```Go
req := NewGraphRequest(`
subscription {
me {
name
}
}
`, nil)
```
You also need to define the struct:
```Go
type Me struct {
Name string
}
type Response struct {
Me Me
}
```
Then run subscribe, passing a pointer to it:
```Go
subscribe := client.Body(req).Subscription()
go subscribe.Run(context.Background())
defer subscribe.Stop()
for {
decoder := <-subscribe.ResultChan()
var resp Response
err := decoder.Decode(&resp)
if err != nil {
// Handle error.
return
}
}
```