|
| 1 | +package connections |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "github.com/hasura/go-graphql-client" |
| 8 | + "go.uber.org/zap" |
| 9 | + "golang.org/x/oauth2" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +const ConnectTimeout = 10 * time.Second |
| 14 | + |
| 15 | +type GraphqlClient struct { |
| 16 | + Host string |
| 17 | + Client *graphql.Client |
| 18 | +} |
| 19 | + |
| 20 | +type GraphqlSubscriptionClient struct { |
| 21 | + Client *graphql.SubscriptionClient |
| 22 | + Id string |
| 23 | +} |
| 24 | + |
| 25 | +func NewGraphqlQueryClient(host string, token string) GraphqlClient { |
| 26 | + src := oauth2.StaticTokenSource( |
| 27 | + &oauth2.Token{AccessToken: token}, |
| 28 | + ) |
| 29 | + httpClient := oauth2.NewClient(context.Background(), src) |
| 30 | + return GraphqlClient{ |
| 31 | + Host: host, |
| 32 | + Client: graphql.NewClient(host, httpClient), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (c GraphqlClient) Connect() error { |
| 37 | + return nil |
| 38 | +} |
| 39 | + |
| 40 | +func NewGraphqlSubscriptionClient(host string, token string) (error, GraphqlSubscriptionClient) { |
| 41 | + client := graphql.NewSubscriptionClient(host). |
| 42 | + WithConnectionParams(map[string]interface{}{ |
| 43 | + "headers": map[string]string{ |
| 44 | + "x-hasura-admin-secret": token, |
| 45 | + }, |
| 46 | + }).OnError(onClientError) |
| 47 | + |
| 48 | + return nil, GraphqlSubscriptionClient{Client: client} |
| 49 | +} |
| 50 | + |
| 51 | +func onClientError(sc *graphql.SubscriptionClient, err error) error { |
| 52 | + zap.S().Fatalf("Connection error on subscription client: %s", err.Error()) |
| 53 | + return err |
| 54 | +} |
| 55 | + |
| 56 | +func (c GraphqlSubscriptionClient) Subscribe(query interface{}, handler func(message *json.RawMessage, err error) error) error { |
| 57 | + id, err := c.Client.Subscribe(query, nil, handler) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + c.Id = id |
| 62 | + return nil |
| 63 | +} |
| 64 | + |
| 65 | +func (c GraphqlSubscriptionClient) Unsubscribe() error { |
| 66 | + err := c.Client.Unsubscribe(c.Id) |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (c GraphqlSubscriptionClient) Start() error { |
| 74 | + errCh := make(chan error) |
| 75 | + readyCh := make(chan bool) |
| 76 | + |
| 77 | + c.Client.OnConnected(func() { |
| 78 | + readyCh <- true |
| 79 | + }) |
| 80 | + |
| 81 | + go func() { |
| 82 | + err := c.Client.Run() |
| 83 | + if err != nil { |
| 84 | + errCh <- err |
| 85 | + } |
| 86 | + }() |
| 87 | + |
| 88 | + for { |
| 89 | + select { |
| 90 | + case err := <-errCh: |
| 91 | + close(errCh) |
| 92 | + close(readyCh) |
| 93 | + return err |
| 94 | + case <-readyCh: |
| 95 | + close(readyCh) |
| 96 | + return nil |
| 97 | + case <-time.After(ConnectTimeout): |
| 98 | + return fmt.Errorf("timeout while waiting subscriber client to connect to host") |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +func (c GraphqlSubscriptionClient) Stop() error { |
| 104 | + err := c.Client.Close() |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + return nil |
| 109 | +} |
0 commit comments