Files
go-caatsm/internal/repository/hasura.go
T

47 lines
1.2 KiB
Go
Raw Normal View History

package repository
import (
"context"
"os"
"caatsm/internal/domain"
2024-07-23 11:29:12 +08:00
"github.com/Khan/genqlient/graphql"
"github.com/google/uuid"
"golang.org/x/oauth2"
)
type HasuraRepository struct {
2024-07-23 11:29:12 +08:00
client graphql.Client
}
2024-07-23 11:29:12 +08:00
// NewHasuraRepo creates a new HasuraRepository
func NewHasuraRepo(endpoint, secret string) *HasuraRepository {
src := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: os.Getenv("GRAPHQL_TOKEN")},
)
httpClient := oauth2.NewClient(context.Background(), src)
return &HasuraRepository{
2024-07-23 11:29:12 +08:00
client: graphql.NewClient(endpoint, httpClient),
}
}
// InsertParsedMessage inserts a new ParsedMessage into the Hasura GraphQL API
func (hr *HasuraRepository) InsertParsedMessage(pm *domain.ParsedMessage) error {
2024-07-23 11:29:12 +08:00
variables := Aviation_telegrams_insert_input{
// Id: 10,
Body_and_footer: pm.BodyAndFooter,
// Body_data: pm.BodyData.(json.RawMessage),
Category: pm.Category,
Date_time: pm.DateTime,
Dispatched_at: pm.DispatchedAt,
Uuid: uuid.New(),
}
2024-07-23 11:29:12 +08:00
_, err := newMessage(context.Background(), hr.client, variables)
if err != nil {
return err
}
2024-07-23 11:29:12 +08:00
// fmt.Printf("Inserted new message with ID: %v\n", resp)
return nil
}