Skip to content

Commit

Permalink
update graphql to have more useful error messages (#188)
Browse files Browse the repository at this point in the history
* add node loader for recursive graqphl queries
* fix generation using repr, and parsing of past values
  • Loading branch information
willscott authored Jun 8, 2021
1 parent 1583fd0 commit 3890009
Show file tree
Hide file tree
Showing 8 changed files with 1,408 additions and 1,034 deletions.
1,977 changes: 989 additions & 988 deletions controller/graphql/schema.go

Large diffs are not rendered by default.

53 changes: 52 additions & 1 deletion controller/graphql/server.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package graphql

import (
"bytes"
"context"
"embed"
"encoding/json"
"fmt"
"io"
"log"
"net/http"

"github.com/filecoin-project/dealbot/controller/state"
"github.com/filecoin-project/dealbot/tasks"
"github.com/graphql-go/graphql"
ipld "github.com/ipld/go-ipld-prime"
"github.com/ipld/go-ipld-prime/codec/dagjson"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
)

//go:embed index.html
Expand Down Expand Up @@ -77,6 +83,37 @@ func GetHandler(db state.State, accessToken string) (*http.ServeMux, error) {
return tsk, nil
},
},
"FinishedTask": &graphql.Field{
Type: Task__type,
Args: graphql.FieldConfigArgument{
"AccessToken": &graphql.ArgumentConfig{Type: graphql.String, Description: "potentially access-restricted query"},
"UUID": &graphql.ArgumentConfig{Type: graphql.NewNonNull(graphql.String), Description: "task uuid"},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
if accessToken != "" {
at, ok := p.Args["AccessToken"]
if !ok || at.(string) != accessToken {
return nil, fmt.Errorf("access token required")
}
}

uuid := p.Args["UUID"].(string)
tsk, err := db.Get(p.Context, uuid)
store := db.Store(p.Context)
storer := func(_ ipld.LinkContext) (io.Writer, ipld.StoreCommitter, error) {
buf := bytes.Buffer{}
return &buf, func(l ipld.Link) error {
c := l.(cidlink.Link).Cid
return store.Set(c, buf.Bytes())
}, nil
}
finished, err := tsk.Finalize(p.Context, storer)
if err != nil {
return nil, err
}
return finished, nil
},
},
"RecordUpdate": &graphql.Field{
Type: RecordUpdate__type,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
Expand All @@ -94,11 +131,25 @@ func GetHandler(db state.State, accessToken string) (*http.ServeMux, error) {
return nil, err
}

loader := func(ctx context.Context, cl cidlink.Link, builder ipld.NodeBuilder) (ipld.Node, error) {
store := db.Store(ctx)
block, err := store.Get(cl.Cid)
if err != nil {
return nil, err
}
if err := dagjson.Decoder(builder, bytes.NewBuffer(block.RawData())); err != nil {
return nil, err
}

n := builder.Build()
return n, nil
}

mux := http.NewServeMux()
mux.Handle("/", http.FileServer(http.FS(index)))
mux.Handle("/graphql", CorsMiddleware(func(w http.ResponseWriter, r *http.Request) {
var result *graphql.Result
ctx := r.Context()
ctx := context.WithValue(r.Context(), nodeLoaderCtxKey, loader)

if r.Method == "POST" && r.Header.Get("Content-Type") == "application/json" {
var p postData
Expand Down
1 change: 1 addition & 0 deletions controller/state/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,5 @@ type State interface {
type Store interface {
Head() (cid.Cid, error)
Get(cid.Cid) (blockformat.Block, error)
Set(cid.Cid, []byte) error
}
11 changes: 9 additions & 2 deletions controller/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ func (s *sdbstore) Head() (cid.Cid, error) {
return headCid, nil
}

func (s *sdbstore) Set(c cid.Cid, data []byte) error {
return s.stateDB.transact(s.Context, func(tx *sql.Tx) error {
_, err := tx.ExecContext(s.Context, cidArchiveSQL, c.String(), data, time.Now())
return err
})
}

func (s *stateDB) db() *sql.DB {
return s.dbconn.SqlDB()
}
Expand Down Expand Up @@ -453,7 +460,7 @@ func (s *stateDB) Update(ctx context.Context, taskID string, req tasks.UpdateTas
if err != nil {
return err
}
flink, err := linkProto.Build(ctx, ipld.LinkContext{}, finalized, txContextStorer(ctx, tx))
flink, err := linkProto.Build(ctx, ipld.LinkContext{}, finalized.Representation(), txContextStorer(ctx, tx))
if err != nil {
return err
}
Expand Down Expand Up @@ -749,7 +756,7 @@ func (s *stateDB) PublishRecordsFrom(ctx context.Context, worker string) error {
rcrdlst := tasks.Type.List_AuthenticatedRecord.Of(rcrds)

update := tasks.Type.RecordUpdate.Of(rcrdlst, headCid, headCidSig)
updateCid, err := linkProto.Build(ctx, ipld.LinkContext{}, update, txContextStorer(ctx, tx))
updateCid, err := linkProto.Build(ctx, ipld.LinkContext{}, update.Representation(), txContextStorer(ctx, tx))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/ipfs/go-cid v0.0.7
github.com/ipfs/go-log/v2 v2.1.2
github.com/ipld/go-car v0.1.1-0.20201119040415-11b6074b6d4d
github.com/ipld/go-ipld-graphql v0.0.0-20210425153336-bd6fb64874b6
github.com/ipld/go-ipld-graphql v0.0.0-20210608181858-5e7994523c5a
github.com/ipld/go-ipld-prime v0.7.1-0.20210519202903-3a67953d6ef3
github.com/ipld/go-ipld-prime-proto v0.1.1 // indirect
github.com/lib/pq v1.9.0
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,12 @@ github.com/ipld/go-car v0.1.1-0.20201119040415-11b6074b6d4d h1:iphSzTuPqyDgH7WUV
github.com/ipld/go-car v0.1.1-0.20201119040415-11b6074b6d4d/go.mod h1:2Gys8L8MJ6zkh1gktTSXreY63t4UbyvNp5JaudTyxHQ=
github.com/ipld/go-ipld-graphql v0.0.0-20210425153336-bd6fb64874b6 h1:sL8KmNP+r/RyIXftsi8n4Z7wf7i54zF5p9r//zY0z6I=
github.com/ipld/go-ipld-graphql v0.0.0-20210425153336-bd6fb64874b6/go.mod h1:mOMXf1WKBUilb6y+zerpQrDrS/mEzFmkauXfR2Tz55o=
github.com/ipld/go-ipld-graphql v0.0.0-20210608175138-5ce6ea648d27 h1:j5hfmica0XHsiUWCvWNrMTeibF89Gduzy+ssICPM4Mg=
github.com/ipld/go-ipld-graphql v0.0.0-20210608175138-5ce6ea648d27/go.mod h1:mOMXf1WKBUilb6y+zerpQrDrS/mEzFmkauXfR2Tz55o=
github.com/ipld/go-ipld-graphql v0.0.0-20210608181402-402a472355ba h1:uf699sFhy7rwHX+vvcS5jS9QGbHd5jviD8eLGsCf2go=
github.com/ipld/go-ipld-graphql v0.0.0-20210608181402-402a472355ba/go.mod h1:mOMXf1WKBUilb6y+zerpQrDrS/mEzFmkauXfR2Tz55o=
github.com/ipld/go-ipld-graphql v0.0.0-20210608181858-5e7994523c5a h1:SHbnaSlBCr/HV1xESbE136iE+eqAPOW+B6NNKul7eBI=
github.com/ipld/go-ipld-graphql v0.0.0-20210608181858-5e7994523c5a/go.mod h1:mOMXf1WKBUilb6y+zerpQrDrS/mEzFmkauXfR2Tz55o=
github.com/ipld/go-ipld-prime v0.0.2-0.20200428162820-8b59dc292b8e/go.mod h1:uVIwe/u0H4VdKv3kaN1ck7uCb6yD9cFLS9/ELyXbsw8=
github.com/ipld/go-ipld-prime v0.5.1-0.20200828233916-988837377a7f/go.mod h1:0xEgdD6MKbZ1vF0GC+YcR/C4SQCAlRuOjIJ2i0HxqzM=
github.com/ipld/go-ipld-prime v0.5.1-0.20201021195245-109253e8a018/go.mod h1:0xEgdD6MKbZ1vF0GC+YcR/C4SQCAlRuOjIJ2i0HxqzM=
Expand Down
28 changes: 14 additions & 14 deletions tasks/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ func main() {
schema.SpawnStructField("Miner", "String", false, false),
schema.SpawnStructField("PayloadCID", "String", false, false),
schema.SpawnStructField("CARExport", "Bool", false, false),
schema.SpawnStructField("Schedule", "String", true, false),
schema.SpawnStructField("ScheduleLimit", "String", true, false),
schema.SpawnStructField("Tag", "String", true, false),
schema.SpawnStructField("MaxPriceAttoFIL", "Int", true, false),
schema.SpawnStructField("Schedule", "String", true, true),
schema.SpawnStructField("ScheduleLimit", "String", true, true),
schema.SpawnStructField("Tag", "String", true, true),
schema.SpawnStructField("MaxPriceAttoFIL", "Int", true, true),
}, schema.SpawnStructRepresentationMap(map[string]string{})))

ts.Accumulate(schema.SpawnStruct("StorageTask", []schema.StructField{
Expand All @@ -103,9 +103,9 @@ func main() {
schema.SpawnStructField("StartOffset", "Int", false, false),
schema.SpawnStructField("FastRetrieval", "Bool", false, false),
schema.SpawnStructField("Verified", "Bool", false, false),
schema.SpawnStructField("Schedule", "String", true, false),
schema.SpawnStructField("ScheduleLimit", "String", true, false),
schema.SpawnStructField("Tag", "String", true, false),
schema.SpawnStructField("Schedule", "String", true, true),
schema.SpawnStructField("ScheduleLimit", "String", true, true),
schema.SpawnStructField("Tag", "String", true, true),
}, schema.SpawnStructRepresentationMap(map[string]string{})))

ts.Accumulate(schema.SpawnStruct("Task", []schema.StructField{
Expand All @@ -127,17 +127,17 @@ func main() {
schema.SpawnStructField("Status", "Status", false, false),
schema.SpawnStructField("StartedAt", "Time", false, false),
schema.SpawnStructField("ErrorMessage", "String", true, false),
schema.SpawnStructField("RetrievalTask", "RetrievalTask", true, false),
schema.SpawnStructField("StorageTask", "StorageTask", true, false),
schema.SpawnStructField("RetrievalTask", "RetrievalTask", true, true),
schema.SpawnStructField("StorageTask", "StorageTask", true, true),
schema.SpawnStructField("DealID", "Int", false, false),
schema.SpawnStructField("MinerMultiAddr", "String", false, false),
schema.SpawnStructField("ClientApparentAddr", "String", false, false),
schema.SpawnStructField("MinerLatencyMS", "Int", true, false),
schema.SpawnStructField("TimeToFirstByteMS", "Int", true, false),
schema.SpawnStructField("TimeToLastByteMS", "Int", true, false),
schema.SpawnStructField("MinerLatencyMS", "Int", true, true),
schema.SpawnStructField("TimeToFirstByteMS", "Int", true, true),
schema.SpawnStructField("TimeToLastByteMS", "Int", true, true),
schema.SpawnStructField("Events", "Link_List_StageDetails", false, false),
schema.SpawnStructField("MinerVersion", "String", true, false),
schema.SpawnStructField("ClientVersion", "String", true, false),
schema.SpawnStructField("MinerVersion", "String", true, true),
schema.SpawnStructField("ClientVersion", "String", true, true),
}, schema.SpawnStructRepresentationMap(map[string]string{})))
ts.Accumulate(schema.SpawnLinkReference("Link_FinishedTask", "FinishedTask"))

Expand Down
Loading

0 comments on commit 3890009

Please sign in to comment.