Skip to content

Commit

Permalink
improve the subscription life cycle event and add new settings (shurc…
Browse files Browse the repository at this point in the history
…ooL#82)

* improve the subscription logic and add new settings
  • Loading branch information
hgiasac authored Mar 16, 2023
1 parent 3bcda95 commit 0acd9d9
Show file tree
Hide file tree
Showing 10 changed files with 1,613 additions and 646 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
cd ./example/hasura
docker-compose up -d
- name: Run Go unit tests
run: go test -v -race -coverprofile=coverage.out ./...
run: go test -v -race -timeout 3m -coverprofile=coverage.out ./...
- name: Go coverage format
run: |
go get github.com/boumenot/gocover-cobertura
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,12 @@ client.
// max size of response message
WithReadLimit(10*1024*1024).
// these operation event logs won't be printed
WithoutLogTypes(graphql.GQLData, graphql.GQLConnectionKeepAlive)
WithoutLogTypes(graphql.GQLData, graphql.GQLConnectionKeepAlive).
// the client should exit when all subscriptions were closed, default true
WithExitWhenNoSubscription(false).
// WithRetryStatusCodes allow retry the subscription connection when receiving one of these codes
// the input parameter can be number string or range, e.g 4000-5000
WithRetryStatusCodes("4000", "4000-4050")
```
#### Subscription Protocols
Expand Down Expand Up @@ -629,6 +634,12 @@ client.OnDisconnected(fn func())
// If this function is empty, or returns nil, the error is ignored
// If returns error, the websocket connection will be terminated
client.OnError(onError func(sc *SubscriptionClient, err error) error)

// OnConnectionAlive event is triggered when the websocket receive a connection alive message (differs per protocol)
client.OnConnectionAlive(fn func())

// OnSubscriptionComplete event is triggered when the subscription receives a terminated message from the server
client.OnSubscriptionComplete(fn func(sub Subscription))
```
#### Custom HTTP Client
Expand Down
12 changes: 6 additions & 6 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,22 @@ func ConstructMutation(v interface{}, variables map[string]interface{}, options
}

// ConstructSubscription build GraphQL subscription string from struct and variables
func ConstructSubscription(v interface{}, variables map[string]interface{}, options ...Option) (string, error) {
func ConstructSubscription(v interface{}, variables map[string]interface{}, options ...Option) (string, string, error) {
query, err := query(v)
if err != nil {
return "", err
return "", "", err
}
optionsOutput, err := constructOptions(options)
if err != nil {
return "", err
return "", "", err
}
if len(variables) > 0 {
return fmt.Sprintf("subscription %s(%s)%s%s", optionsOutput.operationName, queryArguments(variables), optionsOutput.OperationDirectivesString(), query), nil
return fmt.Sprintf("subscription %s(%s)%s%s", optionsOutput.operationName, queryArguments(variables), optionsOutput.OperationDirectivesString(), query), optionsOutput.operationName, nil
}
if optionsOutput.operationName == "" && len(optionsOutput.operationDirectives) == 0 {
return "subscription" + query, nil
return "subscription" + query, optionsOutput.operationName, nil
}
return fmt.Sprintf("subscription %s%s%s", optionsOutput.operationName, optionsOutput.OperationDirectivesString(), query), nil
return fmt.Sprintf("subscription %s%s%s", optionsOutput.operationName, optionsOutput.OperationDirectivesString(), query), optionsOutput.operationName, nil
}

// queryArguments constructs a minified arguments string for variables.
Expand Down
11 changes: 8 additions & 3 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -635,11 +635,16 @@ func TestConstructSubscription(t *testing.T) {
},
}
for _, tc := range tests {
got, err := ConstructSubscription(tc.inV, tc.inVariables, OperationName(tc.name))
got, gotName, err := ConstructSubscription(tc.inV, tc.inVariables, OperationName(tc.name))
if err != nil {
t.Error(err)
} else if got != tc.want {
t.Errorf("\ngot: %q\nwant: %q\n", got, tc.want)
} else {
if got != tc.want {
t.Errorf("\ngot: %q\nwant: %q\n", got, tc.want)
}
if gotName != tc.name {
t.Errorf("\ninvalid operation name \ngot: %q\nwant: %q\n", gotName, tc.name)
}
}
}
}
Expand Down
Loading

0 comments on commit 0acd9d9

Please sign in to comment.