forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable Subscription Resolver to return websocket error message (99des…
…igns#2506) * Enanble Subscription Resolver to return websocket error message * add PR link * lint * fmt and regenerate Signed-off-by: Steve Coffman <steve@khanacademy.org> Signed-off-by: Steve Coffman <steve@khanacademy.org> Co-authored-by: Zhixin Wen <zwen@nuro.ai> Co-authored-by: Steve Coffman <steve@khanacademy.org>
- Loading branch information
1 parent
2bd7cfe
commit 11c3a4d
Showing
6 changed files
with
84 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
graphql/handler/apollofederatedtracingv1/generated/apollo_trace.pb.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package transport | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/vektah/gqlparser/v2/gqlerror" | ||
) | ||
|
||
// A private key for context that only this package can access. This is important | ||
// to prevent collisions between different context uses | ||
var wsSubscriptionErrorCtxKey = &wsSubscriptionErrorContextKey{"subscription-error"} | ||
|
||
type wsSubscriptionErrorContextKey struct { | ||
name string | ||
} | ||
|
||
type subscriptionError struct { | ||
errs []*gqlerror.Error | ||
} | ||
|
||
// AddSubscriptionError is used to let websocket return an error message after subscription resolver returns a channel. | ||
// for example: | ||
// | ||
// func (r *subscriptionResolver) Method(ctx context.Context) (<-chan *model.Message, error) { | ||
// ch := make(chan *model.Message) | ||
// go func() { | ||
// defer func() { | ||
// close(ch) | ||
// } | ||
// // some kind of block processing (e.g.: gRPC client streaming) | ||
// stream, err := gRPCClientStreamRequest(ctx) | ||
// if err != nil { | ||
// transport.AddSubscriptionError(ctx, err) | ||
// return // must return and close channel so websocket can send error back | ||
// } | ||
// for { | ||
// m, err := stream.Recv() | ||
// if err == io.EOF { | ||
// return | ||
// } | ||
// if err != nil { | ||
// transport.AddSubscriptionError(ctx, err) | ||
// return // must return and close channel so websocket can send error back | ||
// } | ||
// ch <- m | ||
// } | ||
// }() | ||
// | ||
// return ch, nil | ||
// } | ||
// | ||
// see https://github.com/99designs/gqlgen/pull/2506 for more details | ||
func AddSubscriptionError(ctx context.Context, err *gqlerror.Error) { | ||
subscriptionErrStruct := getSubscriptionErrorStruct(ctx) | ||
subscriptionErrStruct.errs = append(subscriptionErrStruct.errs, err) | ||
} | ||
|
||
func withSubscriptionErrorContext(ctx context.Context) context.Context { | ||
return context.WithValue(ctx, wsSubscriptionErrorCtxKey, &subscriptionError{}) | ||
} | ||
|
||
func getSubscriptionErrorStruct(ctx context.Context) *subscriptionError { | ||
v, _ := ctx.Value(wsSubscriptionErrorCtxKey).(*subscriptionError) | ||
return v | ||
} | ||
|
||
func getSubscriptionError(ctx context.Context) []*gqlerror.Error { | ||
return getSubscriptionErrorStruct(ctx).errs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
plugin/resolvergen/testdata/return_values/return_values_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters