Graphql schema for DAG-JSON objects #112

Closed
roysc wants to merge 10 commits from roy/graphql-to-ipld into main
4 changed files with 42 additions and 50 deletions
Showing only changes of commit 4adce81981 - Show all commits

View File

@ -121,7 +121,7 @@ func (q queryResolver) QueryRecords(ctx context.Context, attributes []*KeyValueI
res, err := nsQueryClient.ListRecords(
context.Background(),
&registrytypes.QueryListRecordsRequest{
Attributes: parseRequestAttributes(attributes),
Attributes: toRpcAttributes(attributes),
All: (all != nil && *all),
},
)

View File

@ -256,60 +256,43 @@ func GetGQLAuction(auction *auctiontypes.Auction, bids []*auctiontypes.Bid) (*Au
return &gqlAuction, nil
}
func parseRequestValue(value *ValueInput) *registrytypes.QueryListRecordsRequest_ValueInput {
if value == nil {
func toRpcValue(value *ValueInput) *registrytypes.QueryListRecordsRequest_ValueInput {
var rpcval registrytypes.QueryListRecordsRequest_ValueInput
switch {
case value == nil:
return nil
}
var val registrytypes.QueryListRecordsRequest_ValueInput
if value.String != nil {
val.String_ = *value.String
val.Type = "string"
}
if value.Int != nil {
val.Int = int64(*value.Int)
val.Type = "int"
}
if value.Float != nil {
val.Float = *value.Float
val.Type = "float"
}
if value.Boolean != nil {
val.Boolean = *value.Boolean
val.Type = "boolean"
}
if value.Link != nil {
reference := &registrytypes.QueryListRecordsRequest_ReferenceInput{
Id: value.Link.String(),
case value.Int != nil:
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Int{int64(*value.Int)}
case value.Float != nil:
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Float{*value.Float}
case value.String != nil:
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_String_{*value.String}
case value.Boolean != nil:
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Boolean{*value.Boolean}
case value.Link != nil:
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Link{value.Link.String()}
case value.Array != nil:
var contents registrytypes.QueryListRecordsRequest_ArrayInput
for _, val := range value.Array {
contents.Values = append(contents.Values, toRpcValue(val))
}
val.Reference = reference
val.Type = "reference"
}
// handle arrays
if value.Array != nil {
values := []*registrytypes.QueryListRecordsRequest_ValueInput{}
for _, v := range value.Array {
val := parseRequestValue(v)
values = append(values, val)
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Array{&contents}
case value.Map != nil:
var contents registrytypes.QueryListRecordsRequest_MapInput
for _, kv := range value.Map {
contents.Values[kv.Key] = toRpcValue(kv.Value)
}
val.Values = values
val.Type = "array"
rpcval.Value = &registrytypes.QueryListRecordsRequest_ValueInput_Map{&contents}
}
return &val
return &rpcval
}
func parseRequestAttributes(attrs []*KeyValueInput) []*registrytypes.QueryListRecordsRequest_KeyValueInput {
func toRpcAttributes(attrs []*KeyValueInput) []*registrytypes.QueryListRecordsRequest_KeyValueInput {
kvPairs := []*registrytypes.QueryListRecordsRequest_KeyValueInput{}
for _, value := range attrs {
parsedValue := parseRequestValue(value.Value)
parsedValue := toRpcValue(value.Value)
kvPair := &registrytypes.QueryListRecordsRequest_KeyValueInput{
Key: value.Key,
Value: parsedValue,

View File

@ -68,8 +68,7 @@ func (suite *KeeperTestSuite) TestGrpcGetRecordLists() {
{
Key: "type",
Value: &registrytypes.QueryListRecordsRequest_ValueInput{
Type: "string",
String_: "WebsiteRegistrationRecord",
Value: &registrytypes.QueryListRecordsRequest_ValueInput_String_{"WebsiteRegistrationRecord"},
},
},
},
@ -86,8 +85,7 @@ func (suite *KeeperTestSuite) TestGrpcGetRecordLists() {
{
Key: "x500state_name",
Value: &registrytypes.QueryListRecordsRequest_ValueInput{
Type: "string",
String_: "california",
Value: &registrytypes.QueryListRecordsRequest_ValueInput_String_{"california"},
},
},
},

View File

@ -21,6 +21,9 @@ import (
bank "github.com/cosmos/cosmos-sdk/x/bank/keeper"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"
"github.com/ipld/go-ipld-prime/codec/dagjson"
basicnode "github.com/ipld/go-ipld-prime/node/basic"
)
var (
@ -336,6 +339,14 @@ func (k Keeper) PutRecord(ctx sdk.Context, record types.Record) {
}
func (k Keeper) processAttributes(ctx sdk.Context, attrs map[string]any, id string, prefix string) error {
np := basicnode.Prototype.Any // Pick a stle for the in-memory data.
nb := np.NewBuilder() // Create a builder.
err := dagjson.Decode(nb, bytes.NewReader(content)) // Hand the builder to decoding -- decoding will fill it in!
if err != nil {
return "", err
}
n := nb.Build() // Call 'Build' to get the resulting Node. (It's immutable!)
for key, value := range attrs {
if subRecord, ok := value.(map[string]any); ok {
k.processAttributes(ctx, subRecord, id, key)