Skip to content

Commit

Permalink
feat(explorer/graphql): reading from db (#190)
Browse files Browse the repository at this point in the history
Hooking `graphql` into db as well as adding local endpoint without `db`
for testing

task: none
  • Loading branch information
DanFlannel authored Jan 30, 2024
1 parent 96522ca commit 88daf44
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
16 changes: 14 additions & 2 deletions explorer/graphql/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"time"

"github.com/omni-network/omni/explorer/db"
"github.com/omni-network/omni/explorer/graphql/data"
"github.com/omni-network/omni/lib/errors"
"github.com/omni-network/omni/lib/log"
Expand All @@ -16,7 +17,18 @@ func Run(ctx context.Context, conf ExplorerGraphQLConfig) error {
ctx, cancel := context.WithCancel(ctx)

go func() {
provider := data.Provider{}
// create ent client
entClient := db.NewClient()
client, err := entClient.CreateNewEntClient(conf.DBUrl)

if err != nil {
log.Error(ctx, "Failed to open ent client", err)
return
}

provider := data.Provider{
EntClient: client,
}

mux := http.NewServeMux()

Expand All @@ -33,7 +45,7 @@ func Run(ctx context.Context, conf ExplorerGraphQLConfig) error {

log.Info(ctx, "Starting to serve GraphQL - API on port: %v", httpServer.Addr)

err := httpServer.ListenAndServe()
err = httpServer.ListenAndServe()
if errors.Is(err, http.ErrServerClosed) {
log.Info(ctx, "Closed http server @%v", httpServer.Addr)
} else {
Expand Down
32 changes: 19 additions & 13 deletions explorer/graphql/data/provider.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,40 @@
package data

import (
"context"
"math/big"
"time"

"github.com/omni-network/omni/explorer/db/ent"
"github.com/omni-network/omni/explorer/db/ent/block"
"github.com/omni-network/omni/explorer/graphql/resolvers"
"github.com/omni-network/omni/lib/log"

"github.com/ethereum/go-ethereum/common"

"github.com/graph-gophers/graphql-go"
)

type Provider struct {
EntClient ent.Client
EntClient *ent.Client
}

func (Provider) XBlock(sourceChainID uint64, height uint64) (*resolvers.XBlock, bool, error) {
h := common.Hash{}
h.SetBytes([]byte{1, 3, 23, 111, 27, 45, 98, 103, 94, 55, 1, 3, 23, 111, 27, 45, 98, 103, 94, 55})
var chainID big.Int
chainID.SetUint64(sourceChainID)
var blockHeight big.Int
blockHeight.SetUint64(height)
func (p Provider) XBlock(sourceChainID uint64, height uint64) (*resolvers.XBlock, bool, error) {
ctx := context.Background()
query, err := p.EntClient.Block.Query().
Where(block.SourceChainID(sourceChainID)).
Where(block.BlockHeight(height)).
First(ctx)

if err != nil {
log.Error(ctx, "Graphql provider err", err)
return nil, false, err
}

res := resolvers.XBlock{
SourceChainIDRaw: resolvers.BigInt{Int: chainID},
BlockHeightRaw: resolvers.BigInt{Int: blockHeight},
BlockHashRaw: h,
Timestamp: graphql.Time{Time: time.Now()},
SourceChainIDRaw: resolvers.BigInt{Int: *new(big.Int).SetUint64(query.SourceChainID)},
BlockHeightRaw: resolvers.BigInt{Int: *new(big.Int).SetUint64(query.BlockHeight)},
BlockHashRaw: common.Hash(query.BlockHash),
Timestamp: graphql.Time{Time: query.Timestamp},
Messages: dummyMessages(),
}

Expand Down

0 comments on commit 88daf44

Please sign in to comment.