Skip to content

Commit

Permalink
add debug mode, expose GraphQL builder functions and UnmarshalGraphQL…
Browse files Browse the repository at this point in the history
… for unit tests (#30)

add debug mode, expose GraphQL builder functions and UnmarshalGraphQL for unit tests
  • Loading branch information
hgiasac authored Mar 14, 2022
1 parent 965b8db commit 7e27ec6
Show file tree
Hide file tree
Showing 6 changed files with 335 additions and 96 deletions.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ For more information, see package [`github.com/shurcooL/githubv4`](https://githu
- [Authentication](#authentication-1)
- [Options](#options)
- [Events](#events)
- [Custom HTTP Client](#custom-http-client)
- [Custom WebSocket client](#custom-websocket-client)
- [Options](#options-1)
- [With operation name (deprecated)](#with-operation-name-deprecated)
- [Raw bytes response](#raw-bytes-response)
- [Multiple mutations with ordered map](#multiple-mutations-with-ordered-map)
- [Debugging and Unit test](#debugging-and-unit-test)
- [Directories](#directories)
- [References](#references)
- [License](#license)
Expand Down Expand Up @@ -500,6 +502,19 @@ client.OnDisconnected(fn func())
client.OnError(onError func(sc *SubscriptionClient, err error) error)
```
#### Custom HTTP Client
Use `WithWebSocketOptions` to customize the HTTP client which is used by the subscription client.
```go
client.WithWebSocketOptions(WebsocketOptions{
HTTPClient: &http.Client{
Transport: http.DefaultTransport,
Timeout: time.Minute,
}
})
```
#### Custom WebSocket client
By default the subscription client uses [nhooyr WebSocket client](https://github.com/nhooyr/websocket). If you need to customize the client, or prefer using [Gorilla WebSocket](https://github.com/gorilla/websocket), let's follow the Websocket interface and replace the constructor with `WithWebSocket` method:
Expand Down Expand Up @@ -665,6 +680,59 @@ variables := map[string]interface{}{
}
```
### Debugging and Unit test
Enable debug mode with the `WithDebug` function. If the request is failed, the request and response information will be included in `extensions[].internal` property.
```json
{
"errors": [
{
"message":"Field 'user' is missing required arguments: login",
"extensions": {
"internal": {
"request": {
"body":"{\"query\":\"{user{name}}\"}",
"headers": {
"Content-Type": ["application/json"]
}
},
"response": {
"body":"{\"errors\": [{\"message\": \"Field 'user' is missing required arguments: login\",\"locations\": [{\"line\": 7,\"column\": 3}]}]}",
"headers": {
"Content-Type": ["application/json"]
}
}
}
},
"locations": [
{
"line":7,
"column":3
}
]
}
]
}
```
Because the GraphQL query string is generated in runtime using reflection, it isn't really safe. To assure the GraphQL query is expected, it's necessary to write some unit test for query construction.
```go
// ConstructQuery build GraphQL query string from struct and variables
func ConstructQuery(v interface{}, variables map[string]interface{}, options ...Option) (string, error)

// ConstructQuery build GraphQL mutation string from struct and variables
func ConstructMutation(v interface{}, variables map[string]interface{}, options ...Option) (string, error)

// ConstructSubscription build GraphQL subscription string from struct and variables
func ConstructSubscription(v interface{}, variables map[string]interface{}, options ...Option) (string, error)

// UnmarshalGraphQL parses the JSON-encoded GraphQL response data and stores
// the result in the GraphQL query data structure pointed to by v.
func UnmarshalGraphQL(data []byte, v interface{}) error
```
Directories
-----------
Expand Down
Loading

0 comments on commit 7e27ec6

Please sign in to comment.