Graphql schema for DAG-JSON objects #112

Closed
roysc wants to merge 10 commits from roy/graphql-to-ipld into main
Showing only changes of commit 21aa925758 - Show all commits

View File

@ -150,20 +150,13 @@ func (k Keeper) ListRecords(ctx sdk.Context) []types.Record {
// RecordsFromAttributes gets a list of records whose attributes match all provided values // RecordsFromAttributes gets a list of records whose attributes match all provided values
func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*types.QueryListRecordsRequest_KeyValueInput, all bool) ([]types.Record, error) { func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*types.QueryListRecordsRequest_KeyValueInput, all bool) ([]types.Record, error) {
log := logger(ctx).With("function", "RecordsFromAttributes")
resultRecordIds := []string{} resultRecordIds := []string{}
for i, attr := range attributes { for i, attr := range attributes {
val, err := EncodeAttributeValue2(attr.Value) suffix, err := EncodeQueryValue(attr.Value)
if err != nil { if err != nil {
return nil, err return nil, err
} }
attributeIndex := GetAttributesIndexKey(attr.Key, val) attributeIndex := GetAttributesIndexKey(attr.Key, suffix)
log.Debug("attribute index",
"key", attr.Key,
"value", val,
"value_type", fmt.Sprintf("%T", val),
"index", attributeIndex)
recordIds, err := k.GetAttributeMapping(ctx, attributeIndex) recordIds, err := k.GetAttributeMapping(ctx, attributeIndex)
if err != nil { if err != nil {
return nil, err return nil, err
@ -191,8 +184,8 @@ func (k Keeper) RecordsFromAttributes(ctx sdk.Context, attributes []*types.Query
return records, nil return records, nil
} }
// TODO non recursive // TODO not recursive, and only should be if we want to support querying with whole sub-objects
func EncodeAttributeValue2(input *types.QueryListRecordsRequest_ValueInput) ([]byte, error) { func EncodeQueryValue(input *types.QueryListRecordsRequest_ValueInput) ([]byte, error) {
np := basicnode.Prototype.Any np := basicnode.Prototype.Any
nb := np.NewBuilder() nb := np.NewBuilder()
@ -344,7 +337,7 @@ func (k Keeper) processRecord(ctx sdk.Context, record *types.RecordEncodable, is
return err return err
} }
expiryTimeKey := GetAttributesIndexKey(ExpiryTimeAttributeName, record.ExpiryTime) expiryTimeKey := GetAttributesIndexKey(ExpiryTimeAttributeName, []byte(record.ExpiryTime))
if err := k.SetAttributeMapping(ctx, expiryTimeKey, record.ID); err != nil { if err := k.SetAttributeMapping(ctx, expiryTimeKey, record.ID); err != nil {
return err return err
} }
@ -391,7 +384,6 @@ func (k Keeper) processAttributeMap(ctx sdk.Context, n ipld.Node, id string, pre
return err return err
} }
// for key, value := range attrs {
if valuenode.Kind() == ipld.Kind_Map { if valuenode.Kind() == ipld.Kind_Map {
k.processAttributeMap(ctx, valuenode, id, key) k.processAttributeMap(ctx, valuenode, id, key)
} else { } else {
@ -409,9 +401,10 @@ func (k Keeper) processAttributeMap(ctx sdk.Context, n ipld.Node, id string, pre
return nil return nil
} }
func GetAttributesIndexKey(key string, value interface{}) []byte { func GetAttributesIndexKey(key string, suffix []byte) []byte {
keyString := fmt.Sprintf("%s%s", key, value) keyString := fmt.Sprintf("%s%s", key, suffix)
return append(PrefixAttributesIndex, []byte(keyString)...) return append(PrefixAttributesIndex, []byte(keyString)...)
// return append(append(PrefixAttributesIndex, key...), suffix...)
} }
func (k Keeper) SetAttributeMapping(ctx sdk.Context, key []byte, recordID string) error { func (k Keeper) SetAttributeMapping(ctx sdk.Context, key []byte, recordID string) error {